“可能未定义的方法”的原因

发布于 2024-11-04 20:06:28 字数 2891 浏览 0 评论 0原文

我正在研究一本书(ActionScript 3.0 设计模式,O'Reilly)中的 Space Invaders 示例,并且我已经很好地弄清楚了事情,只是现在我看到了

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

    }

}

Ship.as 中的工厂类>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;
            }
        }

    }

}

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;
            }
        }

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2024-11-11 20:06:28

唯一让我惊讶的是你的“Ship”基类中的方法没有访问修饰符。尝试明确地将它们公开!我不确定 AS3 默认值是什么,如果未指定访问修饰符,它们可能会被视为受保护。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文