PDA

View Full Version : display - remove players from screen from XML feed


LOLFlash
06-30-2009, 02:02 AM
Hi all
I'm new in game design need some suggestion
I have XML feed from server refreshing every 2 sec.
In XML I have 2 teams and number of players in each team can be vary from 0 to 20;

<players>
<team1>
<player name="JOC">
<x>45</x>
<y>4</>........
</player>
...........
</team1>
<team2>
...............
</team2>
</players>

each node <player> represents on screen MovieClip with Click listener

every time I'm receiving XML I'm setting a properties for each player according the node
<player name="BOB">
<x>30</x>
<y>68</>........
</player>

attribute name unique in each team;

But if in next XML player disappeared, I have to remove it from screen and as well if new joined I have to add it on screen:

Where to start to add and remove players according new XML every 2 sec.

rrh
06-30-2009, 07:31 PM
Maybe the simplest to program would be to remove them all and then re-add everything listed in the XML. It might not the most efficient in terms of processing, but it avoids having to write separate adding and removing code.

LOLFlash
07-01-2009, 12:48 AM
thx rrh

What about garbage collection.
Every 2 sec I have to remove players with their listeners and create new. Is it OK for flash?

rrh
07-01-2009, 02:17 AM
Like I said, it might be inefficient for processing.

A slightly more complex but less processor heavy method would be:


//lastTimePlayers has players from last update
var currentPlayers newPlayers:Object = empty objects;

for each player listed in XML {
if a player of that name exists in lastTimePlayers {
update the position
remove it from lastTimePlayers
put it in currentPlayers
} else {
add this player to newPlayers;
}
}

loop through lastTimePlayers and remove any that no longer exist

loop through newPlayers and create each one, adding them to currentPlayers

move all the players from currentPlayers to lastTimePlayers

LOLFlash
07-01-2009, 05:00 AM
You right remove all and add all much easy. But I'll try your last code to make it work.
Will publish later on what I get.

bluemagica
07-01-2009, 10:21 AM
I don't know if I should recommend this, cause its a bit advanced, but in your situation, an object pool would work great!
http://www.lostinactionscript.com/blog/index.php/2008/10/30/object-pooling-in-as3/

LOLFlash
07-01-2009, 06:55 PM
I briefly went through this project and I have one question:
What object pool can do what AS3 can't?
For now I want to try rrh way:


var tempAr:Array=playersAr.slice(0);
for each (var xml:XML in dataXML.team1)
{
var player= this.getChildByName("team1_"+xml.@name);
if (player==nul){
var myplayer:Player= new Player(xml);
myplayer.name="team1_"+xml.@name;
playersAr.push(this.addChild(myplayer));

}else {
delete tempAr[tempAr.indexOf(player)];
player.setXML=xml;
player.flag=curTime;

}
....... same for team2......

var n:uint=tempAr.length;
for (var i=n;i>0;i--){
if(tempAr[i-1] == null){
var r=playersAr[i-1];
r.removeEventListener(MouseEvent.MOUSE_OVER,over);
r.removeEventListener(MouseEvent.MOUSE_OUT,out);
this.removeChild(r);
playersAr.splice(i-1,1);
}
}