keyboard

16 posts

Flag Post

im not quite sure whats wrong. I have received no errors just right and left keys not moving the basket. I tried to go it alone with the keyboard and failed.

package  {
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	
	public class MainGame extends MovieClip {
		
		public var army:Array;
		public var gb:GoldBar;
		public var basket:Basket;
		public var gameTimer:Timer;
		public var leftKeyPress:Boolean;
		public var rightKeyPress:Boolean;

		public function MainGame()
		{
			leftKeyPress = false;
			rightKeyPress = false;
			
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			
			army = new Array();
			
			basket = new Basket();
			basket.x = 225;
			basket.y = 100;
			addChild(basket);
			
			gameTimer = new Timer(25);
			gameTimer.addEventListener(TimerEvent.TIMER, onTick);
			gameTimer.start();
			
		}
		
		public function onTick(e:TimerEvent):void
		{
			if( Math.random () < 0.08 )
			{
				
				var randomX:Number = Math.random() * 500;   
				var gb = new GoldBar(randomX , -20);
				army.push(gb);
				addChild (gb);
				
				for each (var gb in Army)
				{
					gb.moveDown();
				}
				
			}
			
				if( rightKeyPress == true)
				{
					basket.Move(1);
				}
				
				if (leftKeyPress == true)
				{
					basket.Move(-1);
				}
		}
		
		function onAddToStage(e:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
		}
		
		public function onKeyPress(e:KeyboardEvent):void
		{
			if(keyboardEvent.keyCode == Keyboard.RIGHT)
			{
				rightKeyPress = true;
			}
			
			if(keyboardEvent.keyCode == Keyboard.LEFT)
			{
				leftKeyPress = true;
			}
		}
		
		public function onKeyRelease(e:KeyboardEvent):void
		{
			if(keyboardEvent.keyCode == Keyboard.RIGHT)
			{
				rightKeyPress = false;
			}
			
			if(keyboardEvent.keyCode == Keyboard.LEFT)
			{
				leftKeyPress = false;
			}
		}

	}
	
}
	package  {

	import flash.display.MovieClip;
	
	public class Basket extends MovieClip {
	
		public function Basket() 
		{
			
        }
		
		public function Move(xMove:Number):void
		{
			var baseSpeed:Number = 3;
			x += (xMove * baseSpeed);
		}
    }
	
}
 
Flag Post

keyboardEvent doesn’t exist, shouldn’t even compile, use e.keyCode instead

 
Flag Post

Basically what Kewry said.

And BTW, you don’t need to write if( rightKeyPress == true), since rightKeyPress is already a boolean, you’ll only need if( rightKeyPress )

 
Flag Post

still not moving it

package  {
	
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	
	public class MainGame extends MovieClip {
		
		public var army:Array;
		public var gb:GoldBar;
		public var basket:Basket;
		public var gameTimer:Timer;
		public var leftKeyPress:Boolean;
		public var rightKeyPress:Boolean;

		public function MainGame()
		{
			leftKeyPress = false;
			rightKeyPress = false;
			
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			
			army = new Array();
			
			basket = new Basket();
			basket.x = 225;
			basket.y = 100;
			addChild(basket);
			
			gameTimer = new Timer(25);
			gameTimer.addEventListener(TimerEvent.TIMER, onTick);
			gameTimer.start();
			
		}
		
		public function onTick(e:TimerEvent):void
		{
			if( Math.random () < 0.08 )
			{
				
				var randomX:Number = Math.random() * 500;   
				var gb = new GoldBar(randomX , -20);
				army.push(gb);
				addChild (gb);
				
				for each (var gb in Army)
				{
					gb.moveDown();
				}
				
			}
			
				if( rightKeyPress)
				{
					basket.Move(1);
				}
				
				if (leftKeyPress)
				{
					basket.Move(-1);
				}
		}
		
		function onAddToStage(e:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
		}
		
		public function onKeyPress(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.RIGHT)
			{
				rightKeyPress = true;
			}
			
			if(e.keyCode == Keyboard.LEFT)
			{
				leftKeyPress = true;
			}
		}
		
		public function onKeyRelease(e:KeyboardEvent):void
		{
			if(e.keyCode == Keyboard.RIGHT)
			{
				rightKeyPress = false;
			}
			
			if(e.keyCode == Keyboard.LEFT)
			{
				leftKeyPress = false;
			}
		}

	}
	
}
package  {
	
	import flash.display.MovieClip;
	
	public class Basket extends MovieClip {
	
		public function Basket() 
		{
			
        }
		
		public function Move(xMove:Number):void
		{
			var baseSpeed:Number = 3;
			x += (xMove * baseSpeed);
		}
    }
	
}
 
Flag Post

Put some traces in there, when and where does it break down? Do the two keyboard listeners fire? Are the keycodes properly detected? Does the Move() function ever get executed?

Inserting trace() statements at various points in your code to figure out if everything is still going according to plan is a basic debugging technique that will serve you well. If you switched to FD it also has some pretty powerful debugging tools integrated that allow you to stop your program at any point and check what the value of you variables is, to stop execution and resume it line-by-line, to choose a line in your source and execute the program until it reaches it. All that’s invaluable as well, but inserting traces is the first, most basic debugging technique, and you need to learn it.

 
Flag Post

It took about 30 seconds to find out that the onKeypress function doesn’t function but im not sure whats wrong with it.

 
Flag Post

OK good, doesn’t work in what way? If you turn it into this:

		public function onKeyPress(e:KeyboardEvent):void
		{
			trace("Entered keypress with key", e.keyCode);
			if(e.keyCode == Keyboard.RIGHT)
			{
				trace("Right key press detected!");
				rightKeyPress = true;
			}
			
			if(e.keyCode == Keyboard.LEFT)
			{
				trace("Left key press detected!");
				leftKeyPress = true;
			}
		}

what traces?

 
Flag Post

nothing

 
Flag Post

Well then the current problem is not in onKeyPress(). At least now you have a partial answer though: your basket doesn’t move because onKeyPress never gets called. The new question is: why?

Put a trace in onAddToStage(), and also insert a removeEventListener there call to cancel out addEventListener(Event.ADDED_TO_STAGE, onAddToStage); You no longer need it once you have added Maingame to the stage.

While we’re at it, make sure you do add MainGame to the display list at some point, it will save us some time in our backward walk.

 
Flag Post

well i tried to put a trace statement into the onTick function and nothing came up. The onTick function works fine in game so i don’t know why their not coming up oh and well odne on your 1000th post

 
Flag Post
Originally posted by AlexanderC4:

well i tried to put a trace statement into the onTick function and nothing came up. The onTick function works fine in game so i don’t know why their not coming up

Are you running in debug mode? Assuming you’re using FlashDevelop, you need to run in debug mode to view traces.

 
Flag Post

no flash pro and yes im in debug mode

 
Flag Post

Hmm, in that case, pretty much what Ace said. It looks to me like for whatever reason onAddToStage isn’t being called.

instead of simply calling onAddToStage, try something like this:

if(stage) onAddToStage();
else addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
 
Flag Post

Ahh wait, onAddToStage needs to be a public function, you have it here as just a function. Try making it public.

 
Flag Post
Originally posted by I_love_you_lots:

Ahh wait, onAddToStage needs to be a public function, you have it here as just a function. Try making it public.

I myself always make such listener function private, and it works fine. I don’t think this is the problem.

 
Flag Post
Originally posted by I_love_you_lots:

Hmm, in that case, pretty much what Ace said. It looks to me like for whatever reason onAddToStage isn’t being called.

instead of simply calling onAddToStage, try something like this:

if(stage) onAddToStage();
else addEventListener(Event.ADDED_TO_STAGE, onAddToStage);

onAddToStage needs to be passed an event, so that first line should definitely generate an error if stage ever becomes anything other than null, but I doubt you were suggesting replacing trace diagnostics (which doesn’t work for some reason(?)) with error generation diagnostics.

Even if you were suggesting voluntarily generating errors as a way to track which parts of the program are accessed, I still think the OP would be better off adding a tracetext:TextField in a corner and writing what he would have traced there (with tracetext.appendText(“\nI’m there or whatever”); rather than reassigning the text variable, so all the traces can be reviewed.