更改具有physicsBody的SKSpriteNode的位置 [英] Change position of a SKSpriteNode that has a physicsBody

查看:60
本文介绍了更改具有physicsBody的SKSpriteNode的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过更改来改变SKSpriteNode的位置

I can't change the position of a SKSpriteNode by changing

self.position = newPosition;

如果它有物理实体,它就不再有用了。

It doesn't work anymore if it has a physicsBody.

我得到的解决方法是:

    self.myStar.physicsBody = nil;

    [self.myStar changePosition];

    self.myStar.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.myStar.size.width/2];

    [self runAction:[SKAction moveTo:newPosiion duration:0.0]];

但#2不顺畅。它需要在没有移动动作的情况下弹出另一个位置。

But #2 isn't smooth. It needs to pop up on another position without a moving action.

推荐答案

我遇到了同样的问题。显然,一旦拥有PhysicsBody,你就无法显式设置Sprite节点的位置。我通过暂时删除精灵节点的PhysicsBody来解决它。

I had the same problem. Apparently you cannot explicitly set the position of a Sprite node once it has a PhysicsBody. I solved it by temporarily removing the sprite node's PhysicsBody.

CGFloat yPosition = 400.f;
SKPhysicsBody* goldfishPhysicsBody = _goldfish.physicsBody;
_goldfish.physicsBody = nil;
_goldfish.position = CGPointMake(_goldfish.position.x, yPosition);
_goldfish.physicsBody = goldfishPhysicsBody;

这篇关于更改具有physicsBody的SKSpriteNode的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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