Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 11-27-2009, 03:16 PM   #1
jagarock
Registered User
 
Join Date: Nov 2009
Posts: 13
Default how to make something trigger once on keypress

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

Code:
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;
		}
	}
}

Last edited by jagarock; 11-27-2009 at 05:20 PM.
jagarock is offline   Reply With Quote
Old 11-27-2009, 05:21 PM   #2
jagarock
Registered User
 
Join Date: Nov 2009
Posts: 13
Default

bumping it, made a better description of problem.
jagarock is offline   Reply With Quote
Old 11-27-2009, 05:31 PM   #3
Renderer10
Registered CS5 User
 
Join Date: Nov 2009
Location: Somewhere in the USA
Posts: 196
Default

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:

ActionScript Code:
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
Renderer10 is offline   Reply With Quote
Old 11-27-2009, 06:16 PM   #4
jagarock
Registered User
 
Join Date: Nov 2009
Posts: 13
Default

ah thanks
jagarock is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:04 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.