### Author Topic: Player movement help  (Read 1802 times)

#### UberGeorge

• Member
• Posts: 99
• Karma: +0/-0
##### Player movement help
« on: Mon, Mar 25, 2013 »
Hi,

I'm not sure if the approach I'm taking to player input is correct.

In my game the player uses the left/right cursor keys to move a bucket back and forth to catch falling objects, but I need to delay the movement so that the bucket doesn't just zoom across the screen.

So I set up a timer and only check player input when the timer hits zero. It works but I don't feel comfortable with this approach, could there be a better way to achieve this or is this how it's done?

Thanks for your time.

This is the pertinent code:

Code: [Select]
`timer -= FlxG.elapsed; if(timer <= 0){ if (FlxG.keys.LEFT) { x -= 50; } if (FlxG.keys.RIGHT) { x += 50; } timer = delay; }`

#### Gazp

• Member
• Posts: 13
• Karma: +0/-0
##### Re: Player movement help
« Reply #1 on: Mon, Mar 25, 2013 »
Maybe I am misunderstanding your question, but why don't you just use acceleration and velocity for this kind of movement instead of messing with a timer? Its much MUCH simpler and controllable.

For example:
Code: [Select]
`public function Player(X:Number, Y:Number){  (...)  maxVelocity.x = 400;  drag.x = 2000;override public function update():void{   if (FlxG.keys.LEFT) {     acceleration.x = -300;   }   if (FlxG.keys.RIGHT) {     acceleration.x = 300;   }}`

#### UberGeorge

• Member
• Posts: 99
• Karma: +0/-0
##### Re: Player movement help
« Reply #2 on: Tue, Mar 26, 2013 »

The bucket moves 50 pixels at a time from column to column rather than pixel smooth.

I just need to keep it from moving too fast, hence the timer.

Without the timer, the player press right arrow and the bucket shoots across the screen making it impossible to position the bucket in the specific column to catch the falling object.

My approach works but it feels like a kludge.

#### Gazp

• Member
• Posts: 13
• Karma: +0/-0
##### Re: Player movement help
« Reply #3 on: Wed, Mar 27, 2013 »
Oh, so you need it to move exactly 50px per button press? Out of curiosity, why are you trying to achieve this kind of movement? Are the things to be caught by the bucket falling on specific spaced columns or something like that?

You could try to figure out something with a boolean variable and an If statement thrown in. Lets see:
Code: [Select]
`private var moveIt:Boolean = false;override public function update():void{    if (FlxG.keys.justPressed("RIGHT") && moveIt== false) { x += 50;        moveIt = true;    }    if (FlxG.keys.justReleased("RIGHT")) {        moveIt = false;    }}`
This way you have to keep pressing RIGHT or LEFT to move instead of leaving your finger down, but you won't zoom across the screen, I think.

#### wg/funstorm

• Global Moderator
• Key Contributor
• Posts: 596
• Karma: +0/-0
##### Re: Player movement help
« Reply #4 on: Wed, Mar 27, 2013 »
The bucket moves 50 pixels at a time from column to column rather than pixel smooth.
I just need to keep it from moving too fast, hence the timer.

I think what I would do is create an invisible movement object that moves smoothly, but then snap the player position to 50 pixels. Something like this (pseudo code)...

override create()
{
playerPosition = new FlxObject(spawnX,spawnY);
}

override update()
{
super.update();
if (moveLeft) playerPosition.velocity.x = 200;
else if (moveRight) playerPosition.velocity.x = -200;
}

override postUpdate()
{
super.postUpdate();
player.x = FlxU.floor(playerPosition.x / 50) * 50;
}

#### UberGeorge

• Member
• Posts: 99
• Karma: +0/-0
##### Re: Player movement help
« Reply #5 on: Wed, Mar 27, 2013 »
Quote
Out of curiosity, why are you trying to achieve this kind of movement? Are the things to be caught by the bucket falling on specific spaced columns or something like that?

Yes, the objects fall down evenly spaced columns.

I don't want to move once per button press, that would be easy. I want the player to be able to hold down the button and have the bucket keep moving but in a controlled manner so the player can stop it right in the column he wants it.

That's why I'm using a timer to delay the movement.

Do you guys see a potential problem with my timer approach?

#### Gama11

• Contributor
• Posts: 390
• Karma: +0/-0
##### Re: Player movement help
« Reply #6 on: Thu, Mar 28, 2013 »
Do you guys see a potential problem with my timer approach?

Nope, not really... You already said it's working fine, and that's what counts I guess.

But I suppose if you want smooth movement, where the bucket accelerates and thus moves faster the longer the button is pressed (as opposed to constant velocity, although you could just decrease the value of the delay var the longer the button is pressed), funstorms approach would be a lot simpler. It's not very difficult to set up either - it all depends on what you're going for.

#### UberGeorge

• Member
• Posts: 99
• Karma: +0/-0
##### Re: Player movement help
« Reply #7 on: Thu, Mar 28, 2013 »
Okay then, I guess I'll stick with my timer approach.
Thanks.