从数组中的MovieClip访问函数 - AS3

时间:2013-03-07 20:05:50

标签: actionscript-3 flash

我正在制造一个二维射击游戏,试图让不同的攻击对某些敌人类型产生不同的影响。我将我的子弹和敌人放入两个阵列,然后使用for循环,在子弹和敌人上执行hitTestObject。这一切都有效,但后来我在Bullet类中有一个公共静态函数,它返回它作为String的类型和Enemy类中的公共静态函数,它接受一个String然后使用一个开关去除敌人受到的伤害。当我运行我的程序时,我不断收到错误#1069:在obj.QuickBullet上找不到属性getType,并且没有默认值。

敌人的代码。

package char {

import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;

public class Enemy1 extends MovieClip {

    private var _type:String;
    private static var _health:Number;
    private var _vx:Number;
    private var _vy:Number;
    private var _stage:Stage;

    private static var _instance:Enemy1;

    public function Enemy1() {
        init();
    }

    private function init():void {
        //Vars
        _vx = -10;
        _vy = Math.random()*10;
        _health = 1;
        _stage = Main.getStage();
        _instance = this;

        //Listeners
        addEventListener(Event.ADDED_TO_STAGE, onAdded);
        addEventListener(Event.ENTER_FRAME, enemyLoop);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);

        //Add Enemy
        Main.getInstance().addChild(this);
    }

    //When Added
    private function onAdded(event:Event):void{
        //Set position
        this.x = _stage.stageWidth;
        this.y = Math.random() * _stage.stageHeight;
        //trace("Enemy created");

        dispatchEvent(new Event("enemyCreated", true));
    }

    //Loop
    private function enemyLoop(event:Event):void {
        //Move
        x += _vx;
        y += _vy;

        //Boundaries
        if ( this.y <= 0 + this.height/2){
            this.y = this.height/2;
            _vy *= -1;
        }
        if ( this.y >= _stage.stageHeight - this.width/2){
            this.y = _stage.stageHeight - this.width/2;
            _vy *= -1;
        }

        //Health cheack
        if ( _health <= 0){
            if (this.parent) {
                this.parent.removeChild(this);
            }
            trace(this + " is dead.");
        }
        //Leaves screen
        if (this.x <= -this.width){
            Main.getInstance().removeChild(this);
        }
    }

    public static function isHit(type:String):void{
        switch(type){
            case "power" :
                _health -= 1;
                break;
            case "quick" :
                _health -= 1;
                break;
            case "strong" :
                _health -= 1;
                break;
            default:
                    _health -= 1;
            }
        }

        public static function getEnemy1():Enemy1{
            return _instance;
        }

        //When Removed
        private function onRemoved(event:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAdded);
            removeEventListener(Event.ENTER_FRAME, enemyLoop);
            removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
            //trace("enemy removed");
        }
    }

}

子弹的代码。

package obj {

import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;


public class PowerBullet extends MovieClip {
    //const
    private static const TYPE:String = "power";

    //Varibles
    private var _vx:Number;
    private var _vy:Number;
    private var _startX:Number;
    private var _startY:Number;
    private var _stage:Stage;

    public function PowerBullet(startX:Number, startY:Number) {
        _startX = startX;
        _startY = startY;

        addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

        Main.getInstance().addChild(this);
    }

    //When created
    private function onAddedToStage(event:Event):void{
        //Initialize vars
        this.x = _startX;
        this.y = _startY;
        _vx = 20;
        _vy = 0;
        _stage = Main.getStage();

        dispatchEvent(new Event("bulletCreated", true));

        //Event Handlers
        addEventListener(Event.ENTER_FRAME, bulletLoop);
        addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }

    //Bullet loop
    private function bulletLoop(event:Event):void {
        //Move bullet
        x += _vx;
        y += _vy;

        if (this.x - this.width > _stage.stageWidth)
        {
            parent.removeChild(this);
        }
    }

    public static function getType():String{
        return TYPE;
    }

    //When Removed
    private function onRemoved(event:Event):void {
        removeEventListener(Event.ENTER_FRAME, bulletLoop);
        removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        removeEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
    }
}

}

我正在使用的for循环。

for (var i:int = 0; i < _enemies.length; i++){
for(var j:int = 0; j < _bullets.length; j++){
    if(_bullets[j].hitTestObject(_enemies[i])){
        if (_bullets[j].parent) {
                            _bullets[j].parent.removeChild(_bullets[j]);
                    }

                    _enemies[i].isHit(_bullets[j].getType());

                    _enemies.splice(i, 1);
                    _bullets.splice(j, 1);

                    i--;
                    j--;

                    Main.setScore(10);
                }
            }

        }

2 个答案:

答案 0 :(得分:2)

从此代码中删除static

public static function getType():String{
    return TYPE;
}

答案 1 :(得分:2)

注意:您的错误是getType()上没有属性QuickBullet,但您只提供了PowerBullet的代码。

您已将static命名空间应用于getType()。这意味着您只能通过类本身而不是该类的实例来调用此方法:

PowerBullet.getType();

如果删除static,您将能够像预期的那样在每个实例上访问该方法。

附加说明:对于少数类成员而言,您似乎正在使用static,而healthisHit()似乎不合逻辑。我建议你阅读静态命名空间以更好地理解它的用途。