Actionscript 3 游戏中需要的程序帮助
Posted
技术标签:
【中文标题】Actionscript 3 游戏中需要的程序帮助【英文标题】:Program help needed in Actionscript 3 game 【发布时间】:2016-06-25 08:53:05 【问题描述】:我一直在用 AS3 制作这个游戏,并且基本工作正常。我遇到的问题是在游戏开始时以及玩家死亡时出现“开始”菜单。我一直在尝试 .visible 代码,但这似乎不起作用。
问题:我在我的游戏中添加了哪些代码,以使开始按钮在游戏开始时出现,并且在玩家死亡时出现。代码:
package
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event
public class Main extends MovieClip
//constants
const gravity:Number = 1.5; //gravity of the game
const dist_btw_obstacles:Number = 300; //distance between two obstacles
const ob_speed:Number = 8; //speed of the obstacle
const jump_force:Number = 15; //force with which it jumps
//variables
var player:Player = new Player();
var lastob:Obstacle = new Obstacle(); //varible to store the last obstacle in the obstacle array
var obstacles:Array = new Array(); //an array to store all the obstacles
var yspeed:Number = 0; //A variable representing the vertical speed of the bird
var score:Number = 0; //A variable representing the score
public function Main()
init();
function init():void
//initialize all the variables
player = new Player();
lastob = new Obstacle();
obstacles = new Array();
yspeed = 0;
score = 0;
//add player to center of the stage the stage
player.x = stage.stageWidth/2;
player.y = stage.stageHeight/2;
addChild(player);
//create 3 obstacles ()
createObstacle();
createObstacle();
createObstacle();
//Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
private function key_up(event:KeyboardEvent)
if(event.keyCode == Keyboard.SPACE)
//If space is pressed then make the bird
yspeed = -jump_force;
function restart()
if(contains(player))
removeChild(player);
for(var i:int = 0; i < obstacles.length; ++i)
if(contains(obstacles[i]) && obstacles[i] != null)
removeChild(obstacles[i]);
obstacles[i] = null;
obstacles.slice(0);
init();
function onEnterFrameHandler(event:Event)
//update player
yspeed += gravity;
player.y += yspeed;
//restart if the player touches the ground
if(player.y + player.height/2 > stage.stageHeight)
restart();
//Don't allow the bird to go above the screen
if(player.y - player.height/2 < 0)
player.y = player.height/2;
//update obstacles
for(var i:int = 0;i<obstacles.length;++i)
updateObstacle(i);
//display the score
scoretxt.text = String(score);
//This functions update the obstacle
function updateObstacle(i:int)
var ob:Obstacle = obstacles[i];
if(ob == null)
return;
ob.x -= ob_speed;
if(ob.x < -ob.width)
//if an obstacle reaches left of the stage then change its position to the back of the last obstacle
changeObstacle(ob);
//If the bird hits an obstacle then restart the game
if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
|| ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true))
restart();
//If the bird got through the obstacle without hitting it then increase the score
if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered)
++score;
ob.covered = true;
//This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
function changeObstacle(ob:Obstacle)
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
lastob = ob;
ob.covered = false;
//this function creates an obstacle
function createObstacle()
var ob:Obstacle = new Obstacle();
if(lastob.x == 0)
ob.x = 800;
else
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100+Math.random()*(stage.stageHeight-200);
addChild(ob);
obstacles.push(ob);
lastob = ob;
提前致谢!
【问题讨论】:
您问题中的开始按钮可见性代码在哪里? 这听起来像是家庭作业? 代码中没有关于菜单的内容。 【参考方案1】: 画一个盒子(有颜色填充但没有轮廓)。转换为MovieClip
类型。给一个名称,例如:“菜单”
它将是
菜单的容器。双击添加内容,添加
新图层上的图形和文本。或者,如果您在其他一些帧上已有内容,则只需“剪切帧”,然后在此新影片剪辑的时间轴内执行“粘贴帧”。李>
转换后,它被添加到 库 部分
(ctrl + L
)。在库中找到它(“菜单”)并右键单击,然后选择“属性”。在
链接部分点击“export for actionscript”。 类名应自动变为“菜单”。现在只需点击“确定”即可。
游戏代码中的用法示例..(即:添加到屏幕或删除等)
//# load the movieClip...
var Menu_Screen : Menu = new Menu(); //# Menu is linkage name given in Library
//# Adding to screen...
gameContainer_MC.addChild( Menu_Screen );
gameCcontainer_MC.removeChild( Menu_Screen );
//# Controlling the movieClip...
Menu_Screen.x = 50;
gameContainer_MC.Menu_Screen.x = 50;
【讨论】:
以上是关于Actionscript 3 游戏中需要的程序帮助的主要内容,如果未能解决你的问题,请参考以下文章
将值从 c# 传递到 actionscript 3 和跨域问题
需要帮助使用 Flex/ActionScript 3 创建平滑的翻转或 FishEye 效果
需要帮助修复代码以将文本框中的值转换为 ActionScript 3 中的变量