A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Sending a class instance reference to a different class instance

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    4

    Sending a class instance reference to a different class instance

    Hi everyone, I hope the title represents what I'm trying to do here...

    I'm new to Actionscript and Flash in general and I'm trying to learn. I'm creating a little game - The player controls a character that follows the mouse. Pigs run away from the player, and the player has to get them all into a pen. To make the game a little more difficult I'm trying to add an enemy - wolves, that attempt to perform a 'hunt' method every x seconds. This method sends a reference of the wolf instance to Main (my document class) and Main then loops through the pigs on stage to see if there's any nearby. Now as far as I know this works - my problem is I'm unsure how to send the pig instance reference back to the wolf that called the hunt method, so it can then 'target' the pig, and then attempt to pounce on it. I hope this makes sense!


    This is my Wolf class:
    Code:
    package 
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	import Main;
    	import Pig;
    
    	public class Wolf extends MovieClip
    	{
    		
    		private var huntTimer:Timer;
    		private var game:Main;
    		private var target:Object;
    		
    		public function Wolf(main, startX, startY):void
    		{
    			this.x = startX;
    			this.y = startY;
    			game = main;
    			this.addEventListener(Event.ENTER_FRAME, entersFrame);
    			addEventListener(Event.ADDED_TO_STAGE, setUp);
    		}
    		
    		public function setUp(event:Event):void
    		{
    			huntTimer = new Timer(1000);
    			huntTimer.addEventListener(TimerEvent.TIMER, hunt);
    			huntTimer.start();
    		}
    		
    		public function hunt(event:TimerEvent):void
    		{
    			this.game.hunt(this);
    		}
    			
    		public function entersFrame(event:Event):void
    		{
    			//chase target
    		}
    		
    		public function pigtarget(p:Pig):void
    		{
    			this.target = p;
    		}
    	}
    }
    And this is my Main(document) class, I've made green the problem and I'm getting "1119: Access of possibly undefined property w through a reference with static type Main." as an error, which I'm guessing is a silly mistake on my part:
    Code:
    package 
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	import Wolf;
    	import Pig;
    
    	public class Main extends MovieClip
    	{
    		private var _game:Main;
    		protected var _creeps:Array = new Array();
    		protected var _pigs:Array = new Array();
    		var spawnWolf:Timer;
    		var spawnPig:Timer;
    
    		public function Main()
    		{
    			init();
    			stage.addEventListener(Event.ENTER_FRAME, entersFrame);
    		}
    
    		public function init():void
    		{
    			spawn();
    		}
    
    		public function spawn():void
    		{
    			spawnWolf = new Timer(1000);
    			spawnWolf.addEventListener(TimerEvent.TIMER, createWolf);
    			spawnWolf.start();
    			spawnPig = new Timer(2000)
    			spawnPig.addEventListener(TimerEvent.TIMER, createPig);
    			spawnPig.start();
    		}
    
    		public function createWolf(event:TimerEvent):void
    		{
    			var startX:Number = 200;
    			var startY:Number = 200;
    			var w:Wolf = new Wolf(this,startX,startY);
    			this._creeps.push(w);
    			stage.addChild(w);
    			spawnWolf.stop();
    		}
    
    		public function createPig(event:TimerEvent):void
    		{
    			var startX:Number = 300;
    			var startY:Number = 300;
    			var pig:Pig = new Pig(this,startX,startY);
    			this._pigs.push(pig);
    			stage.addChild(pig);
    		}
    
    		private function entersFrame(event:Event):void
    		{
    			for (var i:int = 0; i < _pigs.length; i++)
    			{
    				for (var j:int = 0; j < _creeps.length; j++)
    				{
    					var dx:Number = (_pigs[i]).x - (_creeps[j]).x;
    					var dy:Number = (_pigs[i]).y - (_creeps[j]).y;
    					var distance:Number = Math.sqrt(dx * dx + dy * dy);
    					if (distance <= 150)
    					{
    						//trace(("wolf" + j) + (" is close to pig" + i));//make pig run away from wolf
    					}
    				}
    			}
    		}
    
    		public function hunt(w:Wolf):void
    		{
    			try
    			{
    				//script working here...hunt function is running...
    				var p:Pig = this.obtainTarget(w.x, w.y, 100);
    				//pig IS being returned...but how do we send the wolf that called this function a reference of the pig...
    				this.w.pigTarget(p);//<----- isn't working
    			}
    			
    			catch (e:Error)
    			{
    			}
    
    		}
    
    		public function obtainTarget(posX :int, posY :int, radius :int):Pig
    		{
    			targetLoop:for (var i :int = 0; i < this._pigs.length; i++)
    			{
    				// Calculate distance to creep
    				var diffX:int = Math.abs(posX - this._pigs[i].x);
    				var diffY:int = Math.abs(posY - this._pigs[i].y);
    				var dist :int = Math.round(Math.sqrt((diffX * diffX) + (diffY * diffY)));
    				//script is looking through the pig array, working from the supplied wolf
    
    				if (dist <= radius)
    				{
    					trace("distance to pig" + [i] + ": " + dist)
    					// return this creep
    					return this._pigs[i];
    				}
    
    				else
    				{
    					//nothing in range
    				}
    			}
    			throw new Error('No targets available');
    		}
    	}
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Remove "this." from "this.w.pigTarget(p)".

    w is not a property of the Main instance, it is a parameter of the function.

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by 5TonsOfFlax View Post
    Remove "this." from "this.w.pigTarget(p)".

    w is not a property of the Main instance, it is a parameter of the function.
    Hey I appreciate the quick reply, I've removed the "this." from the "w.pigTarget(p)" method but it's still giving the same error, and as I'm new to this I'm not quite sure how to solve it. Any help or advice is greatly appreciated.
    Last edited by Nugusta; 04-14-2011 at 12:03 PM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Run in debug mode and get a line number. That particular instance of the error should be gone, but you may have others.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Main.as, line 82, which is:

    Code:
    w.pigTarget(p);
    I can run trace statements to show that the rest of the code is working up untill trying to send the Pig reference back to the instance of Wolf, which called the hunt method to begin with.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That doesn't make sense. You're sure the error message is the same?

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    1061: Call to a possibly undefined method pigTarget through a reference with static type Wolf.

    My bad, was a different error!

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ah. Well that makes sense. Your method in Wolf is pigtarget, not pigTarget.

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