2012-10-04 32 views
1

我在Flash(AS3)中創建了動態遮擋地形,並且一切都很順利,地形正確放置。但我需要包含碰撞,並且我希望這些塊位於動畫片段(精靈)內,因此我可以測試與地形本身的碰撞。AS3:無法將子索引影片剪輯添加到精靈中

Ps:我不知道是否可以單獨測試每個塊的衝突,因爲我將使用enterframe函數,並且塊生成是動態的。

我面臨的問題是我有一個名爲blockHolder的精靈,但我不能將塊添加到它。

下面的代碼(我簡化,它使我們在級聯所創建的塊,如果你的AddChild他們進入階段直接,如的addChild(clonedSquare)

我收到的錯誤: 類型錯誤:錯誤#1009:無法訪問空對象引用的屬性或方法

var blockHolder:Sprite = new Sprite(); 

var clonedSquare = new square(); 

var lowestPoint:int = 10; 
var highestPoint:int = 20; 
var areaLenght:int = 10; 

function createLvl():void 
{ 
    for (var i:Number = 0; i<(areaLenght); i++) 
    { 
     clonedSquare = new square(); 
     clonedSquare.x = i * clonedSquare.width; 
     //sets the height of the first block 
     if (i == 0) 
     { 
      var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height; 
      clonedSquare.y = firstY; 
      trace("terrain begins " + firstY + " px down"); 
     } 
     else 
     { 
       var previousId:Number = i - 1; 
       clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height; 
     } 
     //sets the entity (block) name based on the iteration 
     clonedSquare.name = "newSquare" + i; 
     //adds the cloned square 
     blockHolder.addChild(clonedSquare); 
    } 
    addChild(blockHolder); 
} 

createLvl(); 
+0

你問爲什麼你得到這個錯誤? – Ronnie

+0

在'blockHolder.addChild(clonedSquare)'之前,如果你跟蹤(blockHolder)'',你會得到什麼? –

回答

0

嗯,我固定的錯誤,我仍然並不清楚你問什麼,我基本上增加。每個塊都是一個數組和引用該塊的方式。您的clonedSquare.y = getChildByName("newSquare"+previousId).y + clonedSquare.height;正在拋出錯誤。此外,您的firstY被放置第一個塊的方式把我的舞臺,所以我只是將它設置爲0作爲firstY

var blockHolder:Sprite = new Sprite(); 
var squares:Array  = []; 
var lowestPoint:int  = 10; 
var highestPoint:int = 20; 
var areaLenght:int  = 10; 

function createLvl():void 
{ 
    for (var i:Number = 0; i<(areaLenght); i++) 
    { 
     var clonedSquare = new square(); 
     clonedSquare.x = i * clonedSquare.width; 
     if (i == 0) 
     { 
      var firstY:Number = Math.ceil(Math.random()*((lowestPoint-highestPoint))+highestPoint)*clonedSquare.height; 
      //clonedSquare.y = firstY; 
      clonedSquare.y = 0; 
      trace("terrain begins " + firstY + " px down"); 
     } 
     else 
     { 
      clonedSquare.y = squares[i - 1].y + clonedSquare.height; 
     } 
     blockHolder.addChild(clonedSquare); 
     squares.push(clonedSquare); 
    } 
    addChild(blockHolder); 
} 
createLvl(); 
+0

感謝很多人,它的工作!首先是乘以計算的平方高度(我使用10px的平方,這就是爲什麼它出現在我的舞臺上) – Fogs

+0

啊,陷阱!是我正在使用的廣場接近100px ..好吧,很酷,很高興它的工作 – Ronnie