如何让精灵节点指向不同的精灵?

时间:2015-04-26 21:29:37

标签: ios objective-c sprite-kit

我想在用户左右滑动时更改精灵。我在堆栈上发现的所有东西都谈到了改变纹理。但是我想彻底改变精灵。

这是我的代码,试图这样做

@implementation GameScene
SKSpriteNode* player;
SKSpriteNode* playerL;
SKSpriteNode* playerR;

- (void) addPlayer
{
    //player right
    playerR = [[SKSpriteNode alloc] initWithImageNamed: @"pummelManTestR.png"];
    playerR.scale = 0.2f;
    playerR.name = playerCategoryName;
    playerR.position = CGPointMake(CGRectGetMidX(self.frame)*1.3f, playerR.frame.size.height *1.4f);

    //player left
    playerL = [[SKSpriteNode alloc] initWithImageNamed: @"pummelManTestL.png"];
    playerL.scale = 0.2f;
    playerL.name = playerCategoryName;
    playerL.position = CGPointMake(CGRectGetMidX(self.frame)*1.3f, playerL.frame.size.height *1.4f);

//set the starting player position
player = playerL;

[self addChild:player];  
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint endPosition = [touch locationInNode:self];

    if (touchStartPoint.x < endPosition.x && player != playerR) {
        // Right swipe
        //change the image for the player to the other side
        player = playerR;
        NSLog(@"right");


    } else if (touchStartPoint.x > endPosition.x && player != playerL){
        // Left swipe
        //change the image for the player to the other side
        player = playerL;

        NSLog(@"left");

    }
}

NSLogs打印正确的信息,但屏幕上的精灵不会改变 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

player.texture = "your image"
player.name = "your name"
....

重新定义元素,使用此解决方案进行测试!好吧!