Be a Supporter!

KeyEvent problems

  • 245 Views
  • 1 Reply
New Topic Respond to this Topic
SpeedUP
SpeedUP
  • Member since: May. 21, 2009
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
KeyEvent problems 2009-09-30 10:37:50 Reply

I dont understand whats wrong with it. Help please kind people as it to make correctly.

Main class

package{
	import flash.display.*
	import flash.text.*
	import flash.events.*
	import flash.net.URLRequest;
	
	public class Os extends MovieClip{
		static public var main;
		
		public var poi:Sprite;
		public function Os(){
			main = this;
			
		}
	}
}

Object Class:

package{
	import flash.display.*
	import flash.events.*
	import flash.ui.*
	import flash.filters.*
	import flash.utils.Timer;
    import flash.events.TimerEvent;
	
	public class Poi extends MovieClip{
		static public var main;
		public var up:Boolean = false;
		public var v:Number = 5;
		public function Poi(){
			main = this;
			Os.main.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyP);
			Os.main.stage.addEventListener(KeyboardEvent.KEY_UP,keyR);
			
			addEventListener(Event.ENTER_FRAME,enterF);
		}
		function enterF(e:Event):void{
			if(up){
				this.x += v;
			      }  
		}
		function keyP(e:KeyboardEvent):void{
			if ( e.keyCode == Keyboard.UP )
	        {
		    up = true;
	        }
		}
		function keyR(e:KeyboardEvent):void{
			if ( e.keyCode == Keyboard.UP )
	        {
		    up = false;
	        }
		}
	}
}
bcapecci
bcapecci
  • Member since: Mar. 7, 2008
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to KeyEvent problems 2009-09-30 16:13:14 Reply

At 9/30/09 10:37 AM, SpeedUP wrote: I dont understand whats wrong with it. Help please kind people as it to make correctly.

Main class

package{
import flash.display.*
import flash.text.*
import flash.events.*
import flash.net.URLRequest;

public class Os extends MovieClip{
static public var main;

public var poi:Sprite;
public function Os(){
main = this;

}
}
}

Object Class:

package{
import flash.display.*
import flash.events.*
import flash.ui.*
import flash.filters.*
import flash.utils.Timer;
import flash.events.TimerEvent;

public class Poi extends MovieClip{
static public var main;
public var up:Boolean = false;
public var v:Number = 5;
public function Poi(){
main = this;
Os.main.stage.addEventListener(KeyboardE vent.KEY_DOWN,keyP);
Os.main.stage.addEventListener(KeyboardE vent.KEY_UP,keyR);

addEventListener(Event.ENTER_FRAME,enter F);
}
function enterF(e:Event):void{
if(up){
this.x += v;
}
}
function keyP(e:KeyboardEvent):void{
if ( e.keyCode == Keyboard.UP )
{
up = true;
}
}
function keyR(e:KeyboardEvent):void{
if ( e.keyCode == Keyboard.UP )
{
up = false;
}
}
}
}

I dont understand what the fuck OS is supposed to be doing. Thus I don't know what your trying to accomplish. It's strange coding, that's for sure.

public var stageReference:Stage;

public function CONSTRUCTOR(stageReference:Stage) : void
{
stageReference.addEventListener(KeyboardEvent.KEY_DOWN,keyP);
stageReference.addEventListener(KeyboardEvent.KEY_UP,keyR);
addEventListener(Event.ENTER_FRAME,enterF);
}

All I did was make a reference to the stage and passed it into the function. Using a document class you wouldn't need to do such but I'm assuming this is any class. All the event listeners are added to the stage and the enter frame the object itself. Trash that first class, it's useless to you.

Also and I mean this with no disrespect: Please stop taking shortcuts and making your code impossible to read. This means don't import every goddamn class and write out keyboardPressed not keyP. Illegibility is fine if you don't make mistakes...

Feel free to write again if this wasn't fixed or you have other problems.