daihovey
daihovey

Reputation: 3575

Copying CCSprite animation and position randomly

I'm using the code below to animation a leaf and at the same time drop it from the top of the screen to the bottom. I want to have multiple instances of it starting from random positions. Whats the best way to go about it?

    leaf1Sprite = [CCSprite spriteWithFile:@"LeafBrown0000.png"];
    leaf1Sprite.position = ccp(screenSize.width * 0.5f - 40.0f, screenSize.height);
    leaf1Sprite.contentSize = CGSizeMake([leaf1Sprite boundingBox].size.width, [leaf1Sprite boundingBox].size.height);
    [self addChild:leaf1Sprite z:10 tag:30];

CCAnimation *leaf1Ani = [CCAnimation animation];

[leaf1Ani addFrameWithFilename:@"LeafBrown0000.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0001.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0002.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0003.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0004.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0005.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0006.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0007.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0008.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0009.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0010.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0011.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0012.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0013.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0014.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0015.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0016.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0017.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0018.png"];
[leaf1Ani addFrameWithFilename:@"LeafBrown0019.png"];

id leaf1AniANIMATION = [CCAnimate actionWithDuration:1.9f animation:leaf1Ani restoreOriginalFrame:YES];

id repeatLeaf1Animation = [CCRepeatForever actionWithAction:leaf1AniANIMATION];

[leaf1Sprite runAction:repeatLeaf1Animation];

[leaf1Sprite runAction:[CCMoveTo actionWithDuration:5.0f position:ccp(screenSize.width * 0.5f + 10.0f, -100.0f)]];

Upvotes: 0

Views: 267

Answers (2)

daihovey
daihovey

Reputation: 3575

Postion randomly on screen:

float screenWidth = screenSize.width * 0.5f;
randomX = arc4random() % (int)screenWidth;

Upvotes: 2

Graham Perks
Graham Perks

Reputation: 23398

Take a look at the Particle System samples. In particular there's one called "Rain" that's similar to what you're asking for. I've not done animation within a particle system; hopefully that's possible.

Upvotes: 1

Related Questions