Sprite Kit中的SKActions

时间:2014-08-12 20:30:02

标签: objective-c sprite-kit skaction

我正在制作一个sprite kit游戏,我试图添加SKActions,但我已经按照我找到的每个指南进行操作,但没有任何工作。这是包含动作的代码,现在动作应该在游戏开始时播放一次,在发生任何事情之前有两秒钟的延迟,所以我不会错过看动画。

SKAction* jumpAction;
SKAction* kneelAction;
SKAction* runAction;

-(id)initWithSize:(CGSize)size{
    if (self = [super initWithSize:size]) {
        [self setup];

        [self performSelector:@selector(spawnCloud) withObject:nil afterDelay:2.0];
        //[self performSelector:@selector(setupCharacter) withObject:nil afterDelay:4.0];
        [self setupCharacter];
        [self setupActions];
        [self createDpad];
        [self spawnStartupClouds];
        //self.physicsWorld.gravity = CGVectorMake(0.2,-2);
        self.physicsWorld.gravity = CGVectorMake(0.2 ,-2);
        self.physicsWorld.contactDelegate = self;

    }
    return self;
}

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

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

    SKAction* jumpAtlasAnimation = [SKAction animateWithTextures:jumpAtlas timePerFrame:0.1];
    SKAction* wait = [SKAction waitForDuration:2.5];

    jumpAction = [SKAction sequence:@[wait, jumpAtlasAnimation,]];
    NSLog(@"setupActions");
}

3 个答案:

答案 0 :(得分:0)

您正在尝试运行SKAction但未指定运行它的内容。 " SKAction对象是由场景中的节点执行的动作"。

首先创建您的SKSpriteNode(例如将其命名为myNode),然后在其上运行SKAction。像这样:

SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithImageNamed:@"idle.png"];
// add rest of specs...

然后你可以使用你的代码:

SKTextureAtlas* atlas = [SKTextureAtlas atlasNamed:@"jump"];
SKTexture* jumpTex1 = [atlas textureNamed:@"jump1.png"];
SKTexture* jumpTex2 = [atlas textureNamed:@"jump2.png"];
SKTexture* jumpTex3 = [atlas textureNamed:@"jump3.png"];

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

SKAction* jumpAtlasAnimation = [SKAction animateWithTextures:jumpAtlas timePerFrame:0.1];
SKAction* wait = [SKAction waitForDuration:2.5];

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

[myNode runAction:jumpAction]; // <- now you are running the action on a node

阅读&#34;运行动作&#34;在SKNode Class Reference

答案 1 :(得分:0)

您是否曾为您的场景调用runAction

这可能就是他们不工作的原因。

[self runAction:jumpAction];

答案 2 :(得分:0)

如果我说得对,你试图用很少的图像制作跳跃动画? 我在游戏中通过不使用地图集解决了这个问题。所以试试这个:

-(void)makePlayerJump:(SKSpriteNode *)player
{
    SKTexture *jump1 = [SKTexture textureWithImageNamed:@"jump1.png"];
    SKTexture *jump2 = [SKTexture textureWithImageNamed:@"jump2.png"];
    SKTexture *jump3 = [SKTexture textureWithImageNamed:@"jump3.png"];

    SKAction *wait = [SKAction waitForDuration: 2.5];
    SKAction *jumping = [SKAction animateWithTextures:@[jump1, jump2, jump3] timePerFrame:0.1];

   SKAction *jumpAction = [SKAction sequence:@[wait, jumping]];

   [player runAction:jumpAction];
}

当你想激活跳跃时,请执行以下操作:

[self makePlayerJump:myNode];

[self makePlayerJump:self.myNode];

取决于你如何宣布它。 希望这有效。