Question

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

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];

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.

Some example code follows:

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top