I wrote one of my first classes in Actionscript 3 and I want someone versed in AS3 to help me fine tune my class. I am sure there are things that could really help with efficiency and I love learning about Object Oriented "Programming" so please take a look and let me know what you think.
Project background; This project is a card game that reads all the cards information from an XML file so the client can easily add, edit, or delete card information. The user will drag an Activity card from one of the six stacks and place it on top of the Event card. The six stacks will have a card face up so the user can see the information and match the best card to the Event card (ie ... an Event card may read "Burning auto wreck with a person inside" and the Activity cards may read "Pull Victim" or "Put out Flames" or etc .....
When the user makes the drop the Activity card on the Event Card it goes thru a point check and determines if it was a good drop and assigns points accordingly.
After the drop and points happen the next user clicks "Next >>" and the deck reshuffles and the process starts all over again until a certain amount of points occur.
Hopefully that made sense and you can get sone kind of idea on how it works, if not I can elaborate more if needed.
Document Class (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:ActivityCard;
private var _event:EventCard;
private var _randomCardArray:Array;
/*
* contructor
*/
public function CardGame():void
{
_loader = new URLLoader();
_loader.addEventListener(Event.COMPLETE,buildArray);
_loader.load(new URLRequest("library/card.xml"));
start_btn.addEventListener(MouseEvent.CLICK,buildDeck);
restart_btn.addEventListener(MouseEvent.CLICK,removeDeck);
}
/*
* 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;
}
private function randomizeArray():Array
{
//trace(buildArray(null));
_randomCardArray = new Array();
do
{
var cardItem = int(Math.random()*_cardArray.length);
_randomCardArray.push(_cardArray[cardItem]);
_cardArray.splice(cardItem,1);
}
while (_cardArray.length > 0);
return _randomCardArray;
}
public function buildDeck(e:Event):void
{
var _finalCardArray = randomizeArray();
//trace(_finalCardArray);
for(var i=0;i<_finalCardArray.length;i++)
{
if(_finalCardArray[i].face == "activity")
{
_activity = new ActivityCard();
_activity.x = 200;
_activity.y = 150;
_activity.buttonMode = true;
_activity.mouseChildren = false;
_activity.name = "card"+i;
_activity.id_txt.text = _finalCardArray[i].id;
_activity.value_txt.text = _finalCardArray[i].value;
_activity.addEventListener(MouseEvent.MOUSE_DOWN,dragStart);
_activity.addEventListener(MouseEvent.MOUSE_UP,dragStop);
addChild(_activity);
}
else if(_finalCardArray[i].face == "event")
{
_event = new EventCard();
_event.x = stage.stageWidth/2;
_event.y = 550;
_event.mouseChildren = false;
_event.name = "card"+i;
_event.id_txt.text = _finalCardArray[i].id;
_event.value_txt.text = _finalCardArray[i].value;
addChild(_event);
}
else
{
trace("no cards");
}
}
}
public function dragStart(e:Event):void
{
_activity = e.target as ActivityCard;
_activity.parent.setChildIndex(_activity, numChildren - 1);
_activity.startDrag(false);
}
public function dragStop(e:Event):void
{
this.stopDrag();
}
private function removeDeck(e:Event):void
{
if(_cardArray.length == 0)
{
for(var i=0;i<106;i++)
{
removeChild(getChildByName("card"+i));
}
buildArray(null);
}
buildDeck(null);
}
}
}
Thanks in advance :-)