Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You never set the variable activity to the correct target. An easy fix is to set it in the dragStart method. This will ensure that you always drag the correct card.</p> <pre><code>activity = e.target as Activity; </code></pre> <hr> <pre><code>public function dragStart(e:Event):void { activity = e.target as Activity; activity.parent.setChildIndex(activity, numChildren - 1); activity.startDrag(false); } </code></pre> <hr> <p>About your other question, I dont think that it should take such a long time parsing the XML and creating the instances. Maybe some of your methods are causing the delay. Or maybe there are heavy graphics that you could optimize.</p> <hr> <p><strong>EDIT: This code makes the sorting work for me. Hope it helps.</strong></p> <hr> <p>Main.as</p> <pre><code>package { import flash.display.*; import flash.events.*; import flash.net.*; public class CardGame extends Sprite { /* * variables */ private var _xml:XML; private var _loader:URLLoader; private var _cardArray:Array; private var _activity : Activity; /* * constructor */ public function CardGame() { _loader = new URLLoader(); _loader.addEventListener(Event.COMPLETE, buildArray); _loader.load(new URLRequest("cards.xml")); //click_btn.addEventListener(MouseEvent.CLICK,buildDeck); } /* * metohods */ public function buildArray(e:Event) : void { _cardArray = new Array(); _xml = new XML(_loader.data); for(var i=0;i&lt;_xml.card.length();i++) { if(_xml.card.face[i]=="activity") { _cardArray.push({id:_xml.card.id[i],face:_xml.card.face[i],category:_xml.card.category[i],point:_xml.card.point[i],value:_xml.card.value[i]}); } else if(_xml.card.face[i]=="event") { _cardArray.push({id:_xml.card.id[i],face:_xml.card.face[i],category:_xml.card.category[i],point:_xml.card.point[i],value:_xml.card.value[i]}); } else { trace("no card found"); } } buildDeck(null); } public function buildDeck(e:Event):void { for(var i=0 ; i &lt; _cardArray.length ; i++) { if(_cardArray[i].face == "activity") { _activity = new Activity(); _activity.x = 200; _activity.y = 150; _activity.id = i; _activity.name = "b"+i; _activity.buttonMode = true; addChild(_activity); _activity.addEventListener(MouseEvent.MOUSE_DOWN,dragStart); _activity.addEventListener(MouseEvent.MOUSE_UP,dragStop); } } } public function dragStart(e:Event):void { _activity = e.target as Activity; _activity.parent.setChildIndex(_activity, numChildren - 1); _activity.startDrag(false); } public function dragStop(e:Event):void { this.stopDrag(); } } } </code></pre> <hr> <p>Activity.as</p> <pre><code>package { import flash.display.Sprite; public class Activity extends Sprite { private var _id : int; public function Activity() { super(); with(graphics) { lineStyle(0, 0xFF0000, 1); beginFill(Math.random()*0xFFFFFF, 1.0); drawRect(0, 0, 80, 100); } } public function get id():int { return _id; } public function set id(value:int):void { _id = value; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload