2014-02-11 72 views
0

我對動作相對比較陌生,我試圖選擇一個隨機選擇的障礙物來飛向我的玩家角色。我不斷收到錯誤1151:在命名空間內部與定義obstInstance存在衝突。我得到了同樣的錯誤三次。我認爲這是因爲我反覆分配相同的障礙實例新值(即使它在條件格式內)。任何幫助將不勝感激!ActionScript錯誤1151:隨機選擇障礙

this.addEventListener(Event.ENTER_FRAME, obstacleGenerator); 
function obstacleGenerator(e:Event):void 
{ 
trace("Obstacle Generator Initiated"); 
var obstacleSelector:uint=Math.ceil(Math.random()*4); 
trace("Obstacle Number ", obstacleSelector); 

if (obstacleSelector==1){ 
    var obstInstance:Obj1 = new Obj1(); 
} 
else if (obstacleSelector==2){ 
    var obstInstance:Obj2 = new Obj2(); //error 1151 
} 
else if (obstacleSelector==3){ 
    var obstInstance:Obj3 = new Obj3(); //error 1151 
} 
else if (obstacleSelector==4){ 
    var obstInstance:Obj4 = new Obj4(); //error 1151 
} 
} 

回答

1

您試圖給4個不同的對象具有相同的名稱。每個需要一個單獨的名稱。然後,您可以創建一個變量來保存currentObject並將其用作參考。

嘗試

this.addEventListener(Event.ENTER_FRAME, obstacleGenerator); 

var currentObj:Object; 

var obstInstance1:Obj1; 
var obstInstance2:Obj2; 
var obstInstance3:Obj3; 
var obstInstance4:Obj4; 

function obstacleGenerator(e:Event):void 
{ 

    var obstacleSelector:uint=Math.ceil(Math.random()*4); 


    if (obstacleSelector==1){ 
     setCurrentObj(obstInstance1 = new Obj1()) 
    } 
    else if (obstacleSelector==2){ 
     setCurrentObj(obstInstance2 = new Obj2()) 
    } 
    else if (obstacleSelector==3){ 
     setCurrentObj(obstInstance3 = new Obj3()) 
    } 
    else if (obstacleSelector==4){ 
     setCurrentObj(obstInstance4 = new Obj4()) 
    } 
} 

function setCurrentObj(myObject:Object):void{ 
    currentObj = myObject; 
}