程序帮助需要在ActionScript

程序帮助需要在ActionScript

本文介绍了程序帮助需要在ActionScript 3游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在AS3上做这个游戏,基本工作。我遇到的麻烦是在游戏开始时出现开始菜单,而当玩家死亡时也会出现。我一直在尝试。可见的代码,但似乎并没有工作。问题:我在游戏中添加了哪些代码,使游戏启动时出现启动按钮,玩家何时死亡。代码:

 包{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //用于ENTER_FRAME事件

public class Main扩展MovieClip {

//常量
const gravity:Number = 1.5; //游戏的引力
const dist_btw_obstacles:Number = 300; //两个障碍物之间的距离
const ob_speed:Number = 8; //障碍物的速度
const jump_force:Number = 15; //强制跳转

//变量
var player:Player = new Player();
var lastob:Obstacle = new Obstacle(); // varible存储障碍数组中的最后一个障碍
var barriers:Array = new Array(); //存储所有障碍物的数组
var yspeed:Number = 0; //代表鸟的垂直速度的变量
var score:Number = 0; //表示分数的变量

public function Main(){
init();


函数init():void {
//初始化所有变量
player = new Player();
lastob = new Obstacle();
障碍=新阵列();
yspeed = 0;
分数= 0;

//将玩家添加到舞台中央
player.x = stage.stageWidth / 2;
player.y = stage.stageHeight / 2;
addChild(player);

//创建3个障碍()
createObstacle();
createObstacle();
createObstacle();

//添加EnterFrame EventListeners(每帧调用一次)和KeyBoard EventListeners
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,key_up);

$ b私钥函数key_up(event:KeyboardEvent){
if(event.keyCode == Keyboard.SPACE){
//如果空格被按下,鸟
yspeed = -jump_force;



函数重启(){
if(contains(player))
removeChild(player); (包括(障碍物[i])&&障碍物[i]!=空)的
(var i:int = 0; i< obstacle.length; ++ i)
removeChild(障碍[i]);
障碍[i] = null;
}
barriers.slice(0);
init();


函数onEnterFrameHandler(event:Event){
//更新播放器
yspeed + = gravity;
player.y + = yspeed;

//如果玩家接触地面,重新启动
if(player.y + player.height / 2> stage.stageHeight){
restart();
}

//不要让小鸟超出屏幕
if(player.y - player.height / 2 player.y = player.height / 2;


//更新障碍
for(var i:int = 0; i< obstacle.length; ++ i){
updateObstacle(i);
}

//显示分数
scoretxt.text = String(分数);
}

//这个函数更新障碍
函数updateObstacle(i:int){
var ob:Obstacle =障碍物[i];

if(ob == null)
return;
ob.x - = ob_speed;

if(ob.x //如果障碍物到达舞台的左侧,则将其位置改变到最后一个障碍物的后面
changeObstacle(OB);
}

//如果这只小鸟遇到障碍物,那么重新启动游戏
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((player.x - player.width / 2> ob.x + ob.width / 2)&&!ob.covered){
++分数;
ob.covered = true;



//这个函数改变障碍物的位置,使它成为最后一个障碍物,并随机化它的y位置
function changeObstacle ob:Obstacle){
ob.x = lastob.x + dist_btw_obstacles;
ob.y = 100 + Math.random()*(stage.stageHeight-200);
lastob = ob;
ob.covered = false;


//这个函数创建一个障碍
函数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);
obstac.push(ob);
lastob = ob;



$ b code

$ b

}

pre



一个盒子(彩色填充但没有轮廓)。转换为 MovieClip 类型。给一个名字,例如:菜单

  • 这将是
    菜单的容器。通过双击添加内容,添加
    graphics&在新的图层上的文字。或者,如果您在其他某些框架上已经有内容,则只需剪切框架,稍后在此新movieClip的时间轴内执行粘贴框架 li>
  • 转换后,它被添加到 Library 部分
    ctrl + L )。在库中找到它(菜单),然后右键单击,然后选择属性。在
    连结区段中,按一下[导出动作]。 班级名称应自动变为菜单。现在只需点击确定即可。



  • 游戏代码中的示例用法(即:添加到屏幕或移除等) p>

      //#加载movieClip ... 
    var Menu_Screen:Menu = new Menu(); //#菜单是库中给出的链接名称

    //#添加到屏幕...
    gameContainer_MC.addChild(Menu_Screen);
    gameCcontainer_MC.removeChild(Menu_Screen);

    //#控制movieClip ...
    Menu_Screen.x = 50;
    gameContainer_MC.Menu_Screen.x = 50;


    I have been making this game in AS3 and the basics work. The thing i am having trouble with is making a 'start' menu appear when the game starts but also when the player dies. I have been trying the .visible code but that didn't seem to work.

    Question: What code do i add to my game that makes the start button appear when the game starts but also when the player dies. Code:

    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;
        }
    
    
    }
    

    }

    Thanks in advance!

    解决方案

    • Draw a Box (with colour fill but no outline). Convert to MovieClip type. Give a name eg: "Menu"
    • It will bea container for your menu. Add content by double-clicking it, to addgraphics & text on new layers. Or if you have already have content on some other frames, then just "cut frames" and later do a "paste frames" inside the timeline of this new movieClip.
    • When you converted, it was added to the Library section(ctrl + L). Find it ("Menu") in the library and right-click then choose "properties". Inlinkage section click "export for actionscript". The Class name should become "Menu" automatically. Now just click "OK".

    Example usage in game code.. (ie: To add to screen or remove etc)

    //# 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游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-25 03:59