【问题标题】:SpriteKit texture change according to sprite x-axis directionSpriteKit 纹理根据精灵 x 轴方向变化
【发布时间】:2014-11-21 19:08:29
【问题描述】:

这似乎是一个很容易解决的问题,但我不能。正如标题所暗示的,我需要我的精灵根据它的前进方向(左或右)更改为不同的纹理。这是我尝试做的:

if (self.sprite.position.x > 0) //also tried "self.sprite.position.x" instead of 0.
{
    [self.sprite setTexture:[SKTexture textureWithImageNamed:@"X"]];
}
if (self.sprite.position.x < 0)
{
    [self.sprite setTexture:[SKTexture textureWithImageNamed:@"XX"]];
}

两者都不起作用(精灵的 x 轴 == 0 -_- 时除外)。

我读过这个:SpriteKit: Change texture based on direction 它没有帮助。

编辑: 对不起,我忘了提到精灵的运动在 2D 空间中是完全随机的。我为此使用 arc4random()。当它上升或下降时,我不需要任何纹理变化。但左右是必不可少的。

我一定是写错了:

if (self.sprite.physicsBody.velocity.dx > 0)
if (self.sprite.physicsBody.velocity.dx < 0)

不适合我。也不是

if (self.sprite.physicsBody.velocity.dx > 5)
if (self.sprite.physicsBody.velocity.dx < -5)

再澄清一下这个精灵的运动:它是在 的帮助下随机运动的

-(void)update:(CFTimeInterval)currentTime

方法。这是这样实现的:

-(void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */

    if (!spritehMoving)
    {   
        CGFloat fX = [self getRandomNumberBetweenMin:-5 andMax:5];
        CGFloat fY = [self getRandomNumberBetweenMin:-5 andMax:7];


        int waitDuration = arc4random() %3;

        SKAction *moveXTo       = [SKAction moveBy:CGVectorMake(fX, fY) duration:1.0];
        SKAction *wait          = [SKAction waitForDuration:waitDuration];
        SKAction *sequence      = [SKAction sequence:@[moveXTo, wait]];
        [self.sprite runAction:sequence];
    }

}

【问题讨论】:

  • 首先,如果 sprite 是“场景的孩子”,x 永远不会小于 0,因为它超出了屏幕边界。二、如何控制精灵的运动?
  • 如果你对该精灵使用物理,请使用 self.sprite.physicsBody.velocity 而不是位置
  • 是什么让你的精灵向左或向右?根据导致精灵移动的逻辑更改图像。行动?当您运行使其执行的操作时更改它。物理?根据速度在didSimulatePhysics 中更改它。直接设置position?选择新位置时更改它。
  • 我有两个问题:1)你是如何移动精灵的?和 2) 见问题 1。也许,您应该发布左右移动精灵的代码。
  • 更新了我的帖子以更详细地了解代码中发生的事情。请看一看。

标签: objective-c ios8 sprite-kit sktexture


【解决方案1】:

您可以检查fX 是大于还是小于0,这意味着x 轴上的“力”是正的(向右移动)或负的(向左移动)。

这是一个修改过的代码,应该可以解决问题-
顺便说一句-我还在运动的开始和结束时设置了SpritehMovingBOOL,否则,即使精灵正在移动,也会调用此方法(这没有什么问题,但看起来达到检查BOOL 值的目的。

if (!spritehMoving)
{   
    spritehMoving = YES;  

    CGFloat fX = [self getRandomNumberBetweenMin:-5 andMax:5];
    CGFloat fY = [self getRandomNumberBetweenMin:-5 andMax:7];


    int waitDuration = arc4random() %3;  

    SKAction *changeTexture;  

    if(fX > 0) { // Sprite will move to the right  
        changeTexture = [SKAction setTexture:[SKTexture textureWithImageNamed:@"RightTexture.png"]];  
    } else if (fX < 0) { // Sprite will move to the left  
        changeTexture = [SKAction setTexture:[SKTexture textureWithImageNamed:@"LeftTexture.png"]];  
    } else {  
        // Here I'm creating an action that doesn't do anything, so in case  
        // fX == 0 the texture won't change direction, and the app won't crash    
        // because of nil action  
        changeTexture = [SKAction runBlock:(dispatch_block_t)^(){}];
    }

    SKAction *moveXTo    = [SKAction moveBy:CGVectorMake(fX, fY) duration:1.0];
    SKAction *wait       = [SKAction waitForDuration:waitDuration];  
    SKAction *completion = [SKAction runBlock:(dispatch_block_t)^(){ SpritehMoving = NO; }];
    SKAction *sequence   = [SKAction sequence:@[changeTexture, moveXTo, wait, completion]];
    [self.sprite runAction:sequence];
}

【讨论】:

  • 谢谢。替换后 changeTexture = [SKAction setTexture:[SKTexture textureWithImageNamed:@"RightTexture.png"]]; with [self.sprite setTexture:[SKTexture textureWithImageNamed:@"whateverDirectionImage"]];一切开始工作:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 2015-09-30
相关资源
最近更新 更多