Objective-C使用游戏手柄移动玩家

时间:2013-05-23 18:05:33

标签: objective-c touch joypad

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

- (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];
    }
}

这是viewcontroller的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];
}

就像我说的,如果我在没有移动手指的情况下触摸Joypad的区域,玩家只会移动一次。有什么想法让它一直移动直到我松开手指?

1 个答案:

答案 0 :(得分:0)

以下是您正在寻找的D-pad控件的完整教程。 Here是SneakyInput的.zip文件,其中包含模拟和D-Pad代码,用于cocos2d。这些为你完成大部分工作!

:) HTH