sprites actions/particles/sound revisit the 'flying saucer' sprite: again add it to the...

Post on 05-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Sprites Actions/Particles/Sound

Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles and asteroids Use the particle system to give the impression that it has blown up, and play a 'boom' sound as well

Tie in with physics engine

By subclassing CCSprite so that:

init method will create the collision body and collision shape setPosition method will also tell the physics engine the position of the sprite dealloc method will also remove the physics body and shape when the sprite is deallocated

Tie in with physics engine

In addition: create a flag in the in the object to mark it as “blown up' put a pointer to the object in the collision shape so we can get to the flag in the physics engine Collision Handler actually remove the flying saucer in the step: method of the AsteroidLayer

Define the interface

#define FLYINGSAUCERMASS (10.0f)

@interface FlyingSaucerSprite : CCSprite{

cpShape *shape;bool blowItUp;

}

@property cpShape *shape;@property bool blowItUp;

-(id) initWithFile:(NSString*)filename;-(void)setPosition:(CGPoint)pos;-(void)dealloc;

@end

Define the implementation@implementation FlyingSaucerSprite@synthesize shape;@synthesize blowItUp;

-(id)initWithFile:(NSString*)filename{if ((self=[super initWithFile:filename])) {float radius = 35.0f;cpBody *body = cpBodyNew(FLYINGSAUCERMASS,

cpMomentForCircle(FLYINGSAUCERMASS, 0.0f, radius, cpvzero));cpSpaceAddBody(space, body);

shape = cpCircleShapeNew(body, radius, cpvzero);shape->collision_type = kColl_FlyingSaucer;shape->data = self; // So we can get a pointer to this instance from within a

Chipmunk Physics collision callbackcpSpaceAddShape(space, shape);

blowItUp = NO;}return self;}

Define the implementation

-(void)setPosition:(CGPoint)pos{ [super setPosition:pos];

shape->body->p = pos; }

-(void)dealloc{

removeFromSpace(space, shape);[super dealloc];

}

@end

Integrating it into the game

Create a global to hold the once instance that we will have:FlyingSaucerSprite *flyingSaucer = NULL;

In the step method in the AsteroidsLayer: Create a space ship every so often Remove it when it goes off of the screen Only allow one at a time

Create one every so often if there isn't one

if (flyingSaucer == NULL && rand()%100 == 0) {flyingSaucer= [FlyingSaucerSprite

spriteWithFile:@"2d_alien_inside_his_spaceship_royalty_free.png"];[self addChild:flyingSaucer];

CGSize s = [[CCDirector sharedDirector] winSize];cpFloat yPos = rand()%((int)s.height);CGPoint pos = ccp(s.width, yPos);[flyingSaucer setPosition:pos];CCAction *overAction = [CCMoveBy actionWithDuration:5

position:ccp(-s.width,0)];[flyingSaucer runAction:overAction];

}

Remove it if it's off of the screen

if (flyingSaucer != NULL && flyingSaucer.position.x <= 0.0f) {[self removeChild:flyingSaucer cleanup:YES];flyingSaucer = NULL;

}

Define the flag for the collision type

In the enum of 'CollisionTypes' add:

kColl_FlyingSaucer

Define the collision handlers

In the init method of AsteroidLayer add:

cpSpaceAddCollisionHandler(space, kColl_FlyingSaucer, kColl_Asteroid, &beginFlyingSaucerHitAsteroid, NULL, NULL, NULL, NULL);

cpSpaceAddCollisionHandler(space, kColl_Missile, kColl_FlyingSaucer, &beginMissileHitFlyingSaucer, NULL, NULL, NULL, NULL);

Define the collision handlers themselves

int beginFlyingSaucerHitAsteroid(cpArbiter *arb, cpSpace *space __attribute__ ((unused)), void *data __attribute__ ((unused))) {NSLog(@"flyingSaucerHitAsteroid!!!\n");return 1;

}

int beginMissileHitFlyingSaucer(cpArbiter *arb, cpSpace *space __attribute__ ((unused)), void *data __attribute__ ((unused))) {

NSLog(@"missileHitFlyingSaucer(!!!\n");return 1;

}

So give it a try!

Next we will: add code in the collision handlers and step: method to remove the flying saucer use the particle system to show a display when we remove it play some sound as well

Mark the flying saucer as blown up & leave the asteroid alone

int beginFlyingSaucerHitAsteroid(cpArbiter *arb, cpSpace *space __attribute__ ((unused)), void *data __attribute__ ((unused))) {NSLog(@"FlyingSaucerHitAsteroid!!!\n");

cpShape *flyingSaucerShape, *asteroidShape; cpArbiterGetShapes(arb, &flyingSaucerShape, &asteroidShape);

FlyingSaucerSprite *flyingSaucer = flyingSaucerShape->data;flyingSaucer.blowItUp = YES;

return 0;}

Mark the flying saucer as blown up & distroy the missile

int beginMissileHitFlyingSaucer(cpArbiter *arb, cpSpace *space __attribute__ ((unused)), void *data __attribute__ ((unused))) {NSLog(@"MissileHitFlyingSaucer!!!\n");

cpShape *missileShape, *flyingSaucerShape; cpArbiterGetShapes(arb, &missileShape, &flyingSaucerShape);

cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepDistroyMissile, missileShape, NULL);

FlyingSaucerSprite *flyingSaucer = flyingSaucerShape->data;flyingSaucer.blowItUp = YES;

return 0;}

Add code in the step: method to do the actual work

// If the flying Saucer exists and has just left the screen, then remove it...if (flyingSaucer != NULL &&

(flyingSaucer.position.x <= 0.0f || flyingSaucer.blowItUp == YES)) {if (flyingSaucer.blowItUp == YES) {

NSLog(@"step: blowup FlyingSaucer!!!\n");// we should do something interesting graphically here....

}// if it's off of the screen remove it...[self removeChild:flyingSaucer cleanup:YES];flyingSaucer = NULL;

}

Add the particles and sound

NSLog(@"step: blowup FlyingSaucer!!!\n");// we should do something interesting graphically here....[[SimpleAudioEngine sharedEngine] playEffect:EXPLOSION_WAV];

CCParticleSystem *emitter = [CCParticleExplosion node];[self addChild: emitter z:1];

emitter.texture = [[CCTextureCache sharedTextureCache] addImage: @"stars-grayscale.png"];

emitter.autoRemoveOnFinish = YES; // This will cause the object to be garbage collected

cpShape *shape = flyingSaucer.shape;emitter.position = shape->body->p;

Don't forget

To define the wav file and symbols for the audio engine:

#import "SimpleAudioEngine.h"#define EXPLOSION_WAV @"explosion.wav"

Preload the wav file in the applicationDidFinishLaunching:[[SimpleAudioEngine sharedEngine] preloadEffect: EXPLOSION_WAV];

top related