PDA

View Full Version : how to make something trigger once on keypress


jagarock
11-27-2009, 03:16 PM
the problem:

the CTRL button in this game is doing two things:
if movieclip is visible, then turn it off.
if movieclip is not visible, then turn it on.

however, if i hold the CTRL button pressed, it will trigger over and over, turning the movieclip on and off on and off on and off and so on.

how should i fix this problem?

files are here:
http://www.avisavis.no/game/as3ball.swf
http://www.avisavis.no/game/as3ball.as
http://www.avisavis.no/game/as3ball.fla


package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.utils.*;
public class as3ball extends MovieClip {
var the_hero: Sprite = new ball();
var the_ring: Sprite = new ring();
var power = 15.7;
var friction = 0.50;
var xspeed = 0;
var yspeed = 0;
var up = false;
var down = false;
var left = false;
var right = false;
var action1 = false;

public function as3ball() {
addChild(the_hero);
the_hero.x = 250;
the_hero.y = 200;
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, key_released);
addEventListener(Event.ENTER_FRAME, render);
}

function key_pressed(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = true;
break;
case Keyboard.DOWN :
down = true;
break;
case Keyboard.LEFT :
left = true;
break;
case Keyboard.RIGHT :
right = true;
break;
case Keyboard.CONTROL :
action1 = true;
break;
}
}
function key_released(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.UP :
up = false;
break;
case Keyboard.DOWN :
down = false;
break;
case Keyboard.LEFT :
left = false;
break;
case Keyboard.RIGHT :
right = false;
break;
case Keyboard.CONTROL :
action1 = false;
break;
}
}
private function render(e:Event):void {
if (up) {
if (the_hero.y >45){ yspeed -= power; ybox.text = String(the_hero.y); }
}
if (down) {
if (the_hero.y <980) { yspeed += power; ybox.text = String(the_hero.y); }
}
if (left) {
if (the_hero.x >45) { xspeed -= power; xbox.text = String(the_hero.x); }
}
if (right) {
if (the_hero.x <1230) { xspeed += power; xbox.text = String(the_hero.x); }
}
the_ring.x = the_hero.x + 15;
the_ring.y = the_hero.y + 35;

if (action1) {
if (this.contains(the_ring) ) { removeChild(the_ring); }
else { addChild(the_ring); }
}
xspeed *= friction;
yspeed *= friction;
the_hero.x += xspeed;
the_hero.y += yspeed;
}
}
}

jagarock
11-27-2009, 05:21 PM
bumping it, made a better description of problem.

Renderer10
11-27-2009, 05:31 PM
Create a boolean variable that stores whether the Ctrl key is down or not. So when you press the Ctrl key, it checks if the variable is false, if so, it's sets it to true and executes the code, but only that one time. However, you will have to reset the variable on the Ctrl key release, so create another event listener, and set the Ctrl key variable to false again. It should look like this:

var ctrl_key:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleaseHandler);
function keyPressHandler(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.CONTROL && !ctrl_key) {
ctrl_key = true;
trace ("Pressed!");
}
}
function keyReleaseHandler(evt:KeyboardEvent):void {
if (evt.keyCode == Keyboard.CONTROL) {
ctrl_key = false;
trace ("Released!");
}
}

-Renderer10

jagarock
11-27-2009, 06:16 PM
ah thanks