我正在使用 Objective-C/Sprite Kit 开发一个项目,但无法让 Sprite Kit Action 起作用,我已经尝试了我所看到的一切,但没有任何效果。
这是一些代码:
myscene.h
@property (strong, nonatomic) SKAction *jumpAction;
@property (strong, nonatomic) SKAction *kneelAction;
@property (strong, nonatomic) SKAction *runAction;
myscene.m(带大小方法的初始化)
[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
中)它无法“看到”SKAction
jumpAction
...
最佳答案
当您在任何类接口(interface)中声明 Objective-C 中的属性时,您需要在实现中使用 self.propertyName
访问它。您还可以使用 _propertyName
访问与该属性关联的实例变量。因此,可以使用 self.jumpAction
或简单地使用 _jumpAction
访问 jumpAction
属性。
这是因为属性实际上不是实例变量,而是对类数据的封装。请看一下 documentation以获得正确的解释。
或者,为了能够使用它的确切名称访问该属性,您可以在实现中显式合成它。
@implementation MyScene //synthesize properties below this.
@synthesize jumpAction, kneelAction, runAction;
这样做之后,您可以直接访问属性,就像您在代码中所做的那样。
一个建议:您不需要为 SKAction 对象使用属性,除非它们需要被其他类访问。
关于ios - Sprite Kit SKActions 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594698/