ForumsProgramming ForumPress Key Once

21 11273
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
maffegozer
offline
maffegozer
64 posts
Nomad

In AS2:

on (enterFrame) {
xspeed = 0;
yspeed = 0;
}

if (Key.IsDown(Key.RIGHT)) {
xspeed = 10;
}
if (Key.IsDown(Key.LEFT)) {
xspeed = -10;
}
if (Key.IsDown(Key.UP)) {
yspeed = -10;
}
if (Key.IsDown(Key.DOWN)) {
xspeed = 10;
}

_x = _x + xspeed
_y = _x + yspeed



That's it.
If you want it that it goes 1 direction only.

[quote]on (enterFrame) {
xspeed = 0;
yspeed = 0;
}

if(Key.IsDown(Key.RIGHT)){
xspeed = 10;
yspeed = 0;
}
if(Key.IsDown(Key.LEFT)){
xspeed = -10;
yspeed = 0;
}
if(Key.IsDown(Key.UP)){
xspeed = 0;
yspeed = -10;
}
if(Key.IsDown(Key.DOWN)){
xspeed = 0;
yspeed = 10;
}

_x = _x + xspeed;
_y = _y + yspeed;
Oseb
offline
Oseb
29 posts
Nomad

@Maffegozer

its didnt worked, when i pressed a direction its go to that diredtion but didn't stop. Plus i want to add sound to the walk.

JohnGarell
offline
JohnGarell
1,747 posts
Peasant

How do you think you shall do it? Have you done something similar before?

Oseb
offline
Oseb
29 posts
Nomad

I can make character move and those... but now i want that when i press a direction then go only 1 and dont start to walk towards. Weither i hold direction down its only go 1 and play the walk sound once.

xfactor800
offline
xfactor800
24 posts
Nomad

Maybe try changing the power of the walk like this


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


Try that!

Oseb
offline
Oseb
29 posts
Nomad

Sry its now working, when i press a direction its keeps going, and sound starts all over again.

Showing 16-21 of 21