這裏我簡單的類圖鑄造對象,並使用所有覆蓋方法,無需if語句
if (exit.isPressed()) {
stop();
}
//I want to casting this Sprite depent on their subclass
Sprite temp=map.getPlayer(); //This return Sprite object
//like this:
if(temp instanceof Bisona)
Bison player=(Bison) map.getPlayer();
else
SuperBison player=(SuperBison) map.getPlayer();
if(message==PLAYING){
//I got error on this line
//I know why i got that error. Coz i dont declare player.
//And java don't know what player object exactly? is Bison/SuperBison
if (player.isAlive() && player.getState()==Sprite.STATE_NORMAL) {
float velocityX = 0;
if (moveLeft.isPressed()) {
velocityX-=player.getMaxSpeed();
}
if (moveRight.isPressed()) {
velocityX+=player.getMaxSpeed();
}
if (jump.isPressed()) {
//My aim is to call this line without if that object
player.jump(false);
}
if (attack.isPressed()){
//My aim is to call this line without if that object
player.attack();
}
player.setVelocityX(velocityX);
}
}
else
{
//Set Velocity to zero
player.setVelocityX(0);
}
這是可能做到這一點?
感謝
對不起我的英文不好:d
我不明白問題(或標題)。 – 2011-06-14 17:05:03
你究竟得到什麼錯誤信息? 爲什麼你想投資Bizon/SuperBizon呢? – Sorceror 2011-06-14 17:07:01