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.