Hello,
i am looking for a script that checks if space is pressed and if so being able to execute something after that (abvious) This may seam easy but now it is like check if space is pressed and it keeps checkking so like yesyesyesyesyes and executes a lot of times. I just want it to execute only once after it positively checked it for space to be down.
grtz, Snuur
action script
Started by Wazzup, Jul 11 2006 06:29 PM
1 reply to this topic
#1
Posted 11 July 2006 - 06:29 PM
#2
Posted 11 July 2006 - 06:57 PM
build a keyDown listener, and switch it off when the key has been pressed..
Extract from the Flash Help (F1)
as you see they create a case statement and a listener which checks which key has been pressed (down), in your case you just need to add to the case space your actions and at the end of the space case put this line..
Extract from the Flash Help (F1)
Quote
SPACE (Key.SPACE property)
public static SPACE : Number
The key code value for the Spacebar (32).
Availability: ActionScript 1.0; Flash Player 5
Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
switch (Key.getCode()) {
case Key.SPACE :
horn_sound.start();
break;
case Key.LEFT :
car_mc._x -= DISTANCE;
break;
case Key.UP :
car_mc._y -= DISTANCE;
break;
case Key.RIGHT :
car_mc._x += DISTANCE;
break;
case Key.DOWN :
car_mc._y += DISTANCE;
break;
}
};
Key.addListener(keyListener_obj);
public static SPACE : Number
The key code value for the Spacebar (32).
Availability: ActionScript 1.0; Flash Player 5
Example
The following example moves a movie clip called car_mc a constant distance (10) when you press an arrow key. A sound plays when you press the Spacebar. For this example, give a sound in the library a linkage identifier of horn_id.
var DISTANCE:Number = 10;
var horn_sound:Sound = new Sound();
horn_sound.attachSound("horn_id");
var keyListener_obj:Object = new Object();
keyListener_obj.onKeyDown = function() {
switch (Key.getCode()) {
case Key.SPACE :
horn_sound.start();
break;
case Key.LEFT :
car_mc._x -= DISTANCE;
break;
case Key.UP :
car_mc._y -= DISTANCE;
break;
case Key.RIGHT :
car_mc._x += DISTANCE;
break;
case Key.DOWN :
car_mc._y += DISTANCE;
break;
}
};
Key.addListener(keyListener_obj);
as you see they create a case statement and a listener which checks which key has been pressed (down), in your case you just need to add to the case space your actions and at the end of the space case put this line..
Key.removeListener(keyListener);
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
