2015-09-12 60 views
0

我試圖在AS3中製作一個簡單的遊戲,玩家儘可能吃盡可能多的球。我不知道如何編碼得很好,並且即時嘗試在每次吃東西時在舞臺上添加一個新球。 這是我目前在main.as中的代碼。Actionscript 3遊戲:對象重生

private var startX:Number = 512; 
    private var startY:Number = 384; 
    private var speed:Number = 8; 
    var player1; 
    var player2; 
    var player3; 
    var theBall; 
    player1 = new player(50,384, 1); 
    player2 = new player(944,384,2); 
    player3 = new player(488,84,3); 
    stage.addChild(player1); 
    stage.addChild(player2); 
    stage.addChild(player3); 

    if(theBall.hitTestObject(player1) || theBall.hitTestObject(player2) || theBall.hitTestObject(player3)) 
     { 
      //removes the ball from the stage 
      trace("a player has eaten a ball"); 
      stage.removeChild(theBall); 
      //adds new ball 
      //stage.addChild(theBall); 
      //reset x and y 
      startX = Math.random()*speed-speed/2; 
      startY = Math.random()*speed-speed/2; 
     } 

在ball.as ive指定球應該如何隨機移動,從舞臺中心開始,並從牆壁反彈。

沒有錯誤出現,代碼只是不起作用。在吃飯的時候,你如何在舞臺的中心製作新球?我是否會在主要位置或ball.as中聲明此位置?

回答

0

如果你有一個「球」的對象,在路上你有一個「玩家」對象,然後...

 //removes the ball from the stage 
     trace("a player has eaten a ball"); 
     stage.removeChild(theBall); 
     //reset x and y 
     startX = Math.random()*speed-speed/2; 
     startY = Math.random()*speed-speed/2; 
     //adds new ball 
     theBall = new ball(); 
     theBall.x = stage.stageWidth/2; 
     theBall.y = stage.stageHeight/2; 
     stage.addChild(theBall); 

你重用「theBall」變量,因爲你已經刪除了吃來自舞臺的實例。舊的「theBall」實例與stage.removeChild(theBall)一起被拋出,然後您創建一個新的實例並將其添加到舞臺上。

+0

謝謝!我有球對象設置完全相同的方式,所以這完美的工作! :) – 3M3

0

在你的代碼之前,如果BALL沒有被調用併爲你的舞臺做好準備。它可能行不通。

在資料庫中,檢查包含球movie_clip ......而行「AS聯動」旁邊的電影Clipe名下......你應該把它例如「BALL_TEST」

在你的AS3代碼:

你的代碼之前添加此

var ball_bounce:BALL_TEST = new BALL_TEST(); 

那麼你的代碼

如果我沒有正確地理解你的問題...

親切的問候

+0

哦對不起,我沒有提到它,但是我設置了「球」準備好了舞臺。謝謝你的幫助!真的很感激:) – 3M3

+0

沒問題。嘗試更新您的完整代碼。像這樣每個人都可以輕鬆地幫忙保重。將檢查您的代碼。 順便說一句,你有任何錯誤或編譯問題? – Arsenil98

+0

哦真的是對不起,我不知道我錯過了重要的元素,但是謝謝你下次會做:) 我確實有一個錯誤,但它連接到stage.removeChild(theBall); ArgumentError:錯誤#2025:提供的DisplayObject必須是調用者的子項。 \t在flash.display使用::級DisplayObjectContainer/removeChild之() \t在主/ checkForCollision() IM真的不好用動作腳本,但就像我希望它,所以我只是忽略了錯誤啊哈 – 3M3