0

如何将精灵移动到 x 轴上的接触点,增加速度.x。触摸的时间越长速度越快,那么当它进入一定范围的触摸点或用户松开手指时,再放慢速度?

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

干杯,

刘易斯

4

1 回答 1

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;
}
于 2012-08-27T20:51:21.620 回答