0

我目前使用的手柄可以在触摸开始和移动时移动玩家。问题是:如果我触摸手柄并一直触摸它而不移动我的手指或松开它,我怎样才能让播放器继续移动?这是一些代码:

- (void)updateVelocity:(CGPoint)point {


    // Calculate distance and angle from the center. 

    CGRect frame = _player.image.frame;

    float dx = point.x - padCenter.x;
    float dy = point.y - padCenter.y;

    frame.origin.x += dx/10;
    frame.origin.y += dy/10;

    if (!(frame.origin.x < 0 || frame.origin.y < 0 || frame.origin.x > SCREEN_SIZE_X - frame.size.width || frame.origin.y > SCREEN_SIZE_Y - frame.size.height)) //if the players won't exit the screen then move it
    _player.image.frame = frame;
}

这是移动玩家。

当触摸开始/移动时

-(void)touchesBegan :(CGPoint )point{ //same as moved

    if (isPointInCircle(point, padCenter , JOYSTICK_RADIUS)){
        isPressed = YES;
        [self updateVelocity:point];
    }
}

这是视图控制器的 touchesBegan 方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //same as Moved

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];
    [joypad touchesBegan:point];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [joypad touchesEnded];
}

就像我说的,如果我在不移动手指的情况下触摸手柄区域,玩家只会移动一次。在我松开手指之前有什么想法让它继续移动吗?

4

1 回答 1

0

这是您正在寻找的D-pad控件的完整教程。是 SneakyInput 的 .zip 文件,它具有 cocos2d 的模拟和 D-Pad 代码。这些为您完成了大部分工作!

:) HTH

于 2013-05-23T18:21:30.643 回答