ios - 为什么节点在 zRotation 之后按 Y 轴变化

标签 ios objective-c sprite-kit skspritenode anchorpoint

我有一根棍子,触摸时会变得更高,触摸结束后会随着持续时间旋转 90 度。所以我决定不使用这个关节销并且所有的制作都没有物理,但我无法成功地将我的棍子添加到平台上,所以它会旋转 90 度并从另一侧固定到平台上,所以我需要的是图片上:

http://prntscr.com/5hau7p

我的代码在那里,我正在制作平台 anchor ,坚持...

self.p_currentPlatform = [SKShapeNode shapeNodeWithRect:CGRectMake(-100/2, -320/2, 100, 320)];
self.p_currentPlatform.position = CGPointMake(50, 160);
self.p_currentPlatform.strokeColor = [SKColor grayColor];
self.p_currentPlatform.fillColor = [SKColor yellowColor];
self.p_currentPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_p_currentPlatform.frame.size];
self.p_currentPlatform.physicsBody.dynamic = NO;
[self addChild:self.p_currentPlatform atWorldLayer:VZWorldLayerCharacter];


self.anchor = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size:CGSizeMake(1, 1)];
CGPoint pos = CGPointMake(_p_currentPlatform.frame.origin.x + _p_currentPlatform.frame.size.width,
                               _p_currentPlatform.frame.size.height);
self.anchor.position = pos;
self.anchor.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:.1f];
self.anchor.physicsBody.dynamic = NO;
[self addChild:self.anchor atWorldLayer:VZWorldLayerCharacter];


self.p_stick = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(3, 50)];
self.p_stick.position = CGPointMake(_anchor.position.x, _anchor.position.y + _p_stick.frame.size.height/2);
self.p_stick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:
                            CGSizeMake(_p_stick.frame.size.width, _p_stick.frame.size.height)];
self.p_stick.physicsBody.dynamic = NO;
self.p_stick.anchorPoint = CGPointMake(0, 0.5);
[self addChild:self.p_stick atWorldLayer:VZWorldLayerCharacter];

然后在触摸时我创建游戏状态并且更新方法正在进行:

SKAction *resize = [SKAction resizeByWidth:0 height:kVZStickHeight duration:timeSinceLast];
SKAction *move = [SKAction moveByX:0 y:kVZStickMove duration:timeSinceLast];
SKAction *group = [SKAction group:@[resize, move]];
[self.p_stick runAction:group];

然后我进行旋转,但通过“y”,它总是走高。

#define SK_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f)
CGPoint locationConv = [self convertPoint:location toNode:self.anchor]];
[self.p_stick runAction:[SKAction rotateByAngle:(atan2f(locationConv.y, SK_DEGREES_TO_RADIANS(-90)/*locationConv.x*/)) duration:1.0f]];

如何像 joint pin 效果一样固定到平台上?

最佳答案

首先设置 anchor :

self.p_stick.anchorPoint = CGPointMake(0.5, 0);

也可以为所有设置动态属性NO。

你需要使用这个atan2f函数

(atan2f(SK_DEGREES_TO_RADIANS(90), SK_DEGREES_TO_RADIANS(0)))

关于ios - 为什么节点在 zRotation 之后按 Y 轴变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27495347/

相关文章:

ios - NSDictionary 获取重复值

需要有关动态隐藏和显示字段的 iOS UI/UX 建议

iphone - 根据受欢迎程度对 NS(Mutable)Array 进行排序

ios - SpriteKit中滚动小区域内容的策略

iphone - 最大线程限制?

ios - 如何拦截点击 AVPlayerViewController 中的完成按钮?

swift - SpriteKit SKColor 淡入/淡出

swift - 我的 Sprite 不执行我告诉它的操作?

ios - SKNode 子类应该如何与父场景和兄弟节点通信?

ios - 我应该在 iOS 应用程序中的什么位置存储默认字符串列表?