【发布时间】:2012-06-19 17:16:54
【问题描述】:
我不知道如何将 touch started 中的触摸位置传递到我在 touch begin 时运行的方法中。
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[self schedule:@selector(moveSprite:)];
return TRUE;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self unschedule:@selector(moveSprite:)];
}
-(void)moveSprite:(ccTime) delta
{
CGPoint spriteCenter = CGPointMake(player.contentSize.width /2,player.contentSize.height /2);
CGPoint touchPoint; //how to get this touch began?
float distanceX = touchPoint.x - spriteCenter.x;
float distanceY = touchPoint.y - spriteCenter.y;
float angle = atan2f(distanceY,distanceX); // returns angle in radians
player.rotation = angle;
}
我还有一个关于[self schedule:@selector:的问题,这会不断调用我的移动精灵方法吗?因为我希望精灵在按住触摸并且精灵位置发生变化时不断移动并相应地改变旋转。
我的最后一个问题是将精灵移动到触摸的 x 坐标。如果我使用 ccmoveto 我不能使用速度来使精灵慢慢增加它的速度,可以吗?如何将精灵移动到触摸点以增加速度?我猜它与delta有关。
【问题讨论】:
-
self schedule是做什么的?直接调用moveSprite:方法,将UITouch 事件传递给它,然后调用它的locationInView:方法呢? -
我认为它的 cocos2d 相当于 NSTimer,但我不确定它是否像这样不断调用,或者我需要添加一个间隔。我不确定这是否适用于不断更新位置和旋转? locationInView 部分如何工作?我想我可能还需要 delta 来更新速度?
标签: iphone objective-c cocos2d-iphone parameter-passing