Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 “原因”;“可能未定义的方法”;_Actionscript 3_Inheritance_Factory Pattern - Fatal编程技术网

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

Actionscript 3 “原因”;“可能未定义的方法”;,actionscript-3,inheritance,factory-pattern,Actionscript 3,Inheritance,Factory Pattern,我正在阅读一本书中的一个太空入侵者的例子(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:

我正在阅读一本书中的一个太空入侵者的例子(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 { }

    }

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

    }

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

    }

}
HumanShip.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 { }

    }

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

    }

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

    }

}
ShipFactory中的工厂类。如

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

    }

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

    }

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

    }

}

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

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

据我所知,默认修饰符是
内部
,这是我之前让它们设置的。我将再次尝试
public
,看看这是否会改变行为。这是负面的。将显式访问修饰符添加到
Ship
方法(以及
HumanShip
中的覆盖)没有任何作用。但多亏了Adobe版本的IntelliSense,我才得以使用变量名。显然,AS3不喜欢将类方法中的临时对象命名为与类出现在其中的包相同的名称。无论如何都要接受-谢谢你的帮助!据我所知,默认的修饰符是
internal
,这是我之前设置的。我将再次尝试
public
,看看这是否会改变行为。这是负面的。将显式访问修饰符添加到
Ship
方法(以及
HumanShip
中的覆盖)没有任何作用。但多亏了Adobe版本的IntelliSense,我才得以使用变量名。显然,AS3不喜欢将类方法中的临时对象命名为与类出现在其中的包相同的名称。无论如何都要接受-谢谢你的帮助!