【问题标题】:casting and parameters to move a sprite cocos2d移动精灵 cocos2d 的铸造和参数
【发布时间】: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


【解决方案1】:

实际上,我认为您的预定方法没有任何需要。你可以实现

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event

方法并将您的精灵旋转逻辑放在那里。

要获得可变的移动速度,您可以使用CCActionEase 子类之一。将您的移动动作包裹在其中之一中,您将看到移动过程中的速度变化。类似的东西

id move_ease_in = [CCEaseIn actionWithAction:yourMoveAction rate:3.0f];
[player runAction: move_ease_in];

你可以看到几个例子here

【讨论】:

  • 谢谢,能否请您展示一些速度部分的示例代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多