1

我正在制作一个 2d 射击游戏,并试图让不同的攻击对某些敌人类型产生不同的影响。我将子弹和敌人放入两个数组中,然后使用 for 循环对子弹和敌人执行 hitTestObject。这一切都有效,但是我在 Bullet 类中有一个公共静态函数,它将它的类型作为字符串返回,而在 Enemy 类内部有一个公共静态函数,它接受一个字符串,然后使用开关来消除敌人受到的伤害。当我运行我的程序时,我不断收到错误 #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);
                }
            }

        }
4

2 回答 2

2

static从此代码中删除

public static function getType():String{
    return TYPE;
}
于 2013-03-07T21:10:43.570 回答
2

注意:您的错误是说没有属性getType()onQuickBullet但您只提供了PowerBullet.

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

PowerBullet.getType();

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

附加说明:看起来您正在使用static一些类成员,其中看起来不像healthand isHit()。我建议您阅读静态名称空间以更好地了解其用途。

于 2013-03-07T21:11:59.453 回答