我目前正在努力开发一款适合大学作业的游戏。我正在使用flash和actionscript 3这样做(我使用的是旧版本的flash,这是一个2015版本,当它仍被称为Adobe Flash时)。在第一帧中放置代码时,播放器对象移动工作正常,但是当我将它全部放在第二帧中时,这不起作用。为什么?代码完全一样!而且我认为如果我从第2帧开始玩游戏会更方便(至少在第一级保持第一级)。在第1帧,我想要我的主菜单。所以,这是我目前在第1帧的代码:
import flash.display.MovieClip;
import flash.events.Event;
stop();
var optionsActive:Boolean = false;
var gamePaused:Boolean = false;
startGame();
function startGame():void
{
if (optionsActive == false)
{
optionsUI.visible = false;
}
else
{
optionsUI.visible = true;
}
menuTextOptions_active.addEventListener(MouseEvent.CLICK, activateMenuOptions);
menuTextNewgame_active.addEventListener(MouseEvent.CLICK, activateStartNewGame);
}
function activateStartNewGame(event:Event)
{
gotoAndStop(2);
}
function activateMenuOptions(event:Event)
{
optionsActive = true;
optionsUI.visible = true;
if (this.currentFrame == 1)
{
if (menuTextContinue_active.visible != false)
{
menuTextContinue_active.visible = false;
}
if (menuTextNewgame_active.visible != false)
{
menuTextNewgame_active.visible = false;
}
if (menuTextOptions_active.visible != false)
{
menuTextOptions_active.visible = false;
}
if (menuTextExitgame_active.visible != false)
{
menuTextExitgame_active.visible = false;
}
if (menuButton_continue.visible != false)
{
menuButton_continue.visible = false;
}
if (menuButton_newgame.visible != false)
{
menuButton_newgame.visible = false;
}
if (menuButton_options.visible != false)
{
menuButton_options.visible = false;
}
if (menuButton_exitgame.visible != false)
{
menuButton_exitgame.visible = false;
}
}
}
这是我目前在第2帧的代码:
import flash.display.MovieClip;
import flash.events.Event;
stop();
var keybaordLeft:int = 37;
var keyboardUp:int = 38;
var keyboardRight:int = 39;
var keyboardDown:int = 40;
var playerXSpeed:int = 4;
var playerYSpeed:int = 4;
var leftDown:Boolean = false;
var upDown:Boolean = false;
var rightDown:Boolean = false;
var downDown:Boolean = false;
optionsActive = false;
gamePaused = false;
startLevel();
function startLevel():void
{
if (optionsActive == false)
{
optionsUI.visible = false;
}
else
{
optionsUI.visible = true;
}
stage.addEventListener(MouseEvent.CLICK, activateMenuOptions);
mc_playerShip.addEventListener(Event.ENTER_FRAME, movePlayer);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
}
function checkKeysDown(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftDown=true;
}
if (event.keyCode == 38)
{
upDown=true;
}
if (event.keyCode == 39)
{
rightDown=true;
}
if (event.keyCode == 40)
{
downDown=true;
}
}
function checkKeysUp(event:KeyboardEvent):void
{
if (event.keyCode == 37)
{
leftDown=false;
}
if (event.keyCode == 38)
{
upDown=false;
}
if (event.keyCode == 39)
{
rightDown=false;
}
if (event.keyCode == 40)
{
downDown=false;
}
}
function movePlayer(event:Event):void
{
if (leftDown)
{
mc_playerShip.x -= playerXSpeed;
}
if (upDown)
{
mc_playerShip.y -= playerYSpeed;
}
if (rightDown)
{
mc_playerShip.x += playerXSpeed;
}
if (downDown)
{
mc_playerShip.y += playerYSpeed;
}
}
角色根本不动。为什么会这样,我该如何解决?
答案 0 :(得分:0)
这不是您问题的答案,但可能仍然有用。
if (this.currentFrame == 1)
{
if (menuTextContinue_active.visible != false)
{
menuTextContinue_active.visible = false;
}
if (menuTextNewgame_active.visible != false)
{
menuTextNewgame_active.visible = false;
}
if (menuTextOptions_active.visible != false)
{
menuTextOptions_active.visible = false;
}
if (menuTextExitgame_active.visible != false)
{
menuTextExitgame_active.visible = false;
}
if (menuButton_continue.visible != false)
{
menuButton_continue.visible = false;
}
if (menuButton_newgame.visible != false)
{
menuButton_newgame.visible = false;
}
if (menuButton_options.visible != false)
{
menuButton_options.visible = false;
}
if (menuButton_exitgame.visible != false)
{
menuButton_exitgame.visible = false;
}
}
可以全部替换为:
If (this.currentFrame == 1){
if (uiElements.visible != false){
uiElements.visible = false;
}
}
如果您只是将所有这些按钮和影片剪辑放入一个名为uiElements
的父容器中。
编辑11-12-16
首先确保导入flash.display.*
要创建容器,请在任何函数外执行此操作:
var uiElements:Sprite = new Sprite(); // this creates an instance of the Sprite class
(Sprite与影片剪辑相同,但它没有自己的时间轴,所以内存较少)
然后在你的主构造函数中执行:
addChild(uiElements); // this adds the Sprite to the stage
然后,无论您在哪里创建要添加到该容器的元素,请执行以下操作:
uiElements.addChild(yourNewMcHere); //this adds your other movie clip to the container
这有意义吗?
这就是编程方式。如果要在IDE中创建元素,只需创建一个空的MovieClip并将所有其他电影嘴唇添加到其中即可(例如选择一个MovieClip,ctrl + x进行剪切,双击要使用的影片剪辑)作为容器,ctrl + v粘贴,重复你要添加到这个容器的所有影片剪辑。这与我向你展示的以编程方式做的事情是一样的。