Move object while mouse is pressed
i want move object right or left while mouse left button held down. if mouse pressed , located on left half of screen, want object move left (decrease x) , vice versa right.
my current solution create 2 buttons covering screen , adding mouseevent.click listeners , so:
btn_clickleft.addeventlistener(mouseevent.click, btn_clickleft_mouse);
function btn_clickleft_mouse(e:mouseevent):void {
object_to_move.x-=10;
}
btn_clickright.addeventlistener(mouseevent.click, btn_clickright_mouse);
function btn_clickright_mouse(e:mouseevent):void {
object_to_move.x+=10;
}
but causes 2 problems:
1. object moves 10 pixels once per mouse click, how make continously move?
2. if object want move passes below mouse cursor, events stop firing.
is there solution solves these 2 problems?
hey!
so, following logic, solve issue number 1, this:
import flash.events.mouseevent;
import flash.events.event;
leftbtn.addeventlistener(mouseevent.mouse_down, startmoveleft);
rightbtn.addeventlistener(mouseevent.mouse_down, startmoveright);
leftbtn.addeventlistener(mouseevent.mouse_up, stopmoveleft);
rightbtn.addeventlistener(mouseevent.mouse_up, stopmoveright);
function startmoveleft(evt:mouseevent):void{
addeventlistener(event.enter_frame, moveleft);
}
function startmoveright(evt:mouseevent):void{
addeventlistener(event.enter_frame, moveright);
}
function stopmoveleft(evt:mouseevent):void{
removeeventlistener(event.enter_frame, moveleft);
}
function stopmoveright(evt:mouseevent):void{
removeeventlistener(event.enter_frame, moveright);
}
function moveleft(evt:event):void{
objectmc.x -= 10;
}
function moveright(evt:event):void{
objectmc.x += 10;
}
where leftbtn , rightbtn buttons placed on each half of stage , objectmc object want move.
as issue number 2, make sure put 2 buttons in layer on top of layer have object move.
i hope works you!
More discussions in ActionScript 3
adobe
Comments
Post a Comment