ForumsProgramming ForumPress Key Once

21 11272
Oseb
offline
Oseb
29 posts
Nomad

Hi!
I try to make a game in AS2, and i want that when i press a direction then my character walks only once and dont walks more. Any idea? Please add your code idea, thanks.

  • 21 Replies
andyhunter
offline
andyhunter
30 posts
Nomad

this should work

class "Your MC" extends MovieClip
{
var speed;
var once;

function onLoad()
{
speed = 10;
once = 0 ;
}
function onEnterFrame()
{
if(Key.isDown(Key.RIGHT)&& once == 0)
{
_x += speed;
once += 1;
}
}
}

Oseb
offline
Oseb
29 posts
Nomad

not works.
dont even move. But i have sound to my character's walk and when i hold one direction the sound starts all over again.

andyhunter
offline
andyhunter
30 posts
Nomad

post what your code looks like.

ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

Are you using AS2 or AS3?

The code is very different between the 2 things.

ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

****. Didn't see your thing at the top, sorry

I don't know anything about AS2

Xinito
offline
Xinito
109 posts
Nomad

Just that your character move?

Oseb
offline
Oseb
29 posts
Nomad

My code:

onClipEvent (load) {
power = 3;
_root.soundFX = new Sound();
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= power;
_root.soundFX.attachSound("walk"
_root.soundFX.start(0, 1);
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
_root.soundFX.attachSound("walk"
_root.soundFX.start(0, 1);
}
if (Key.isDown(Key.UP)) {
_y -= power;
_root.soundFX.attachSound("walk"
_root.soundFX.start(0, 1);
}
if (Key.isDown(Key.DOWN)) {
_y += power;
_root.soundFX.attachSound("walk"
_root.soundFX.start(0, 1);
}
}

I have problem with the walk and the sound The sound just starting all over again when i hold one of the arrow keys.

andyhunter
offline
andyhunter
30 posts
Nomad

you need to add four more var and set them all to Zero. then you need to add && "varName" == 0) after "if (Key.isDown(Key.RIGHT)" your new if statement should be if (Key.isDown(Key.RIGHT)&& "varName" == 0)

Oseb
offline
Oseb
29 posts
Nomad

Sry i don't know anything about VARIABLES can you show your idea with Key.RIGHT?

andyhunter
offline
andyhunter
30 posts
Nomad

look at my first post.
var once; tells it to use once as a variable.

once = 0; this sets the var once = 0.

if(Key.isDown(Key.RIGHT)&& once == 0)this says to work only if right arrow is down and once = 0.

once += 1; this sets once = 1 so that it won't work to press the right arrow any more until you set once back to 0.

Oseb
offline
Oseb
29 posts
Nomad

I add this code, but don't work. Don't even move.

Oseb
offline
Oseb
29 posts
Nomad

If you just tell me what to do with the sound, because i can do the walking part.

Ps3dude10
offline
Ps3dude10
7 posts
Nomad

I've never done sound before but this tutorial might help.

Oseb
offline
Oseb
29 posts
Nomad

I knew about this tutorial, but i couldn'4 figure it out. Its with another methold, but i can't find the methold i need.

Oseb
offline
Oseb
29 posts
Nomad

Any idea? which methold is what i need?

Showing 1-15 of 21