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

18
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

Upload: nigel-sullivan

Post on 05-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 2: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 3: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 4: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 5: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;}

Page 6: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

Define the implementation

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

shape->body->p = pos; }

-(void)dealloc{

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

}

@end

Page 7: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 8: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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];

}

Page 9: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

Remove it if it's off of the screen

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

}

Page 10: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

Define the flag for the collision type

In the enum of 'CollisionTypes' add:

kColl_FlyingSaucer

Page 11: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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);

Page 12: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;

}

Page 13: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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

Page 14: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;}

Page 15: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;}

Page 16: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;

}

Page 17: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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;

Page 18: Sprites Actions/Particles/Sound Revisit the 'flying saucer' sprite: Again add it to the game Implement Chipmunk Physics collision feedback with missiles

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];