0

好的,所以我有我的游戏,但是我有一个问题,每当我按下按钮时,如果我从按钮上移开光标而不释放鼠标按钮,按钮仍然处于活动状态。我该如何解决它以使鼠标不在按钮的点击区域中时它不是活动的----为了更好和简单地解释它..如果我按下按钮并且不要放开它,如果我是将鼠标拖出按钮区域并松开按钮仍将处于活动状态,我该如何解决?这是我让玩家走路的代码。

    var leftPressed:Boolean = false;
var rightPressed:Boolean = false;

var upPressed:Boolean = false;
var shootDown:Boolean = false;

var onGround:Boolean = true;


var level:Number = 1;

var bullets:Array = new Array();
var container_mc:MovieClip; 
var enemies:Array = new Array();

var score:Number = 0;

var tempEnemy:MovieClip;

var tempLaser:MovieClip;

var timesHit:uint = 0;

// BUTTON EVENTS EITHER CLICKED OR NOT

left_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveRight);
up_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveUp);
shoot_btn.addEventListener(MouseEvent.MOUSE_DOWN, shootPressed);

left_btn.addEventListener(MouseEvent.MOUSE_UP, leftUp);
right_btn.addEventListener(MouseEvent.MOUSE_UP, rightUp);
up_btn.addEventListener(MouseEvent.MOUSE_UP, upUp);


player.gotoAndStop('still');


stage.addEventListener(Event.ENTER_FRAME,onenter);

function onenter(e:Event):void{
    if (rightPressed == true && leftPressed == false){
        player.x += 8;
        player.scaleX = 1;
        player.gotoAndStop("walking");
        cloud.x -= 8;


    } else if (leftPressed == true && rightPressed == false){
        player.x -= 8;
        player.scaleX = -1;
        player.gotoAndStop('walking');
        cloud.x += 8;

   } else if(upPressed == true && leftPressed == false && rightPressed == false){


       }
        else{  
        rightPressed = false;
        leftPressed = false;
        player.gotoAndStop('still')}

}
// **** MOVEMENT CONTROLS *********



function shootPressed(e:MouseEvent):void{
    shootDown = true;
    if(shootDown == true){
        fireBullet();
        }

    }


function fireBullet():void
{
    var playerDirection:String;
    if(player.scaleX < 0){
        playerDirection = "left";
    } else if(player.scaleX > 0){
        playerDirection = "right";
    }
    var bullet:Bullet = new Bullet(player.x, player.y, playerDirection);
    //bullets = new Array();
    bullet.y = player.y + 8;
    stage.addChild(bullet);
    bullets.push(bullet);
    trace(bullets);

}

// BUTTON FUNCTIONS

function moveLeft(e:MouseEvent):void
{
    if(MouseEvent.MOUSE_DOWN){
    leftPressed = true;
    }else if (MouseEvent.MOUSE_UP) {
        leftPressed = false;
        }

}
function moveRight(e:MouseEvent):void
{
    if (MouseEvent.MOUSE_DOWN){
    rightPressed = true;
    }else if (MouseEvent.MOUSE_UP){
        rightPressed = false;
        }

}

function moveUp(e:MouseEvent):void
{
    if(MouseEvent.MOUSE_DOWN){
        upPressed = true;
        onGround = false;
        player.y -= 100;
        } 



}



function leftUp(e:MouseEvent):void
{
    leftPressed = false;
}
function rightUp(e:MouseEvent):void
{
    rightPressed = false;
}
function upUp(e:MouseEvent):void
{
    upPressed = false;
 if(upPressed == false){
     player.y += 100;

     }
}
4

2 回答 2

0

除了 mouseup 事件之外,您还需要对 mouse out 事件做一些事情。如果不这样做,当鼠标按下(“拖出”)时光标从按钮上移动时,永远不会触发鼠标向上事件。

使用事件MouseEvent.MOUSE_OUTMouseEvent.RELEASE_OUTSIDE

最好的方法是将事件监听器添加到MOUSE_UP事件中,或者MOUSE_OUTRELEASE_OUTSIDE触发MOUSE_DOWN事件时添加事件侦听器。

代码示例:

left_btn.addEventListener(MouseEvent.MOUSE_DOWN, handleButtonDown);


function handleButtonDown(e:MouseEvent):void 
{
   // activate "up" listeners
   e.target.addEventListener(MouseEvent.MOUSE_UP, handleButtonUp);
   e.target.addEventListener(MouseEvent.MOUSE_OUT, handleButtonUp);
   e.target.addEventListener(MouseEvent.RELEASE_OUTSIDE, handleButtonUp);

   // call your button action function
   // e.target is your button
   buttonDownAction(e.target);

}


function handleButtonUp(e:MouseEvent):void 
{
   // activate "up" listeners
   e.target.removeEventListener(MouseEvent.MOUSE_UP, handleButtonUp);
   e.target.removeEventListener(MouseEvent.MOUSE_OUT, handleButtonUp);
   e.target.removeEventListener(MouseEvent.RELEASE_OUTSIDE, handleButtonUp);

   // call your button action function
   // e.target is your button
   buttonUpAction(e.target);

}

function buttonDownAction(button:MovieClip) {
   switch(button) {
      case left_btn: 
          // do your thing here

      case right_btn:
          // do something else here
    }
}

在这个例子中,我使 handleButtonDown 和 handleButtonUp 更加通用,因此您可以将这 2 个函数用于所有按钮。

希望对你有帮助,祝你好运!

于 2013-03-24T20:09:28.453 回答
0

我想这就是你要找的东西。这应该足够了。

left_btn.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft);
left_btn.addEventListener(MouseEvent.MOUSE_UP, LeftUp);

function moveLeft(e:MouseEvent):void
{
  leftPressed = true;
  left_btn.addEventListener(MouseEvent.MOUSE_OUT, LeftOut);
}

function leftUp(e:MouseEvent):void
{
  leftPressed = false;
  left_btn.removeEventListener(MouseEvent.MOUSE_OUT, LeftOut);
}

function leftOut(e:MouseEvent):void
{
  leftPressed = false;
  left_btn.removeEventListener(MouseEvent.MOUSE_OUT, LeftOut);
}

MOUSE_OUT 只是在鼠标离开您的对象时触发,因此您只需添加具有此类事件的 EventListener。当然,你可能会明白,如果你之前没有点击过你的btn,那么监听这个事件并不聪明,这就是为什么我选择只在监听MOUSE_DOWN时才添加MOUSE_OUT。因为你想要的只是在你抬起鼠标时设置 leftPressed 我猜你想要鼠标不在时也一样。在此之后,我删除了无用的侦听器 (MOUSE_OUT) 以便能够再次收听它们,因为这是一种常见的内存浪费。

但是,我真的不明白你为什么这样做:

function moveLeft(e:MouseEvent):void
{
   if(MouseEvent.MOUSE_DOWN){
       leftPressed = true;
   }else if (MouseEvent.MOUSE_UP) {
       leftPressed = false;
   }
}

那有什么用?!

于 2013-03-24T20:14:48.410 回答