-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point1 = [touch previousLocationInView:[touch view]];
CGPoint point2 = [touch locationInView:[touch view]];
point1 = [[CCDirector sharedDirector]convertToGL:point1];
point2 = [[CCDirector sharedDirector]convertToGL:point2];
pre = point1;
curr = point2;
CCLOG(@"the pre location is %@",NSStringFromCGPoint(pre));
CCLOG(@"the curr location is %@",NSStringFromCGPoint(curr));
}
上面的代码有什么问题? pre和curr变量gve相同的输出?
答案 0 :(得分:1)
当触摸开始时,由于触摸刚刚开始,显然没有先前的触摸。所以previousLocation和location都是一样的。
如果您在ccTouchesMoved中运行此代码,它将按预期工作。
答案 1 :(得分:0)
您只需要在ccTouchesEnded方法中打印当前位置。它会给你两点之间的差异。
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
所以你必须在ccTouchesBegan方法中获得previousLoaction
。