A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: problem creating static class that does not need to be instantiated

  1. #1

    problem creating static class that does not need to be instantiated

    I'm trying to set up a static class that does not need to be instantiated but I keep getting smacked with the error:

    Spring.as, Line 26 1119: Access of possibly undefined property onEnterFrame through a reference with static type Class.

    If I have an object I wish to spring I just call
    Spring.springToMouse(movieClipToSpring);

    I'm thinking the fix must be quite simple but can't nail it. Any help would be much appreciated.

    Code:
    package
    {
    	import flash.display.*;	
    	import flash.events.Event;
    	
    	public class Spring extends MovieClip
    	{
    		private static var springController:MovieClip = new MovieClip();
    		private static var springObj:Object;
    		private static var spring:Number = 0.1;
    		private var targetX:Number = 0;
    		private var targetY:Number = 0;
    		private var vx:Number = 0;
    		private var vy:Number = 0;
    		private static var friction:Number = 0.95;
    		
    		public function Spring()
    		{
    			
    		}
    		
    		public static function springToMouse(obj:DisplayObject):void
    		{
    			springObj = obj;
    			springController.addEventListener(Event.ENTER_FRAME, Spring.onEnterFrame);
    		}
    		
    		public static function springStop():void
    		{
    			springController.removeEventListener(Event.ENTER_FRAME, Spring.onEnterFrame);
    		}
    		
    		public function onEnterFrame(event:Event):void
    		{
    			var dx:Number = mouseX - springObj.x;
    			var dy:Number = mouseY - springObj.y;
    			var ax:Number = dx * spring;
    			var ay:Number = dy * spring;
    			vx += ax;
    			vy += ay;
    			vx *= friction;
    			vy *= friction;
    			springObj.x += vx;
    			springObj.y += vy;
    		}
    	}
    }
    I do realize I can alternately apply the sprining like so
    var objToSpring:Spring = new Spring(this);
    objToSpring.springToMouse();
    and use the code below, but I'd still like to know how to get it working, plus I'm not sure which way is better performance wise if either.

    Code:
    package
    {
    	import flash.display.*;	
    	import flash.events.Event;
    	
    	public class Spring extends MovieClip
    	{
    		
    		private static var springObj:Object;
    		private static var spring:Number = 0.1;
    		private var targetX:Number = 0;
    		private var targetY:Number = 0;
    		private var vx:Number = 0;
    		private var vy:Number = 0;
    		private static var friction:Number = 0.95;
    		
    		public function Spring(obj:DisplayObject)
    		{
    			springObj = obj;
    		}
    		
    		public function springToMouse():void
    		{
    			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    		}
    				
    		public function springStop():void
    		{
    			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    		}
    		
    		public function onEnterFrame(event:Event):void
    		{
    			var dx:Number = mouseX - springObj.x;
    			var dy:Number = mouseY - springObj.y;
    			var ax:Number = dx * spring;
    			var ay:Number = dy * spring;
    			vx += ax;
    			vy += ay;
    			vx *= friction;
    			vy *= friction;
    			springObj.x += vx;
    			springObj.y += vy;
    		}
    	}
    }

    Also, perhaps there is some other piece of code going awry that I can't locate, but the the enterFrame loop eventually gets removed, almost as if the garbage collector snagged it.
    Last edited by sirzooass; 03-19-2011 at 10:48 AM.

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