Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2010, 08:38 AM   PM User | #1
Animatect
New to the CF scene

 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Animatect is an unknown quantity at this point
Post undefined Method

Hi, I'm extending a class and then assigning an instance of this class to a variable that has the type of its superclass, when I try to access any method of this instance through the superclass typed variable, I get the Error:

1061: Call to a possibly undefined method Thirsty through a reference with static type BaseGameEntity.

please can anyone tell me what may be the problem, thanks.

I'll post a simplified version of the problem using as few classes as possible:
the error triggering code is in the Main class.

Code:
package
{
	import flash.display.Sprite;
	
	public class Main extends Sprite
	{
		private var miner:Miner;
		public function Main()
		{
		  //create a miner
		  miner = new Miner(0);

//here is an implementation that throws the error:
//----------------------------------------------

		  var a:BaseGameEntity;
		  var b:Miner = new Miner(0);

			a = b;
			
			trace (a.Thirsty());
//------------------------------------------
		
		  //simply run the miner through a few Update calls
		  for (var i:uint=0; i<20; ++i)
		  { 
			miner.Update();
		  }
		}
	}
}
Code:
package
{
	import BaseGameEntity;
	
	public class Miner extends BaseGameEntity
	{
		//the amount of gold a miner must have before he feels comfortable
		public const ThirstLevel:int = 5;
		
  		//the higher the value, the thirstier the miner
		private var m_iThirst:uint;
	
		
		public function Miner(id:uint):void
		{
			super(id);
			init();
		}
		
		private function init():void
		{	
			//BaseGameEntity(id);
			m_iThirst = 0;
		}

		public function Thirsty():Boolean
		{
			if (m_iThirst >= ThirstLevel)
			{
				return true;
			}
			
			return false;
		}

		public function Update():void
		{
			m_iThirst += 1;		  
		}
	}
}
Code:
package
{
	import flash.display.Sprite;
	
	public class  BaseGameEntity extends Sprite
	{
		//every entity must have a unique identifying number
		private var m_ID:uint;
		//this is the next valid ID. Each time a BaseGameEntity is instantiated
		//this value is updated. 
		//Esta variable es static porque queremos que el next valid ID se actualize en todas las instancias.
		private static var m_iNextValidID:uint = 0;
		
		public function  BaseGameEntity(id:uint):void
		{			
			setID(id);
		}
		
		private function setID(val:uint):void
		{

		//make sure the val is equal to or greater than the next available ID
			if (val >= m_iNextValidID)
			{
				m_ID = val;
				//esta es una static property asi que se actualiza en cada instancia.
				m_iNextValidID = m_ID + 1;
			}
			else
			{
				m_ID = m_iNextValidID;
				m_iNextValidID = m_ID + 1;
			}
		}
		
		public function IDNum():int
		{
			return m_ID;
		}  
		
		public function ID():String
		{
			if(m_ID == 0)
			{
				return EntityNames.ent_Miner_Bob;
			}
			
			else (m_ID == 1)
			{
				return EntityNames.ent_Elsa;
			}
		}  
	}
}
Thanks in advance for your help, also I cutted the code A LOT, it should be ok, but if it doesn't compile please let me know.
Animatect is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:15 AM.


Advertisement
Log in to turn off these ads.