Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

}
share|improve this question
add comment

2 Answers

Assuming that all your bubbles are the same shape and size where the only difference is just their color, you can instead use a white bubble and change the Sprite's color like this:

target.color = ccc3(0, 0, 0);

Where the 0 values should be replaced by RGB values respectively.

Then you can just generate a different color bubble each time, so you can just keep calling addTarget: until you have enough bubbles.

share|improve this answer
    
Thank you Jamornh –  prakash s Oct 9 '12 at 12:06
add comment

You should try an animation using a sprite sheet. Just have each frame of the animation be a different color. Ray Wenderlich, where you got your (base) code from, has a tutorial on it here.

This is a bit more complicated than just changing the color of a sprite (as suggested in Jamornh's answer), but I see it as a good segue into incorporating animations into your games.

share|improve this answer
    
i have created 8 bubbles and all are used in NSMutablearray is it correct way of generating random sprites? –  prakash s Oct 9 '12 at 12:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.