A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Inventing Gravity :D

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

    Inventing Gravity :D

    Code:
    package  
    {
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Timer;
    	public class Player extends MovieClip
    	{
    		spawn();
    		
    		
    		//Constructs Registration Areas aroud Player
    		public var fReg:FloorReg;
    		public var rReg:RightReg;
    		public var lReg:LeftReg;
    		public var cReg:CeilingReg;
    		private var Time:Timer;
    		public function Player() 
    		{	
                            //This is adding registration areas to the display object and can be ignored
    			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);
                            //threw in a new Timer object, 
    			Time = new Timer(500, 0);
    			
    		}
    		private function spawn():void
    			{
    				//direct player object to it's spawning animation and initialize the Fall function
    				trace("spawnd");
    				Time.start();
    				fall();
    			}
    
    		//Constructs velocity vars
    		private var yVel:Number = 0;
    		private var xVel:Number = 0;
    		public const TERMINAL_VELOCITY:Number = -7;
    		private function fall():void
    			{
    				if (yVel > TERMINAL_VELOCITY)
    					{
    						Time.addEventListener(TimerEvent.TIMER, gravity);
    					}
    			}
    		private function gravity(event:TimerEvent):void
    			{	do
    					{
    						yVel--;
    					}
    				while(yVel > TERMINAL_VELOCITY)
    				Time.removeEventListener(TimerEvent.TIMER, gravity);
    			}
    	}
    	
    }
    I'm trying to simulate gravitational acceleration by constructing a function that will subtract from the Player object's yVelocity every half a second.
    For now, I'm getting 1180:Call to a possibly undefined function spawn
    but I'm sure there are other things wrong with the code, I'm quite new at this whole 'event flow' stuff so that could very well be the issue. Thanks in advance!

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    package {
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Timer;
    	public class Player extends MovieClip {
    
    		//Constructs Registration Areas aroud Player
    		public var fReg:FloorReg;
    		public var rReg:RightReg;
    		public var lReg:LeftReg;
    		public var cReg:CeilingReg;
    		private var Time:Timer;
    		//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 and can be ignored
    			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);
    			//threw in a new Timer object, 
    			Time=new Timer(500,0);
    			spawn();
    
    		}
    		private function spawn():void {
    			//direct player object to it's spawning animation and initialize the Fall function
    			trace("spawnd");
    			Time.start();
    			fall();
    		}
    		private function fall():void {
    			if (yVel>TERMINAL_VELOCITY) {
    				Time.addEventListener(TimerEvent.TIMER, gravity);
    			}
    		}
    		private function gravity(event:TimerEvent):void {
    			do {
    				yVel--;
    			} while (yVel > TERMINAL_VELOCITY);
    			Time.removeEventListener(TimerEvent.TIMER, gravity);
    			trace("done");
    		}
    	}
    }

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    11
    XD
    oh right, kind of just orphaned that mo' fo' in the middle of the class. God that was dumb. Thanks dawsonk! I owe you a cold one!

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