I am animating 3 images (sprites) from off screen to the right into the middle of the screen. I have written the code below, but was wondering if there was a more efficient way of coding it.
-(void) displayMenu
{
CGSize screenSize = [[CCDirector sharedDirector] winSizeInPixels];
CCSprite* menuItemBottle = [CCSprite spriteWithFile:@"btn_i_have_norm.png"];
menuItemBottle.position = ccp((screenSize.width * 2), 600.0f);
menuItemBottle.contentSize = screenSize;
menuItemBottle.scale = 1;
[self addChild:menuItemBottle z:0 tag:4];
CCSprite* menuItemAdvert = [CCSprite spriteWithFile:@"btn_see-ad_norm.png"];
menuItemAdvert.position = ccp((screenSize.width * 2), 500.0f);
menuItemAdvert.contentSize = screenSize;
menuItemAdvert.scale = 1;
[self addChild:menuItemAdvert z:0 tag:5];
CCSprite* menuItemNoBottle = [CCSprite spriteWithFile:@"btn_dont-have_norm.png"];
menuItemNoBottle.position = ccp((screenSize.width * 2), 400.0f);
menuItemNoBottle.contentSize = screenSize;
menuItemNoBottle.scale = 1;
[self addChild:menuItemNoBottle z:0 tag:6];
CGPoint newPointBottle = CGPointMake(screenSize.width * 0.5f + 50, 600.0f);
CCMoveTo *moveBottle = [CCMoveTo actionWithDuration:0.7f position:newPointBottle];
CCSequence *sequenceBottle = [CCSequence actions:moveBottle, nil];
[menuItemBottle runAction:sequenceBottle];
CGPoint newPointAdvert = CGPointMake(screenSize.width * 0.5f + 50, 500.0f);
CCMoveTo *moveAdvert = [CCMoveTo actionWithDuration:0.7f position:newPointAdvert];
CCSequence *sequenceAdvert = [CCSequence actions:moveAdvert, nil];
[menuItemAdvert runAction:sequenc开发者_JAVA百科eAdvert];
CGPoint newPointNoBottle = CGPointMake(screenSize.width * 0.5f + 50, 400.0f);
CCMoveTo *moveNoBottle = [CCMoveTo actionWithDuration:0.7f position:newPointNoBottle];
CCSequence *sequenceNoBottle = [CCSequence actions:moveNoBottle, nil];
[menuItemNoBottle runAction:sequenceNoBottle];
}
You should be able to get all of the actions down to a single line. The first one would end up looking like this:
[menuItemBottle runAction:[CCMoveTo actionWithDuration:0.7f position:ccp(screenSize.width * 0.5f + 50, 600.0f)]];
If you'd prefer to define the position outside of the action then you could continue to do that. Just FYI a CCSequence is for when you want to run actions on the same object consecutively (i.e. a move action runs and when it's done a rotate action runs).
Also I don't believe you need to set the scale to 1 for each of the images as I believe it is already set to that.
精彩评论