Starling ios drag object larger than screen and set boundaries -
in starling have large movieclip. movieclip avout 800% wider screen. if use as3 can set boundary, in can drag large mc. way mc cannot dragged out of screen (so empty background shown). know if possible in starling?
pseudo-code below
1. add mc stage 2. add eventlistener touchevent on mc 3. drag mc 0,0 coordinates smaller stage 0,0 coordinates 4. drag mc widthx,widthy within stagewidthx, stagewidthy
hope makes sense? want drag large mc around within screen area.
cheers.
ps: sorry not including example, have no clue how this.
you can examining touchphase of touchevent see finger dragging to, , update x position of movieclip if new position falls within stage boundaries.
here example, using quad same movieclip:-
package { import starling.core.starling; import starling.display.displayobject; import starling.display.quad; import flash.geom.point; import starling.display.sprite; import starling.events.touchevent; import starling.events.touch import starling.events.touchphase; public class quaddrag extends sprite { private var _startxpos:int = 0; // start of each drag public function quaddrag() { super(); // create quad 8x stage size var quad:quad = new quad(starling.current.stage.stagewidth*8, starling.current.stage.stageheight, 0xffffff); quad.setvertexcolor(0, 0xff0000); quad.setvertexcolor(1, 0x0000ff); quad.setvertexcolor(2, 0xff0000); quad.setvertexcolor(3, 0x0000ff); addchild(quad); // center quad on stage quad.x = math.round(starling.current.stage.stagewidth/2 - quad.width/2); quad.addeventlistener(touchevent.touch, onquadtouch); } private function onquadtouch(e:touchevent):void { var currentxpos:int = 0; var newxpos:int = 0; var touch:touch = e.gettouch(stage); var target:displayobject = e.currenttarget displayobject; if (touch == null) { return; } var position:point = touch.getlocation(stage); if (touch.phase == touchphase.began ) { // store start of drag x pos _startxpos = target.globaltolocal(new point(touch.globalx, touch.globaly)).x; } else if (touch.phase == touchphase.moved ) { // set limits target x var minx:int = -math.round(target.width - starling.current.stage.stagewidth); var maxx:int = 0; // calculate new x based on touch's global coordinates currentxpos = target.globaltolocal(new point(touch.globalx, touch.globaly)).x; newxpos = target.x + currentxpos - _startxpos; if (newxpos <= maxx && newxpos>=minx) // set target's x if falls within limits target.x=newxpos; } else if (touch.phase == touchphase.ended ) { // touch released } return; } } }
Comments
Post a Comment