速度在actionscript 3.0中保持堆叠

时间:2017-06-08 14:10:28

标签: actionscript-3

我们一直在为我们的班级CPT制作这个游戏,你扮演一个角色并尝试通过这些级别,但问题是,每次你死,然后再回到玩水平,速度叠加在以前的速度到了游戏不再可玩的程度。我们已经尝试重置它并将其重新设置为0但似乎没有任何效果我们也尝试编码它以便速度超过21它重置为20(我们理想的速度)我们还尝试删除可能的额外事件监听器一直堆叠在另一个上面。如果你碰巧死了但是到了下一帧,那个帧的速度会回到20帧。但是如果你死在那个帧上,那么它在那个帧上的速度加倍。我们的代码是:

import flash.events.Event;
import flash.display.MovieClip;



//makes the character jump
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKeyPressed);
stage.addEventListener(Event.ENTER_FRAME, update);

function whenKeyPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.UP)
{
    if(jumping != true)
    {
        jumpPow = -50;
        jumping = true;
    }
}   
}


function update(event:Event):void
{
if(jumping)
{
    player_mc.y += jumpPow;
    jumpPow += grav;

    if(player_mc.y >= stage.stageHeight)
    {
        jumping = false;
        player_mc.y = stage.stageHeight;
    }
    }
    //when the character makes it to the other side of the screen, the frame 
    changes
    if (player_mc.hitTestObject(frameChanger1))
    {
    gotoAndStop(91);
    }
    //end the game when the character touches the penguin
    if (player_mc.hitTestObject(penguin1))
   {
    gotoAndStop(89);
   }
   } 

   //makes the character move left and right
   var leftPressed:Boolean = false;
   var rightPressed:Boolean = false;

   player_mc.addEventListener(Event.ENTER_FRAME, moveInDirectionOfKey);
   stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
   stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);

    function moveInDirectionOfKey(event:Event)
  {
   if (leftPressed)
  {
    player_mc.x -= 20;
  }
   if (rightPressed)
  {
    player_mc.x += 20;
  }
  }

   function setKeyPressed(event:KeyboardEvent):void
  {
   switch (event.keyCode)
  {
    case Keyboard.LEFT:
    {
        leftPressed = true;
        break;
    }
    case Keyboard.RIGHT:
    {
        rightPressed = true;
        break;
    }
    }
   }

     function unsetKeyPressed(event:KeyboardEvent):void
    {
        switch (event.keyCode)
    {
    case Keyboard.LEFT:
    {
        leftPressed = false;
        break;
    }
    case Keyboard.RIGHT:
    {
        rightPressed = false;
        break;
    }
    }
   //when the character makes it to the other side of the screen, the frame 
   changes
   if (player_mc.hitTestObject(frameChanger1))
  {
    gotoAndStop(91);
  }
    //end the game when the character touches the penguin
    if (player_mc.hitTestObject(penguin1))
  {
    gotoAndStop(89);
  }
  }

1 个答案:

答案 0 :(得分:1)

你做过尝试:

if(!stage.hasEventListener(Event.ENTER_FRAME)){
    stage.addEventListener(Event.ENTER_FRAME, update);
}
if(!stage.hasEventListener(KeyboardEvent.KEY_DOWN)){
    stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
}
if(!stage.hasEventListener(KeyboardEvent.KEY_UP)){
    stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);
}
if(!player_mc.hasEventListener(Event.ENTER_FRAME)){
    player_mc.addEventListener(Event.ENTER_FRAME, moveInDirectionOfKey);
}

等等......

否则您将多次注册活动...... 这可能是个问题。 我不确定,我避免在多个帧上进行编码。

您也可以在Function being called faster

使用@Philarmon的答案