我是一名学生正在使用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 :(得分: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);
}
您还可以执行类似的操作,例如将变量存储在您想要返回的帧中,这样您就可以使用单个重试帧,该帧可以转到您刚才所处的任何级别。
答案 1 :(得分: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()
应该将游戏重置回其原始状态。以这种方式工作(当你对游戏进行编码时)要比直到最后都要容易,并试图确保你把所有东西都覆盖掉,这是不太可能的。