简体   繁体   English

SKAction scaleXTo:-1

[英]SKAction scaleXTo:-1

I am trying to mirror my SKSpriteNode using SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0]; 我正在尝试使用SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0];来镜像我的SKSpriteNode SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0]; but every time I do, the sprite's physics body seems to get messed up and it drops off the screen. 但是每次我这样做时,子画面的物理物体似乎都被弄乱了,并从屏幕上掉了下来。 I have several objects (floors) beneath the sprite and it falls through all of them as if they are not there. 我在精灵下面有几个对象(地板),它掉落在所有对象之间,好像它们不在那里一样。

This only happens when I mirror the sprite. 这仅在我镜像精灵时发生。 Anyone know how to fix this? 有人知道怎么修这个东西吗?

Update: Reseting the physics body as one answer suggested unfortunately did not fix the problem. 更新:不幸的是,建议将物理物体重置为一个答案不能解决问题。 It appears that only the contact part of the physics body malfunctions. 看来只有物理物体的接触部分会发生故障。 Gravity still has an effect as the little guy drops like a rock. 小家伙像石头一样掉落,重力仍然有作用。

I also tried to reset by again self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width, self.size.height)]; 我也尝试过再次通过self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width, self.size.height)];进行重置self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width, self.size.height)];

right after the self.xScale = -1; self.xScale = -1; but this also did not fix the issue. 但这也不能解决问题。

将可镜像节点添加为某些常规SKNode的子节点,然后在此父节点(而不是负缩放节点)上设置物理主体。

I don't know how to fix this but I would like to suggest one way you could create a mirrored sprite. 我不知道如何解决此问题,但我想提出一种创建镜像子画面的方法。

To achieve this set the x or y scale of your sprite node to -1. 为了达到这个目的,请将您的精灵节点的x或y比例设置为-1。 Then use the SKView method textureFromNode: to create a texture from this mirrored node. 然后使用SKView方法textureFromNode:从此镜像节点创建纹理。

You can then use this texture to create a new sprite node that is mirrored but doesn't require any negative scaling. 然后,您可以使用此纹理创建一个新的sprite节点,该节点已镜像但不需要任何负缩放。

不要使用SKAction,只需将其直接设置为SKSpriteNode的xScale属性即可。

self.yourSprite.xScale = -1;

Roecrew is right about setting the xScale property directly. Roecrew关于直接设置xScale属性是正确的。 I would suggest you try this: 我建议您尝试一下:

node.xScale = -1;
node.physicsBody = node.physicsBody;

You will need to 'reset' the physicsBody each time you change the xScale property. 每次更改xScale属性时,您将需要“重置” PhysicsBody。

The xScale issue with physicsBody is a bug in SpriteKit, but I was able to 'retain' the physicsBody using the second line. PhysicsBody的xScale问题是SpriteKit中的一个错误,但是我能够使用第二行“保留” PhysicsBody。

I'm meeting the problem exactly like yours. 我正像您一样遇到问题。 I spent about 2 hours to figure it out. 我花了大约2个小时才弄清楚。

Just init your physicsBody after you scaleX, i dont know why but i had correct this issue by doing this way. 在scaleX之后只是初始化您的PhysicsBody,我不知道为什么,但是我已经通过这种方式纠正了这个问题。

walkRight   = [SKAction sequence:@[resetDirection,[SKAction runBlock:^{
            [self changePhysicsDirectionRight];
        }],[SKAction repeatActionForever: walk]]];
walkLeft   = [SKAction sequence:@[mirrorDirection,[SKAction runBlock:^{
            [self changePhysicsDirectionLeft];
        }],[SKAction repeatActionForever: walk]]];

walkRight and walkLeft is my action when changing direction, and resetDirection and mirrorDirection is exactly the action i used to scaleXTo:1 and scaleXTo:1 walkRight和walkLeft是我更改方向时的动作,resetDirection和mirrorDirection正是我用于scaleXTo:1和scaleXTo:1的动作

So after i scaleXTo i use a method call changePhysicsDirectionRight to re-init my physicBody like 因此,在我scaleXTo之后,我使用了一个名为changePhysicsDirectionRight的方法来重新初始化physicBody

- (void)changePhysicsDirectionRight{
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width,self.size.height)];
    self.physicsBody.categoryBitMask = guyCategory;
    self.physicsBody.contactTestBitMask = 0;
    self.physicsBody.collisionBitMask   = 0;
}

Remember to re-assign all your category and everything like you init before. 记住,之前请重新分配所有类别以及所有类似init的内容。

I hope someone pro at spritekit can tell me the reason why .... 我希望spritekit的专业人士可以告诉我为什么....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM