我在做什么:使一个SKSpriteNode跟随一个UIBezierPath,其中端点是用户触摸的位置。SKSpriteNode遵循bezier路径旋转
问题:当用户触摸我时,将位置发送给播放器节点,播放器节点会自行移动它。然而,这只有在玩家节点向上“指向”时才起作用,例如在第一次触摸时。其他的触动会让玩家误入歧途。
图片说明问题:http://i.stack.imgur.com/nleXj.png
我在Photoshop中增加了一些解释的事情所以这里是一个解释:
- 厚>是我在移动SKSpriteNode。
- 紫色正方形是触摸的确切位置,按顺序编号(实际上为调试目的添加了SKSpriteNodes)
- 细箭头是触摸发生时玩家的位置。
我相信这个问题是在玩家节点坐标系和场景坐标系之间转换的地方。
例如:第一次触摸(当玩家zRotation没有改变时)将玩家移动到正确的位置。然而,第二次触摸是在玩家节点坐标系中的左侧,但是它基本上在场景坐标系中左侧,如果触摸与节点一起旋转,如果它被旋转指向上。
的代码:
GameScene.m:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
[self playRiddle];
CGPoint touchPoint = [touch locationInNode:_player];
[_player moveTo:touchPoint];
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[UIColor purpleColor] size:CGSizeMake(10, 10)];
n.position = [self convertPoint:touchPoint fromNode:_player];
[_worldNode addChild:n];
}
}
Player.m:
- (void)moveTo:(CGPoint)point {
_moving = YES;
[self removeActionForKey:@"move"];
CGVector vector = [self convertAngleToVector:[self shipOrientation]];
CGPoint controlPoint = CGPointMake(vector.dx, vector.dy);
UIBezierPath *path = [self createPathWindEndPoint:point controlPoint:controlPoint];
SKAction *move = [SKAction followPath:[path CGPath] asOffset:YES orientToPath:YES speed:15];
SKAction *done = [SKAction runBlock:^{
_moving = NO;
}];
[self runAction:[SKAction sequence:@[move, done]] withKey:@"move"];
}
- (UIBezierPath *)createPathWindEndPoint:(CGPoint)endpoint controlPoint:(CGPoint)cp {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addQuadCurveToPoint:endpoint controlPoint:cp];
//Draw path
SKShapeNode *line = [SKShapeNode node];
line.path = [path CGPath];
[line setStrokeColor:[UIColor darkGrayColor]];
line.position = self.position;
[self.parent addChild:line];
return path;
}
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dx = cos(radians) * 25;
vector.dy = sin(radians) * 25;
return vector;
}
- (CGFloat)shipOrientation {
return self.zRotation + M_PI_2;
}
的_player父节点是被添加到GameScene一个SKNode。 我尝试了很多东西[节点convertPoint:..]但没有成功。会喜欢一些指向正确方向的指针。
谢谢!