objective-ctouchjoypad

Objective-C moving a player with joypad


i currently use a joypad that can move a player when touches began and moved. the problem is: If i touch on the Joypad and keep touching it without moving my finger or release it, how can I make the player keep moving? Here's some code:

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

This is to move the player.

And this when the touch began/moved

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

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

here's the viewcontroller's touchesBegan method:

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

Like I said, if I touch in the Joypad's area without moving the finger the player moves only once. Any ideas to keep it moving until I release the finger?


Solution

  • Here is a full blown tutorial for the D-pad controls you are looking for. Here is the .zip file for SneakyInput which has analog and D-Pad code, for cocos2d. These do most of the work for you!

    :) HTH