开发者

Cause of "possibly undefined method"

I'm working through a Space Invaders example from a book (ActionScript 3.0 Design Patterns, O'Reilly) and I've gotten things pretty well figured out except now I'm seeing

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.

and I haven't the foggiest idea why. Scoping? Bad inheritance? Package visibility? Have I misunderstood AS3's polymorphism rules? Here (in order) are the base class, child class, and factory class:

Base class in 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 { }

    }

}

Child class in 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();
        }

    }

}

Factory class in 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;
            }
        }

    }

}


The only thing that jumps out at me is that the methods in your base "Ship" class don't have access modifiers on them. Try explicitly making them public! I'm not sure what AS3 defaults to if an access modifier is not specified, they may be being treated as protected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜