我有一个问题,我似乎无法解决。 我想要根据手指移动在屏幕上移动精灵,但不是让精灵向移动,我想让精灵移动到相同的方式手指在屏幕上移动。Cocos2D雪碧相对运动
示例: 用户将他/她的手指放在屏幕上(任意位置),然后将屏幕上的手指向左拖动200像素,精灵也应向左移动200像素。
我的方法是在开始时存储玩家的位置,然后计算手指的起始位置和当前位置之间的差值,以便将其添加到精灵本身的起始位置。
这里是我的代码
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if([touches count] == 1){
UITouch *touch = [touches anyObject];
CGPoint startPos = [touch locationInView:[touch view]];
startPosition = startPos;
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
if([touches count] == 1){
UITouch *touch = [touches anyObject];
CGPoint touchPos = [touch locationInView:[touch view]];
CGPoint playerPos = player.position;
float yDifference = startPosition.y - touchPos.y;
playerPos.y = playerPos.y - yDifference;
// just to change to GL coordinates instead of the ones used by Cocos2D
// still doesn't work even if I remove this...
CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:playerPos];
[player setPosition:convertedPoint];
}
}
我希望我的问题是清楚的,但如果它不是不要犹豫可能会问的问题或进一步解释。我一直停留在这个相当长的一段:“(
你的代码似乎工作,与一些变化的变化。 完全按照我想要的方式,除了它在Y轴上翻转的事实,似乎“convertToGL”部分是问题的一部分,因为它使得字符从位置跳到另一个位置 – 2012-08-08 14:42:58
与之相反,整个“convertToGL”的东西,你有没有尝试过“touchPos = [player.parent convertTouchToNodeSpace:touch]”?顺便说一句,“startPos”需要使用相同的方法。这样你就可以在同一个坐标空间中进行处理。 – ICanChange 2012-08-08 20:34:29
我已经对所有东西进行了排序,而不是添加我应该减去的差异,完美地工作。 再次感谢您的帮助! – 2012-08-09 01:19:53