Hi there I'm new to cocos2d and programming in general and I'm making my first game.
I'm having a big problem of getting animations up and running how i want, I can get an animation working, but just not how i need it to run.
I have a block of code that currently is spawning one CCSprite multiple times(code is below) and moving it and i want to swap out the sprite for an animation.
How do i get the Animation to run instead of the Sprite?
here is the code if it helps,
-(void) initBirds {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCSprite* tempBird = [CCSprite spriteWithFile:@"ball3.png"];
float imageHeight = [tempBird texture].contentSize.height;
int numBirds = (screenSize.height / imageHeight);
NSAssert(birds == nil, @"%@: Birds array is already initialized!", NSStringFromSelector(_cmd));
birds = [[CCArray alloc] initWithCapacity:numBirds];
for (int i = 0; i < numBirds; i++) {
CCSprite* bird = [CCSprite spriteWithSpriteFrameName:@"bird0.png"];
[self addChild:bird z:1 tag:2];
[birds addObject:bird];
}
[self resetBirds];
}
-(void) resetBirds {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CCSprite* tempBird = [CCSprite spriteWithFile:@"ball3.png"];
CGSize imageSize = [tempBird texture].contentSize;
int numBirds = [birds count];
for (int i = 0; i < numBirds; i++)
{
CCSprite* bird = [birds objectAtIndex:i];
bird.position = CGPointMake(screenSize.width -imageSize.width, imageSize.height * i + imageSize.height * 0.5f + 34);
[bird stopAllActions];
}
[self unschedule:@selector(birdsUpdate:)];
[self schedule:@selector(birdsUpdate:) interval:1.5f];
numBirdsMoved = 0;
birdsMoveDuration = 4.0f;
birdSpawn = 1.8f;
}
-(void) birdsUpdate:(ccTime)delta
{
for (int i = 0; i < 10; i++) {
int randomBirdsIndex = CCRANDOM_0_1() * [birds count];
CCSprite* bird = [birds objectAtIndex:randomBirdsIndex];
if ([bird numberOfRunningActions] == 0) {
[self runBirdsMoveSequence:bird];
break;
}
}
}
-(void) runBirdsMoveSequence:(CCSprite*)bird {
numBirdsMoved++;
if (numBirdsMoved % 5 == 0 && birdsMoveDuration > 1.0f) {
birdsMoveDuration -= 0.1f;
CCLOG(@"CallSpeed");
}
[self unschedule:@selector(birdsUpdate:)];
numBirdsMoved++;
if (numBirdsMoved % 20 == 0 && birdSpawn > 0.4f) {
birdSpawn -= 0.1f;
CCLOG(@"CallSpawn");
}
[self schedule:@selector(birdsUpdate:) interval:birdSpawn];
CGPoint leftScreenPosition = CGPointMake(-[bird texture].contentSize.height, bird.position.y);
CCMoveTo* move = [CCMoveTo actionWithDuration:birdsMoveDuration position:leftScreenPosition];
CCCallFuncN* call = [CCCallFuncN actionWithTarget:self selector:@selector(birdLeftScreen:)];
CCSequence* sequence = [CCSequence actions:move, call, nil];
[bird runAction:sequence];
}
-(void) birdLeftScreen:(id)sender {
NSAssert([sender isKindOfClass:[CCSprite class]], @"sender is not of class CCSprite!");
CCSprite* bird = (CCSprite*)sender;
CGPoint pos = bird.position;
CGSize screenSize = [[CCDirector sharedDirector] winSize];
pos.x = screenSize.width + [bird texture].contentSize.width;
bird.position = pos;
}
Please Help!