每次我在动作脚本3中通过屏幕上的游戏重置我的游戏时角色变得更快

时间:2013-12-17 18:13:14

标签: actionscript-3 game-physics

我的游戏以一个开始帧开始。点击游戏按钮。如果我击中一个物体,我会通过屏幕进入游戏。每当我点击屏幕上的游戏开始按钮时,我的角色控制变得更快。我没有为速度设置任何变量,只是“mc_guy.x + = 3”或.y等。如果我不得不说速度快多了,我想说速度加倍。

   import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;


//prevent game loop
stop();

// event listeners for movement
mc_Guy.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownGuy);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpGuy);


// this array holds references to all the keys
var keys:Array = [];



//initiate default values
fuelGauge.height = 100;




//Gravity
addEventListener(Event.ENTER_FRAME,Gravity)

var GyAmt:Number = 5;


function Gravity(e:Event){
mc_Guy.y += GyAmt;  

if (gameFloor.hitTestObject(mc_Guy)){
    mc_Guy.y = gameFloor.y;}
else if (gameFloor2.hitTestObject(mc_Guy)){
    mc_Guy.y = gameFloor2.y;}
else if (gameFloor3.hitTestObject(mc_Guy)){
    mc_Guy.y = gameFloor3.y;}}



// the event listeners
function update(e:Event):void
{
    if (keys[Keyboard.RIGHT])
    {
        mc_Guy.x += 3;
    }

    if (keys[Keyboard.LEFT])
    {
        mc_Guy.x -= 3;
    }
    if (keys[Keyboard.SPACE]){
        mc_Guy.y -= 10;
                    //drain fuel
                    fuelGauge.height -= 1;
                    gaugePercent.text = String(fuelGauge.height)
                    if (fuelGauge.height == 0){
                        nextFrame();}}
}

function onKeyDownGuy(e:KeyboardEvent):void
{
    keys[e.keyCode] = true;
}

function onKeyUpGuy(e:KeyboardEvent):void
{
    keys[e.keyCode] = false;

}






//Array storing each wall
var MC_wall:Array = new Array(mc_Wall,mc_Wall2,mc_Wall5,
mc_Wall6,mc_Wall7,mc_Wall8,mc_Wall9,mc_Wall10,mc_Wall11,mc_Wall12, mc_Wall13)




//Collision Detection
addEventListener(Event.ENTER_FRAME,loop);
function loop(e:Event){
        var i:Number = 0;
    do {
        if (MC_wall[i].hitTestObject(mc_Guy)){
            nextFrame();}
    i++;
    } while (i < MC_wall.length);


    /* for (var i:Number = 0; i < MC_wall.length; i++){
        if (MC_wall[i].hitTestObject(mc_Guy)){
            nextFrame();}
            }*/

     if(mc_Guy.hitTestPoint(mc_WallRun.x, mc_WallRun.y, false)){
            nextFrame();}
     else if(mc_Guy.hitTestObject(fuelPowerup)){
            removeChild(fuelPowerup);
            fuelGauge.height = 100;
            gaugePercent.text = String(fuelGauge.height)}
     else if(mc_Guy.hitTestObject(doorKey)){
        removeChild(doorKey)
                //open door 
            mc_Wall13.height -=20
            mc_Wall13.y -= 10
            }

}

//Object Movement
addEventListener(Event.ENTER_FRAME, onEnterFrame);
var spinSpeed:Number=6;
var axisMove:Number = 90;
var axisMovement:Number = 3;

function onEnterFrame(event:Event):void
{ 
mc_WallRun.rotation+=spinSpeed; 
        //count the movement on axis and move 
        axisMove -= 3;
        mc_Wall9.x += axisMovement;
        gameFloor3.x += axisMovement * .9;
        if(axisMove <= 0){
        axisMove += 90
        axisMovement = axisMovement*-1
        }
}

Gameover框架:

import flash.events.MouseEvent;

stop();

restartGame.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);
function playAgain(event:MouseEvent):void{
    prevFrame();
}

1 个答案:

答案 0 :(得分:4)

听起来就像你重新开始游戏一样

mc_Guy.addEventListener(Event.ENTER_FRAME, update);
再次调用

,而不删除以前添加的侦听器。

因此,现在在Event.ENTER_FRAME上调用了两次更新,然后每个游戏重置一次,导致该家伙移动得更快。