2014-08-31 77 views
0

我正在開發Objective-C/Sprite Kit中的項目,無法讓Sprite Kit開始工作,我試過了所有我見過但沒有任何工作的東西。Sprite Kit SKActions未開火

下面是一些代碼:

myscene.h

@property (strong, nonatomic) SKAction *jumpAction; 
@property (strong, nonatomic) SKAction *kneelAction; 
@property (strong, nonatomic) SKAction *runAction; 

myscene.m(INIT瓦特/大小的方法)

[self setupCharacter]; 
[self createDpad]; 
[self spawnStartupClouds]; 
//self.physicsWorld.gravity = CGVectorMake(0.2,-2); 
self.physicsWorld.gravity = CGVectorMake(0.2 ,-2); 
self.physicsWorld.contactDelegate = self; 

[self setupActions]; 

myscene.m(setupActions方法)

-(void) setupActions{ 
    SKTextureAtlas *jumpAtlas = [SKTextureAtlas atlasNamed:@"jump"]; 
    SKTexture *jumpTex1 = [jumpAtlas textureNamed:@"jump1.png"]; 
    SKTexture *jumpTex2 = [jumpAtlas textureNamed:@"jump2.png"]; 
    SKTexture *jumpTex3 = [jumpAtlas textureNamed:@"jump3.png"]; 

    NSArray *jumpAtlasTexture = @[jumpTex1, jumpTex2, jumpTex3]; 

    SKAction* jumpAtlasAnimation = [SKAction animateWithTextures:jumpAtlasTexture timePerFrame:0.1]; 
    SKAction* wait = [SKAction waitForDuration:0.5]; 


    jumpAction = [SKAction sequence:@[jumpAtlasAnimation, wait]]; 

    BBCharacter* leader = (BBCharacter*)[self childNodeWithName:@"character1"]; 

} 


-(void)setupCharacter{ 
    NSLog(@"Setup character"); 
    leader = [BBCharacter node]; 
    leader.position = CGPointMake(100, 230); 
    [self addChild:leader]; 

} 

它似乎(在setupActions),它不能「看到」 SKActionjumpAction ...

+2

嘗試self.jumpAction而不是jumpAction。 – 0x141E 2014-08-31 19:18:23

+0

這是一個很好的視頻,涵蓋了你想要的內容https://www.youtube.com/watch?v=6eAwWDje7ks – DogCoffee 2014-09-01 07:56:19

+0

@ michaelD33我的回答對你有幫助。 – 2015-06-08 11:18:41

回答

1

當你聲明在任何類接口的Objective-C的屬性,你需要訪問它使用self.propertyName實施。您還可以使用_propertyName訪問與該屬性關聯的實例變量。所以,jumpAction財產可以使用self.jumpAction或簡單地_jumpAction訪問。

這是因爲該屬性實際上並不是一個實例變量,而是對類數據的封裝。請看documentation以獲得正確的解釋。

或者,爲了能夠使用它的確切名稱訪問該屬性,可以在實現中明確地合成它。

@implementation MyScene //synthesize properties below this. 

@synthesize jumpAction, kneelAction, runAction; 

這樣做後,您可以直接訪問屬性,就像您已經在代碼中執行的那樣。

A 建議:除非需要由其他類訪問,否則不需要使用SKAction對象的屬性。

+2

沒有必要綜合一個屬性,這是自動完成的。在這種情況下,支持iVar的屬性名稱前綴爲下劃線,例如_jumpAction。 – Pierre 2014-09-01 07:14:49

+0

是的,但是如果你想使用確切的名稱作爲屬性,你將不得不明確地合成它。 – ZeMoon 2014-09-01 07:15:51

+0

正確,我想明確表示不需要@synhtesize。對於我來說,如果你想直接訪問iVar,那麼你的答案就像你需要一個答案一樣。 – Pierre 2014-09-01 07:29:39

1

希望這將幫助你

確定,通過觀察你的代碼,我能夠理解,你有你的jump.atlas文件夾 數量的圖像,你想你的球員動畫那些紋理時玩家跳下。

所以首先加入這一行

[_playerNode runAction: jumpAction]; 

我沒有看到你的這行代碼。

0

您已經設置了所有操作,現在只需告訴節點使用runAction方法執行操作。如果你正在運行這個動作,那麼當你說你不能讓這些動作工作的時候,就是最具體的。