我正在使用SpriteKit来构建一个游戏,我有一架飞机,我想在触摸时移动。这个想法是将平面重新定位到用户触摸的位置。
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
CGPoint moveToLocation = point;
SKAction *planeMove = [SKAction moveTo:moveToLocation duration:0.5];
[_plane runAction:planeMove]; // _plane represents the plane sprite.
NSLog(@"Touch Point: %@ ",NSStringFromCGPoint(point));
NSLog(@"Plane Location: %@", NSStringFromCGPoint(_plane.position));
上面的代码给出了具有不同值的触摸点和平面位置的意外结果。因此,触摸后的平面远离它想要的点。例如,我在 NSLog 中得到的值:
Touch Point: {391, 743}
Location: {393, 672}
将
第二行更改为
CGPoint point = [touch locationInNode:self];