A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: ENTER_FRAME, why isn't it firing!?

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    11

    ENTER_FRAME, why isn't it firing!?

    Code:
    package 
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	public class Player extends MovieClip 
    	{
    		//Constructs Registration Areas for placing aroud Player
    		public var fReg:FloorReg;
    		public var rReg:RightReg;
    		public var lReg:LeftReg;
    		public var cReg:CeilingReg;
    		//Constructs velocity vars
    		private var yVel:Number=0;
    		private var xVel:Number=0;
    		public const TERMINAL_VELOCITY:Number=-7;
    
    		public function Player() 
    		{
    
    			//This is adding registration areas to the display object
    			var oHeight:Number=this.height;
    			fReg=new FloorReg(0,this.height,this.height/4,this.width);
    			addChild(fReg);
    			rReg=new RightReg(this.width,0,this.height-fReg.height,this.width/4);
    			addChild(rReg);
    			lReg=new LeftReg(this.width/-4,0,this.height-fReg.height,this.width/4);
    			addChild(lReg);
    			cReg=new CeilingReg(0,this.height/-5,oHeight/4,oHeight);
    			addChild(cReg);
    			spawn();
    		}
    		private function spawn():void 
    		{
    			//direct player object to it's spawning animation and initialize the Fall function
    			trace("spawnd");
    			addEventListener(Event.ENTER_FRAME, fall, excecuteVelocity);
    		}
    
    		private function fall(event:Event):void 
    		{
    			if(yVel < TERMINAL_VELOCITY)
    				{
    					yVel--;
    					trace(yVel);
    				}
    		}
    		private function excecuteVelocity(event:Event):void
    		{
    			this.y -= yVel;
    			this.x += xVel;
    		}
    	}
    }
    My first hand with the ENTER_FRAME event. Still pretty confused about event flow... The yVel is not tracing and the Player instance is not falling off the screen like it is supposed to, so I'm not sure if the EventListener is right. I'm starting to really love flashkit and all the merciful help they've bestowed upon me. You guys are GREAT! Thanks in advance!

  2. #2
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Nevermind.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    When in doubt, look at the livedocs. In this case, the documentation of addEventListener.
    http://help.adobe.com/en_US/AS3LCR/F...Listener%28%29

    You should have received a compile time error, because you are passing the wrong arguments to addEventListener. addEventListener takes an event type and a function, and has several optional arguments which we don't care about right now. What you seem to be trying to do is pass multiple functions, expecting all of them to be listeners for the same event. That, obviously, does not work that way. You can set multiple listeners for the same event, but they each need their own addEventListener statement. I would suggest combining the functions, though.

    multiple listeners:
    Code:
    		private function spawn():void 
    		{
    			//direct player object to it's spawning animation and initialize the Fall function
    			trace("spawnd");
    			addEventListener(Event.ENTER_FRAME, fall);
    			addEventListener(Event.ENTER_FRAME, excecuteVelocity);
    		}
    
    		private function fall(event:Event):void 
    		{
    			if(yVel < TERMINAL_VELOCITY)
    				{
    					yVel--;
    					trace(yVel);
    				}
    		}
    		private function excecuteVelocity(event:Event):void
    		{
    			this.y -= yVel;
    			this.x += xVel;
    		}
    combined listener:
    Code:
    		private function spawn():void 
    		{
    			//direct player object to it's spawning animation and initialize the Fall function
    			trace("spawnd");
    			addEventListener(Event.ENTER_FRAME, doFrame);
    		}
    
    		private function doFrame(event:Event):void{
    			fall();
    			executeVelocity();
    		}
    
    		private function fall():void 
    		{
    			if(yVel < TERMINAL_VELOCITY)
    				{
    					yVel--;
    					trace(yVel);
    				}
    		}
    		private function excecuteVelocity():void
    		{
    			this.y -= yVel;
    			this.x += xVel;
    		}
    You could even combine fall and executeVelocity if you wanted.

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    AUGH
    Thank you!
    So if I want more than one function triggered by an event I just add a Listener for each one or devote a function to the listener. This has cleared things up greatly, Flax you da best!

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    and Beathoven you were right, and also quite helpful!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center