2011-12-30 55 views
1

我是一名使用flash開發項目的學生。我只是試圖在Adobe Flash CS5中使用Actionscript 3製作Flash遊戲。一切工作正常,我可以走路和跳躍,但如果主要角色接觸一個物體並死亡(如坑中的尖刺),我希望遊戲重新開始。我在第1幀有一個主菜單,第2幀有第一個等級。主角被稱爲「player_mc」,殺死他的對象被稱爲「死亡」。非常感謝幫助和提示。如何讓對象重置我的遊戲?

代碼:

//The Code For The Character (Player_mc) 
import flash.events.KeyboardEvent; 
import flash.events.Event; 

var KeyThatIsPressed:uint; 
var rightKeyIsDown:Boolean = false; 
var leftKeyIsDown:Boolean = false; 
var upKeyIsDown:Boolean = false; 
var downKeyIsDown:Boolean = false; 

var playerSpeed:Number = 20; 
var gravity:Number = 1; 
var yVelocity:Number = 0; 
var canJump:Boolean = false; 
var canDoubleJump:Boolean = false; 

stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey); 
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey); 


//This two functions cllapsed is the keybindings that the game uses 
function PressAKey(event:KeyboardEvent):void 
{ 
    if(event.keyCode == Keyboard.D) 
    { 
     rightKeyIsDown = true; 
    } 
    if(event.keyCode == Keyboard.A) 
    { 
     leftKeyIsDown = true; 
    } 
    if(event.keyCode == Keyboard.SPACE) 
    { 
     upKeyIsDown = true; 
    } 
    if(event.keyCode == Keyboard.S) 
    { 
     downKeyIsDown = true; 
    } 
} 

function ReleaseAKey(event:KeyboardEvent):void 
{ 
    if(event.keyCode == Keyboard.D) 
    { 
     rightKeyIsDown = false; 
    } 
    if(event.keyCode == Keyboard.A) 
    { 
     leftKeyIsDown = false; 
    } 
    if(event.keyCode == Keyboard.SPACE) 
    { 
     upKeyIsDown = false; 
    } 
    if(event.keyCode == Keyboard.S) 
    { 
     downKeyIsDown = false; 
    } 
} 

//Player animations 
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer); 

function moveThePlayer(event:Event):void 
{ 
    if(!canJump || rightKeyIsDown && !canJump || leftKeyIsDown && !canJump) 
    { 
     player_mc.gotoAndStop(3); 
    } 
    if(!upKeyIsDown && !rightKeyIsDown && !leftKeyIsDown && canJump) 
    { 
     player_mc.gotoAndStop(1); 
    } 
    if(rightKeyIsDown && canJump || leftKeyIsDown && canJump) 
    { 
     player_mc.gotoAndStop(2); 
    } 
    //Animation stops here 

    if(rightKeyIsDown) 
    { 
     floor_mc.x -= playerSpeed; 
     dies.x -= playerSpeed; 
     player_mc.scaleX = 0.3; 
    } 
    if(leftKeyIsDown) 
    { 
     floor_mc.x += playerSpeed; 
     dies.x += playerSpeed; 
     player_mc.scaleX = -0.3; 
    } 
    if(upKeyIsDown && canJump) 
    { 
     yVelocity = -18; 
     canJump = false; 
     canDoubleJump = true; 
    } 
    if(upKeyIsDown && canDoubleJump && yVelocity > -2) 
    { 
     yVelocity =-13; 
     canDoubleJump = false; 
    } 
    if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown) 
    { 
     player_mc.gotoAndStop(1); 
    } 
    yVelocity +=gravity 

    if(! floor_mc.hitTestPoint(player_mc.x, player_mc.y, true)) 
    { 
     player_mc.y+=yVelocity; 
    } 
    if(yVelocity > 20) 
    { 
     yVelocity =20; 
    } 

    for (var i:int = 0; i<10; i++) 
    { 
     if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true)) 
     { 
      player_mc.y--; 
      yVelocity = 0; 
      canJump = true; 
     } 
    } 
} 
+0

你問如何確定'player_mc'和'dies'之間的衝突或者如何重置遊戲變量等? – Marty 2011-12-30 01:57:14

+0

這是一個helluva lotta代碼。你介意修剪它只有必要的部分?另外,請根據@MartyWallace所說的說明 – 2011-12-30 05:33:27

回答

0

如果我沒有理解這個權利,當你死了,你要重新啓動的水平。要做到這一點,你可以將所有的初始變量保存在數組中,比如玩家的位置,落地位置等,然後在死亡時將這些變量恢復到相應的對象。但是,當你變得更加複雜並且有很多很多的對象時,它可能會變得棘手。

一個更簡單的解決方案是,當你按下重啓按鈕或在一定的時間延遲之後,當你點擊死亡對象時在死亡屏幕上顯示某種死亡屏幕(讓我們稱它爲框架3) ,然後回到第2幀並重新開始關卡。

例子:

在moveThePlayer功能將這個:

if(dies.hitTestPoint(player_mc.x, player_mc.y, true)) 
{ 
    // remove all the event listeners 
    player_mc.removeEventListener(Event.ENTER_FRAME, moveThePlayer); 
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey); 
    stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey); 

    gotoAndStop(3); // this has your "dead" screen on it. 
} 

而且在框架3,有類似:

import flash.events.MouseEvent; 

retry_button.addEventListener(MouseEvent.CLICK, retry); 

function retry(e:MouseEvent) { 
    retry_button.removeEventListener(MouseEvent.CLICK, retry); 

    // back to the level 
    gotoAndStop(2); 
} 

你也可以做類似的存儲變量你想回到哪個框架,所以你有一個單一的重試框架,可以達到你剛纔的任何級別。

+0

是的,謝謝你的幫助!這正是我一直在尋找的,我寫錯了代碼,它讓我感到困惑。謝謝! – JohnD 2012-01-02 22:55:05

0

您應該開發的一個好習慣是有一個endGame()或您在工作時更新的類似方法。

例如:

function endGame():void 
{ 
    // nothing here yet 
} 

現在讓我們說你創建一個播放器。玩家成立初期是這樣的:

player.health = 100; 
player.dead = false; 

你要更新endGame()的方法來反映這一點。

function endGame():void 
{ 
    player.health = 100; 
    player.dead = false; 
} 

得到了一些與遊戲引擎本身相關的變量?

var score:int = 0; 
var lives:int = 3; 

添加那些過於:

function endGame():void 
{ 
    player.health = 100; 
    player.dead = false; 

    score = 0; 
    lives = 3; 
} 

現在endGame()應復位遊戲恢復到原來的狀態。以這種方式工作(比如你編寫遊戲)要容易得多,而不是直到最後纔去做,並且試圖確保你覆蓋所有的東西,這是不太可能的。

+0

我明白你的意思了,謝謝你的反饋 – JohnD 2012-01-02 22:55:48

相關問題