将精灵移动到触摸点,触摸时间越长,速度越快

时间:2012-08-27 19:21:50

标签: objective-c cocos2d-iphone physics-engine

如何将精灵移动到x轴上的触摸点,增加velocity.x。触摸时间越长,速度越高,然后当它进入触摸点的某个范围或用户松开手指时,再将其减速?

我有一个带有速度值的播放器类设置,它在更新方法中更新,但不确定如何在touches方法中获得所需的行为?

干杯,

路易斯

1 个答案:

答案 0 :(得分:1)

这应该让你进入大球场(实例变量并将其添加为可触摸的将在init中,知道你是否需要该片段):

- (BOOL) ccTouchBegan: (UITouch *) touch
            withEvent: (UIEvent *) event
{
    _touchBeganAt = [self convertTouchToNodeSpace:touch];
    _velocityChangeSpeed = 1;
}


 - (void) ccTouchEnded: (UITouch *) touch
            withEvent: (UIEvent *) event
{
    _velocityChangeSpeed = -1;
}

- (void) update:(ccTime)delta
{
    velocityThreshold = 1; //? You can tune this
    distanceThreshold = 1; //? Same

    _sprite.velocity += _velocityChangeSpeed;

    //So it comes to a complete stop, as opposed to moving backwards
    if(_sprite.velocity < velocityThreshold)
        _velocityChangeSpeed = 0;

    float distanceFromTouchedPoint = ABS(_sprite.position.x - _touchBeganAt.x);
    if(distanceFromTouchedPoint < distanceThreshold)
        _velocity = 0;
}