【发布时间】:2015-01-05 14:36:47
【问题描述】:
我在将精灵节点旋转到触摸位置时遇到问题
这是我的代码:
#import "THMyScene.h"
#import <math.h>
#define SK_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
#define SK_RADIANS_TO_DEGREES(__ANGLE__) ((__ANGLE__) * 57.29577951f) // PI * 180
@implementation THMyScene
{
CGSize _winSize;
SKSpriteNode *_playerSprite;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:94.0/255.0 green:63.0/255.0 blue:107.0/255.0 alpha:1.0];
_winSize = CGSizeMake(self.size.width, self.size.height);
_playerSprite = [SKSpriteNode spriteNodeWithImageNamed:@"Player"];
_playerSprite.position = CGPointMake(368.0f, 160.0f);
[self addChild:_playerSprite];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
CGPoint spriteLocation = CGPointMake(_playerSprite.position.x, _playerSprite.position.y);
float xDistance;
float yDistance;
xDistance = powf(touchLocation.x - spriteLocation.x,2);
yDistance = powf(touchLocation.y - spriteLocation.y,2);
float angle = atan2f(yDistance,xDistance);
_playerSprite.zRotation = angle - SK_DEGREES_TO_RADIANS(90);
SKAction *moveAction = [SKAction moveTo:touchLocation duration:2];
[_playerSprite runAction: moveAction];
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end
关于问题可能是什么的任何建议
当我单击节点的右侧或顶部时,它会为我提供正确的方向 但是当我单击节点的左侧或底部时,方向在相反的一侧。
【问题讨论】:
-
从 x 和 y 差异的计算中删除
powf将为您提供正确的角度。
标签: ios sprite-kit