android - Move animated movieClips using buttons instead of arrow keys -
i'm trying develop game want character run when click button, , continue running if hold button. i'm new actionscript 3, i'm bit lost here.
i've found code satisfies requirements; uses arrow keys, below:
function moverunkei() { if (key.isdown(key.right)) { dx = 15; //speed runkei._xscale = 50; } else if (key.isdown(key.left)) { dx = -15; runkei._xscale = -50; } else { dx = 0; } runkei._x += dx; if (runkei._x < 100) runkei._x = 100; //30 if (runkei._x > 550) runkei._x = 550; if (dx != 0 && runkei._currentframe == 1) { runkei.gotoandplay("run"); } else if (dx == 0 && runkei._currentframe != 1) { runkei.gotoandstop("stand"); } } this.onenterframe = function(){ moverunkei(); }
i need able using buttons.
////////////////////////////////////////////////////////////////////////////////////
import flash.events.event; var mousedown:boolean; var speed:number=4; addeventlistener(mouseevent.mouse_down, onmousedown); addeventlistener(mouseevent.mouse_up, onmouseup); addeventlistener(event.enter_frame, onenterframe); function onmousedown(event:mouseevent):void { mousedown = true; } function onmouseup(event:mouseevent):void { mousedown = false; } function onenterframe(event:event):void { if (mousedown) { runkei.gotoandplay("run"); runkei.x += speed; } }
this code able make character move continuously when hold button didn't animate while move(the character freeze until release button) - i'm not sure how explain it.
you'll need add event listeners mouse down , mouse on each of buttons movement. have booleans keep track of whether or not button down.
it's worth mentioning code you've linked seems actionscript2 i've changed work as3
var leftdown:boolean = false; var rightdown:boolean = true; leftbutton.addeventlistener(mouseevent.mouse_down, onmousedown) rightbutton.addeventlistener(mouseevent.mouse_down, onmousedown) leftbutton.addeventlistener(mouseevent.mouse_up, onmouseup) rightbutton.addeventlistener(mouseevent.mouse_up, onmouseup) leftbutton.addeventlistener(mouseevent.mouse_out, onmouseup) rightbutton.addeventlistener(mouseevent.mouse_out, onmouseup) function onmousedown(e:mouseevent):void { //since can't click on 2 things @ once, fine. rightdown = (e.target == rightbutton); leftdown = (e.target == rightbutton); } function onmousedown(e:mouseevent):void { //since can't click on 2 things @ once, fine. rightdown = (e.target == rightbutton); leftdown = (e.target == leftbutton); } function moverunkei() { if (rightdown) { dx = 15; //speed runkei.scalex = -0.5; } else if (leftdown) { dx = -15; runkei.scalex = -0.5; } else { dx = 0; } runkei.x += dx; if (runkei.x < 100) runkei.x = 100; //30 if (runkei.x > 550) runkei.x = 550; if (dx != 0 && runkei.currentframe == 1) { runkei.gotoandplay("run"); } else if (dx == 0 && runkei.currentframe != 1) { runkei.gotoandstop("stand"); } } this.addeventlistener(event.enter_frame, onenterframe); function onenterframe(e:event):void { moverunkei(); }
Comments
Post a Comment