我正在创建一个XML自定义纸牌游戏,我正在构建卡片并使它们可拖动,以便用户可以将它们拖放到另一张卡片上。
我已经想出如何构建套牌并放置驻留在库中的影片剪辑的多个实例,我只是想办法让每张卡都可以拖动。目前正在发生的事情是,即使我点击它下方的卡片,卡片顶部的卡片也是唯一会拖动的卡片。我研究过setChildIndex()
,但我认为我没有正确使用它。
我遇到的另一个问题是,当用户点击click_btn(仅用于测试命名)来构建它需要一段时间才能构建它时,可以加速吗?
然后,当用户点击第一张卡片进行拖动时,会有另一个漫长的等待时间吗?
文件类(CardGame.as)
package library
{
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():void
{
_loader = new URLLoader();
_loader.addEventListene(Event.COMPLETE,buildArray);
_loader.load(new URLRequest("library/card.xml"));
click_btn.addEventListener(MouseEvent.CLICK,buildDeck);
}
/*
* metohods
*/
public function buildArray(e:Event):Array
{
cardArray = new Array();
_xml = new XML(_loader.data);
for(var i=0;i<_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");
}
}
return cardArray;
}
public function buildDeck(e:Event):void
{
for(var i=0;i<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;
stage.addChild(activity);
activity.addEventListener(MouseEvent.MOUSE_DOWN,dragStart);
activity.addEventListener(MouseEvent.MOUSE_UP,dragStop);
}
}
}
public function dragStart(e:Event):void
{
activity.parent.setChildIndex(activity, numChildren - 1);
activity.startDrag();
}
public function dragStop(e:Event):void
{
this.stopDrag();
}
}
}
card.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<cardgame>
<card>
<id>1</id>
<face>activity</face>
<catagory>Task</catagory>
<point>2</point>
<value>Change the rule.</value>
</card>
<card>
<id>21</id>
<face>event</face>
<catagory>Injury</catagory>
<point>-2</point>
<value>Injury due to animal/snake/insect bite-count 3.</value>
</card>
</cardgame>
xml文件实际上有64个活动卡条目和41个事件卡条目,但我将其修剪下来以使其更容易。
答案 0 :(得分:0)
您永远不会将变量活动设置为正确的目标。一个简单的解决方法是在dragStart方法中设置它。这将确保您始终拖动正确的卡片。
activity = e.target as Activity;
public function dragStart(e:Event):void
{
activity = e.target as Activity;
activity.parent.setChildIndex(activity, numChildren - 1);
activity.startDrag(false);
}
关于你的另一个问题,我不认为解析XML和创建实例需要这么长时间。也许你的一些方法导致了延迟。或者也许你可以优化重型图形。
编辑:此代码使我的排序工作。希望它有所帮助。
Main.as
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<_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 < _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();
}
}
}
Activity.as
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;
}
}
}