2011-07-19 142 views
0

我將3張圖像(精靈)從屏幕右側移動到屏幕中間。我寫了下面的代碼,但是想知道是否有更高效的代碼編碼方式。CCSprite高效動畫

-(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:sequenceAdvert]; 

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

} 
+0

效率更低,代碼更快,效率更高? – lins314159

+0

更少的代碼,如果運行更快,那麼太棒了! – daidai

回答

0

您應該可以將所有操作都設置爲一行。第一個最終會看起來像這樣:

[menuItemBottle runAction:[CCMoveTo actionWithDuration:0.7f position:ccp(screenSize.width * 0.5f + 50,600.0f)]]];

如果您希望在動作之外定義位置,那麼您可以繼續這樣做。只是FYI,CCSequence用於當您想要連續對同一對象執行操作時(即移動操作運行並且完成旋轉操作時)。

另外我不相信你需要設置爲每個圖像的比例爲1,因爲我相信它已經設置爲。