iOS Spritekit无法在子类SKSpriteNode上运行SKAction [英] iOS Spritekit Cannot Run SKAction on a Subclassed SKSpriteNode

查看:224
本文介绍了iOS Spritekit无法在子类SKSpriteNode上运行SKAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的问题,我想要一个更概念性的答案而不是代码。

For my question I would like a more conceptual answer rather than a code one.

我有一个使用Spritekit的iOS应用程序。我有一个子类SKSpriteNode的类,以便我可以向节点添加其他属性。我能够成功创建SKSpriteNode并将其显示在我的SKSpriteNode背景上并使用操作将其删除。但是,当我运行任何操作时,例如:

I have a iOS application using Spritekit. I have a class that subclasses a SKSpriteNode, so that I can add additional properties to the node. I am able to successfully create the SKSpriteNode and display it on my SKSpriteNode background and remove it using an action. However, when I run any actions such as:

SKAction * moveAction = [SKAction moveTo:CGPointMake(100, 100)   duration: 1];

它不执行我的子类的操作。但是,如果我将相同的操作应用于另一个SKSpriteNode而不是执行操作。这些是在每5秒运行一次的计时器上创建的。

It does not perform the action for my subclass. However, if I apply this same action towards another SKSpriteNode than it performs the action. These are created on a timer that runs every 5 seconds.

下面是一些示例代码:

MyScene.m:

-(void) timeTest {
     MySubclass * subclassTest = [[MySubclass alloc] initWithImage:@"myImage"];
     SKSpriteNode *test = [SKSpriteNode spriteNodeWithImageNamed:@"myImage"];
     subclassTest.position = CGPointMake(0, 0);
     test.position = CGPointMake(0, 100);

     [self.background addChild:subclassTest];
     [self.background addChild:test];

     SKAction * moveAction = [SKAction moveTo:CGPointMake(300, 300)   duration: 2];

     [subclassTest runAction: moveAction];
     [test runAction: moveAction];
 }

MySubclass.h

#import <SpriteKit/SpriteKit.h>

@interface MySubclass : SKSpriteNode

-(MySubclass *) initWithImage: (NSString *) image

@end

MySubclass.m

@implementation MySubclass

-(MySubclass *) initWithImage: (NSString *) image
{
    self = [MySubclass spriteNodeWithImageNamed:image];

    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
    self.physicsBody.dynamic = NO; // Doesnt move with physics
    self.physicsBody.categoryBitMask = MySubclassCategory; 
    self.physicsBody.contactTestBitMask = otherCategory;
    self.physicsBody.collisionBitMask = 0;

    return self;
}

-(void) runAction:(SKAction *)action
{
    [super runAction:action];
    NSLog(@"Running action!");
}

@end

每当动作运行时,它会记录消息正在运行!,所以我知道它正在使用该方法,只是因为某些原因没有执行操作?

Whenever the action is run, it does log the message "Running action!", so I know it is getting to that method, just not executing the actions for some reason?

请告诉我是否您需要更多代码或信息!
谢谢。

Please let me know if you need any more code or information! Thanks.

推荐答案

我复制了你的代码,并且你的MySubclass对象上的操作运行正常。你试过在模拟器和硬件中运行它吗?动作是否在模拟器中运行? iPhone 5s的64位架构上存在SKAction错误。

I copied your code and the action runs fine on your MySubclass object. Hvae you tried running it in the simulator and hardware? Does the action run in the simulator? There are SKAction bugs on the iPhone 5s' 64-bit architecture.

这篇关于iOS Spritekit无法在子类SKSpriteNode上运行SKAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆