actionscript 3 - AS3 shuffling movieclips -
i've added basic targets , applying drag , drop puzzle pieces, im having trouble making shuffling aspect. in, after player completes or opens fla, each time start puzzle pieces in random places of stage. understand using arrays shuffling somehow im not sure how achieve this. i've stored instance of 19 puzzle pieces inside array have no idea array. other tutorials abit out of league , leaves head scratching.
just started doing coding flash professional yeah, shuffling movie clips ie puzzles pieces appreciated.
heres's code, im not posting whole thing since p1 p19 copy pasting:
import flash.events.event; stage.addeventlistener(event.enter_frame, entframe) function entframe(e: event) : void { p1.addeventlistener(mouseevent.mouse_down, fl_clicktodrag); function fl_clicktodrag(event:mouseevent):void { p1.startdrag(); } stage.addeventlistener(mouseevent.mouse_up, fl_releasetodrop); function fl_releasetodrop(event:mouseevent):void { p1.stopdrag(); } if (t1.hittestobject(p1.tar1)) { p1.x = 313.15; p1.y = 242.75; } p19.addeventlistener(mouseevent.mouse_down, fl_clicktodrag_19); function fl_clicktodrag_19(event:mouseevent):void { p19.startdrag(); } stage.addeventlistener(mouseevent.mouse_up, fl_releasetodrop_19); function fl_releasetodrop_19(event:mouseevent):void { p19.stopdrag(); } if (t19.hittestobject(p19.tar19)) { p19.x = 624.35; p19.y = 455.60; } }
here hope more holistic answer.
first, ditch inline functions. right make enter_frame listener , inside function have inline function defined. means every frame tick (which tied frame rate, not main timeline), functions going created again, , since adding them handlers listeners, stay in memory forever.
here way code this, showing ways reduce redundancy , rid of memory leaks. assumes following:
you have 19 objects on stage called
t1
-t19
, represent possible locations pieces can go.you have 19 pieces on stage called
p1
-p19
, that, , numbers correlatet
locations per correct location of piece.//let's create function randomize piece location function seedpieces() { //create array consisting of integers 1 - 19 var unusedspaces:vector.<int> = new vector.<int>; var i:int; (i = 1; <= 19; i++) { //populate array unusedspaces.push(i); } var curlocation:displayobject; //helper var loop below var curpiece:sprite; //helper var loop below //loop 19 times (from 1 - 19) - 1 iteration each piece (i = 1; <= 19; i++) { curpiece = this["p" + i] sprite; //you can piece way, or use array if you've made one, `pieces[i];` trace(curpiece.name); //splice removes , returns item @ specified index (in case random number between 0 , arrays length less 1) - second parameter amount of items remove (just 1 case) curlocation = this["t" + unusedspaces.splice(int(math.random() * unusedspaces.length), 1)] displayobject; trace(" ",curlocation.name); //move piece random location: curpiece.x = curlocation.x; curpiece.y = curlocation.y; } } //now, aside, should use loop add listeners sake of sanity - if have them in array, loop through that, or use sloppy way this: (var i:int = 1; <= 19; i++) { sprite(this["p" + i]).addeventlistener(mouseevent.mouse_down, fl_clicktodrag); } //create var hold piece being dragged, know piece stop drag on later var currentdraggingitem:sprite; seedpieces(); function fl_clicktodrag(event:mouseevent):void { //assign clicked item currentdraggingitem var currentdraggingitem = event.currenttarget sprite; //bring 1 front currentdraggingitem.parent.addchild(currentdraggingitem); //you can use 1 click handler pieces //the piece clicked, referenced event.currenttarget currentdraggingitem.startdrag(); //add mouse listener mouse down stage.addeventlistener(mouseevent.mouse_up, fl_releasetodrop); //listen every frame while dragging stage.addeventlistener(event.enter_frame, entframe); } function fl_releasetodrop(event:mouseevent):void { //if currentdraggingitem has value, stop drag if (currentdraggingitem) { currentdraggingitem.stopdrag(); //send currentdraggingitem.parent.addchildat(currentdraggingitem,0); } //remove mouse , enter frame listener mouse stage.removeeventlistener(mouseevent.mouse_up, fl_releasetodrop); stage.removeeventlistener(event.enter_frame, entframe); if(checkcomplete()){ //game over, } } function entframe(e: event) : void { //this snap peice correct spot when mouse touching correct spot if(currentdraggingitem){ if (this[currentdraggingitem.name.replace("p","t")].hittestpoint(mousex,mousey)) { currentdraggingitem.x = this[currentdraggingitem.name.replace("p","t")].x; currentdraggingitem.y = this[currentdraggingitem.name.replace("p","t")].y; } } } function checkcomplete():boolean { //use loop go through pieces , check if in right spot. again, have them in array, or lazy way (var i:int = 1; <= 19; i++) { if (!this["t"+i].hittestobject(this["p"+i])) { return false; } } return true; }
Comments
Post a Comment