2014-08-31 69 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方法执行操作。如果你正在运行这个动作,那么当你说你不能让这些动作工作的时候,就是最具体的。