flash - The button is still active even when I remove the cursor from it, ActionScript 3 -


ok have game have issue whenever press button, while button still being pressed if remove cursor without releasing mouse button button still active. how can resolve make whenever mouse not in button's hit zone not active----to explain better , simple..if press button adn dont let go of , if drag mouse off buttons zone , let go button still active, how can resolve that?. here code make player walk.

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

this is, think, looking for. should enough.

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 trigger when mouse out of object have add eventlistener type of event. of course, may understand it's not smart listen event if didn't clicked on btn before, that's why choose add mouse_out when mouse_down listened. since want set leftpressed when mouse guess want same when mouse out. after this, remove useless listeners (mouse_out) able listen them again , because it's common waste of memory.

but, can't understand why :

function moveleft(e:mouseevent):void {    if(mouseevent.mouse_down){        leftpressed = true;    }else if (mouseevent.mouse_up) {        leftpressed = false;    } } 

what use of ?!


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -