How to create array with unique sprites? in cocos2d iphone

Posted by prakash s on Game Development See other posts from Game Development or by prakash s
Published on 2012-10-01T12:01:56Z Indexed on 2012/10/01 15:54 UTC
Read the original article Hit count: 391

Filed under:

I write the code like this. This displays only one sprite (red colour bubble) with number of times and moving down, but actually I want to display different sprites (different colour bubble) every time and moving down. I also add no of .png images in resource folder of my project. Here I used only 3.png, but I need to display all *.png images (different colour bubbles) in my project but I don't know how to get this. Please help me Thank you. Here is the code:

-(void)addTarget {
CCSprite *target = [CCSprite spriteWithFile:@"3.png" rect:CGRectMake(0, 0, 256, 256)];

CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY = target.contentSize.height/2;
int maxY = winSize.height - target.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);

[self addChild:target];

// Determine speed of the target
int minDuration = 4.0;
int maxDuration = 12.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2,actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

// Add to targets array
target.tag = 2;
[_targets addObject:target];
}

-(void)gameLogic:(ccTime)dt {

[self addTarget];

}

-(id) init
{

if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {

// Enable touch events
self.isTouchEnabled = YES;

// Initialize arrays
_targets = [[NSMutableArray alloc] init];
_projectiles = [[NSMutableArray alloc] init];
// Get the dimensions of the window for calculation purposes

CGSize winSize = [[CCDirector sharedDirector] winSize];

[self schedule:@selector(gameLogic:)
interval:1.0];
[self schedule:@selector(update:)];

}
return self;
}

- (void)update:(ccTime)dt {

NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
for (CCSprite *projectile in _projectiles) {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);

NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = CGRectMake(target.position.x - (target.contentSize.width/2),
target.position.y - (target.contentSize.height/2),
target.contentSize.width,
target.contentSize.height);

if (CGRectIntersectsRect(projectileRect, targetRect)) {
[targetsToDelete addObject:target];
}
}

for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
_projectilesDestroyed++;
if (_projectilesDestroyed > 30) {
//GameOverScene *gameOverScene = [GameOverScene node];
//  [gameOverScene.layer.label setString:@"You Win!"];
//  [[CCDirector sharedDirector] replaceScene:gameOverScene];
}
}

if (targetsToDelete.count > 0) {
[projectilesToDelete addObject:projectile];
}
[targetsToDelete release];
}

for (CCSprite *projectile in projectilesToDelete) {
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}
[projectilesToDelete release];

}

© Game Development or respective owner

Related posts about cocos2d-iphone