problem in array of shooter sprites which contain different colour bubbles
- by prakash s
everyone i am developing bubble shooter game in cocos2d
I have placed shooter array which contain different color bubbles like this
00000000
it is 8 bubbles array if i tap the screen, first bubbles should move for shooting the target .png .And if i again tap the screen again 2nd position bubble should move for shooting the target.png bubbles,how it will possible for me
because i have already created the array of target which contain different color bubbles,
here i write the code :
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];
NSMutableArray * movableSprites = [[NSMutableArray alloc] init];
NSArray *images = [NSArray arrayWithObjects:@"1.png", @"2.png", @"3.png", @"4.png",@"5.png",@"6.png",@"7.png", @"8.png", nil];
for(int i = 0; i < images.count; ++i) {
int index = (arc4random() % 8)+1;
NSString *image = [NSString stringWithFormat:@"%d.png", index];
CCSprite*projectile = [CCSprite spriteWithFile:image];
//CCSprite *projectile = [CCSprite spriteWithFile:@"3.png" rect:CGRectMake(0, 0,256,256)];
[self addChild:projectile];
[movableSprites addObject:projectile];
float offsetFraction = ((float)(i+1))/(images.count+1);
//projectile.position = ccp(20, winSize.height/2);
//projectile.position = ccp(18,0 );
//projectile.position = ccp(350*offsetFraction, 20.0f);
projectile.position = ccp(10/offsetFraction, 20.0f);
// projectile.position = ccp(projectile.position.x,projectile.position.y);
// Determine offset of location to projectile
int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// Ok to add now - we've double checked position
//[self addChild:projectile];
// Determine where we wish to shoot the projectile to
int realX = winSize.width + (projectile.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
// Add to projectiles array
projectile.tag = 1;
[_projectiles addObject:projectile];
}
}