2013-03-07 43 views
1

我正在製作一個2d射手,並試圖讓不同的攻擊對某些敵人類型有不同的效果。我把我的子彈和敵人放到兩個數組中,然後使用for循環,它對子彈和敵人執行hitTestObject。這一切都有效,但是我在Bullet類中有一個公共靜態函數,它將類型返回爲String類型,而Enemy類中的公共靜態函數需要一個String,然後使用一個開關來清除敵人接收到的傷害。當我運行我的程序時,我不斷收到錯誤#1069:在obj.QuickBullet上找不到屬性getType,並且沒有默認值。從數組中的MovieClip訪問函數 - AS3

敵人的代碼。

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

} 

Bullet的代碼。

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

注意:你的錯誤是說有上QuickBullet沒有財產getType()但你只爲PowerBullet提供的代碼。

您有static名稱空間應用於getType()。這意味着你只能通過類本身而不是類的實例調用此方法:

PowerBullet.getType(); 

如果刪除static,你就可以訪問每個實例方法像預期。

附加說明:它看起來像幾個班級成員使用static,它似乎不像healthisHit()邏輯。我建議你閱讀靜態名稱空間以更好地理解它的用途。

2

從這個代碼刪除static

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

非常感謝你,我一定會閱讀靜態名稱空間。 – Cxsquared 2013-03-08 00:05:45