PDA

View Full Version : Move one increment at a time.


buzza_gts
11-26-2003, 10:50 PM
I am currently trying to build a simple little game.

This is the first problem that I have come across and am seeking some help.

I want to have a character that will move only once per keystroke.

This is how I move the character:

onClipEvent (enterFrame){
if(Key.isDown(Key.UP)){
this._x +=10;
}
}

When this code is used the character keeps moving if you hold the key down. I would like it so if the key is held down the character will only move 10 pixels and when released and pressed again it will move anouther 10 pixels.

Hope this makes some sense.
Any help appriciated,
Buzza :D

Colin Campbell
11-26-2003, 11:50 PM
//this might do the job:
onClipEvent (enterFrame){
if(Key.isDown(Key.UP)){
this._x =this._x+10;
}
}

buzza_gts
11-27-2003, 12:23 AM
Nah no good mate.

Still keeps moving the character when key is held down.

I tried also keyDown as the onClipEvent but that not work either.

CyanBlue
11-27-2003, 12:30 AM
What about keyUp??? ;)

buzza_gts
11-27-2003, 12:36 AM
Using keyUP the character doesn't move at all :p

I thought this was nice and simple question but its been doing my head in for about a week now.

If I can rephrase the question:

Any method that could be used to move a character only once on keypress will make me a happy man indeed, doesn't have to be based on the code that I was using.

Thanks again.

CyanBlue
11-27-2003, 12:55 AM
Um... This might not be the best solution, but here is some dirty code that will do the job... ;)onClipEvent (keyDown)
{
if (Key.isDown(Key.RIGHT))
{
direction = "Right";
}
else if (Key.isDown(Key.LEFT))
{
direction = "Left";
}
}

onClipEvent (keyUp)
{
if (direction == "Right")
{
this._x = this._x + 10;
}
else if (direction == "Left")
{
this._x = this._x - 10;
}
direction = "";
}