我正在通过一本书中的Space Invaders示例(ActionScript 3.0设计模式,O'Reilly),我已经很好地弄清楚了,除非我现在看到了
internal/space-invaders/trunk/ship/ShipFactory.as, Line 11 1180: Call to a possibly undefined method drawShip.
internal/space-invaders/trunk/ship/ShipFactory.as, Line 12 1180: Call to a possibly undefined method setPosition.
internal/space-invaders/trunk/ship/ShipFactory.as, Line 14 1180: Call to a possibly undefined method initShip.
我并不是最模糊的想法。范围界定?遗产不好?包装可见性?我误解了AS3的多态性规则吗?这里(按顺序)是基类,子类和工厂类:
Ship.as
中的基类:
package ship {
import flash.display.Sprite;
class Ship extends Sprite {
function setPosition(x:int, y:int):void {
this.x = x;
this.y = y;
}
function drawShip( ):void { }
function initShip( ):void { }
}
}
HumanShip.as
中的子课程:
package ship {
import weapon.HumanWeapon;
import flash.events.MouseEvent;
class HumanShip extends Ship {
private var weapon:HumanWeapon;
override function drawShip( ):void {
graphics.beginFill(0x00ff00);
graphics.drawRect(-5, -15, 10, 10);
graphics.drawRect(-12, -5, 24, 10);
graphics.drawRect(-20, 5, 40, 10);
graphics.endFill();
}
override function initShip( ):void {
weapon = new HumanWeapon();
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.moveShip);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.fire);
}
protected function moveShip(event:MouseEvent):void {
trace('MOVE');
this.x = event.stageX;
event.updateAfterEvent();
}
protected function fire(event:MouseEvent):void {
trace('FIRE');
weapon.fire(HumanWeapon.MISSILE, this.stage, this.x, this.y - 25);
event.updateAfterEvent();
}
}
}
ShipFactory.as
中的工厂类:
package ship {
import flash.display.Stage;
public class ShipFactory {
public static const HUMAN:uint = 0;
public static const ALIEN:uint = 1;
public function produce(type:uint, target:Stage, x:int, y:int):void {
var ship:Ship = this.createShip(type);
ship.drawShip();
ship.setPosition(x, y);
target.addChild(ship);
ship.initShip();
}
private function createShip(type:uint):Ship {
switch (type) {
case HUMAN: return new HumanShip();
case ALIEN: return new AlienShip();
default:
throw new Error('Invalid ship type in ShipFactory::createShip()');
return null;
}
}
}
}
答案 0 :(得分:1)
唯一突然出现的是你的基础“Ship”类中的方法没有访问修饰符。尝试明确地公开它们!如果未指定访问修饰符,我不确定AS3默认为什么,它们可能被视为受保护。