flash - ActionScript 3. Drag and drop game going wrong -


i have flash game written in actionscript 3, here 4 items should dragged specific fields, it's working correctly when dragging item fields (if correct field - item field's x, y position , if field wrong - item returns fixed position).

it's going wrong when i'm trying drop item on other item (for example trying drag , drop item1 on item2), change positions. if drag item1 , not hittestobject correct field should items1's position, going item2 x, y position , item2 item1 x, y position.

i'm using following code:

private function getposition(target:object):void  {     xpos = target.x;     ypos = target.y; } private function dragobject(e:mouseevent):void {     getposition(e.target);     e.target.startdrag();     trace("started drag"); } private function stopdragobject(e:mouseevent):void {     if (e.target.hittestobject(e.currenttarget.parent[field])) {         e.target.x = (e.currenttarget.parent[field]).x;         e.target.y = (e.currenttarget.parent[field]).y;     } else {         e.target.x = xpos; //here part item get's position         e.target.y = ypos; //seems here problem     } } 

i think depth problem ( index position ) of items, because, example, when drag item depth 1 on 1 index 5, target of mouseevent.mouse_up event second object , not first, think can avoid setting index of current item highest in items container :

function dragobject(e:mouseevent):void {      var container:displayobjectcontainer = displayobjectcontainer(e.target.parent);         container.setchildindex(displayobject(e.target), container.numchildren - 1);      // other instructions  } 

edit :

to avoid second problem ( positioning target of mouseevent.mouse_up event @ (0, 0) or under last dragged object ), can use boolean var indicate if dragging item or not :

var is_dragging:boolean = false;  function dragobject(e:mouseevent):void {     is_dragging = true;      // other instructions } function stopdragobject(e:mouseevent):void {        if(!is_dragging) return;      // other instructions      is_dragging = false;     } 

the boolean var manner, can initialize xpos , ypos, ...

note : reproduce problem, can click anywhere on stage , release mouse on item, item positioned @ (0, 0) because xpos , ypos undefined, if have dragged other item, item positioned @ (xpos, ypos), under last dragged item.

hope can help.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -