2014-02-13 160 views
1

我有以下代碼來創建動畫紋理:雪碧套件創建動畫對象

-(void) setUpActions { 
    SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:@"player"]; 

    // Running player animation 
    SKTexture * runTexture1 = [atlas textureNamed:@"player1.png"]; 
    SKTexture * runTexture2 = [atlas textureNamed:@"player2.png"]; 
    SKTexture * runTexture3 = [atlas textureNamed:@"player3.png"]; 
    SKTexture * runTexture4 = [atlas textureNamed:@"player4.png"]; 
    SKTexture * runTexture5 = [atlas textureNamed:@"player5.png"]; 
    SKTexture * runTexture6 = [atlas textureNamed:@"player6.png"]; 
    SKTexture * runTexture7 = [atlas textureNamed:@"player7.png"]; 
    SKTexture * runTexture8 = [atlas textureNamed:@"player8.png"]; 

    NSArray * runTexture = @[runTexture1, runTexture2, runTexture3, runTexture4, runTexture5, runTexture6, runTexture7, runTexture8]; 

    runAnimation = [SKAction animateWithTextures:runTexture timePerFrame:0.06 resize:YES restore:NO]; 

    SKSpriteNode * playerNode = (SKSpriteNode *)[self childNodeWithName:@"player"]; 
    [playerNode runAction:[SKAction repeatActionForever:runAnimation]]; 
} 

,這將創建精靈節點,並添加到另一種方法場景:

-(SKSpriteNode *)createPlayer { 
    SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"player"]; 
    player.position = CGPointMake(100, 160); 
    player.name = @"player"; 
    player.zPosition = 100; 

    return player; 
} 

做就這樣,我不得不創建一個具有靜態圖像的對象,並用動畫紋理覆蓋它。如何創建一個沒有默認靜態圖像的動畫對象?

回答

1

的動畫動作只能在實際的實例化SKSpriteNode對象上執行的,但是,你可以創建一個「空白」節點,然後在其上運行的動畫動作:

SKSpriteNode *player = [SKSpriteNode node]; // instead of the static player sprite 

或者,如果你想空節點有一個特定的大小,使其響應觸摸/在特定區域內的碰撞它的動畫之前,例如,嘗試一個明確的彩色矩形:

SKSpriteNode *player = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:(CGSizeHere)]; 

或者你要使用的第一動畫的框架作爲精靈的紋理:

SKTexture *temp = runTexture[0]; 
SKSpriteNode *player = [SKSpriteNode spriteNodeWithTexture:temp]; 
+0

很好用!非常感謝! – user2255273