Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2011, 05:56 AM   #1 (permalink)
Registered Member
 
Joe-Kinglake's Avatar
 
Join Date: Mar 2011
Posts: 128
Joe-Kinglake is on a distinguished road
Default Bullet Array Problem

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.
__________________
--------------------------------------------
Our Site
http://www.iworldapps.net/

Like Us On Facebook
http://on.fb.me/fcxiIZ



--------------------------------------------

Last edited by Joe-Kinglake; 08-15-2011 at 06:00 AM.
Joe-Kinglake is offline   Reply With Quote
Old 08-15-2011, 09:04 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Location: Edinburgh
Posts: 209
Justinmichael is on a distinguished road
Default

Quote:
Originally Posted by Joe-Kinglake View Post
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.
Easy, you are trying to remove nil from the projectiles array in finish shoot - you set it to nil on the first and second line of the finishShoot method. Move those two lines to the end of the method and you should be ok.

Quote:
- (void)finishShoot {

// Release
[self removeChild:_nextProjectile cleanup:YES];
[projectiles removeObject:_nextProjectile];
[_nextProjectile release];
_nextProjectile = nil;


}
__________________
Justinmichael is offline   Reply With Quote
Old 08-16-2011, 02:55 PM   #3 (permalink)
Registered Member
 
Joe-Kinglake's Avatar
 
Join Date: Mar 2011
Posts: 128
Joe-Kinglake is on a distinguished road
Default

Ahh yeah, thanks a lot.

EDIT: also i realised i wasn't removing the child with tag only removing the child. Works perfek now.
__________________
--------------------------------------------
Our Site
http://www.iworldapps.net/

Like Us On Facebook
http://on.fb.me/fcxiIZ



--------------------------------------------

Last edited by Joe-Kinglake; 08-17-2011 at 06:38 AM.
Joe-Kinglake is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 413
11 members and 402 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,923
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:20 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0