Hi there,
ok so im trying to learn about arrays from various tutorials, in particular arrays with sprites. I have got a player on screen that moves and shoots but the problem is that when the bullet reaches the end of its life... It doesn't disappear. I cant seem to remove it from the stage. Here is what i have so far:
EDIT: forgot to mention im using Cocos2d 0.99.5 :-)
Code:
-(id) init
{
if( (self=[super init] )) {
canShoot = YES;
touchHeld = NO;
player = [CCSprite spriteWithFile:@"playershieldmax.png"];
player.position = ccp(200,70);
player.scale = 0.2;
[self addChild:player];
projectiles = [[NSMutableArray alloc] init];
[[CCTouchDispatcher sharedDispatcher]addTargetedDelegate:self priority:0 swallowsTouches:YES];
self.isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 30)];
}
return self;
}
//Accelerometer stuff
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
touchHeld = TRUE;
return YES;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
touchHeld = FALSE;
if(!canShoot) return;
if (_nextProjectile != nil) return;
canShoot = 0;
_nextProjectile = [[CCSprite spriteWithFile:@"Bullet.png"] retain];
_nextProjectile.position = ccp(player.position.x,player.position.y);
[self addChild:_nextProjectile];
[projectiles addObject:_nextProjectile];
id action = [CCMoveTo actionWithDuration:1 position:ccp(player.position.x, 250)];
id ease = [CCEaseIn actionWithAction:action rate:1];
[_nextProjectile runAction:ease];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(finishShoot) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(canShootFunc) userInfo:nil repeats:NO];
[self finishShoot];
_nextProjectile.tag = 2;
}
-(void)canShootFunc{
canShoot = TRUE;
}
- (void)finishShoot {
// Release
[_nextProjectile release];
_nextProjectile = nil;
[self removeChild:_nextProjectile cleanup:YES];
[projectiles removeObject:_nextProjectile];
}
Any help is greatly appreciated!
Thanks.