Creating a pacman-style game using Flash (AS3). There are 3 players competing to eat the most dots. Right now when one player eats a dot, on that player's screen the dot goes away (but only for a second) and appears again in the screen. The other player playing, doesn't see that the dot went away and reappeared.
Using hitTestObject, when a player touches a dot, the dot should no longer be seen on the stage. I'm using shared object to create this multi-player game environment. I'm new in using SharedObject and also AS3.
public function PlayerSelect()
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://r92kq5ew6.rtmphost.com/g1");
select_screen.btn1.addEventListener(MouseEvent.MOUSE_UP, select1);
select_screen.btn2.addEventListener(MouseEvent.MOUSE_UP, select2);
select_screen.btn3.addEventListener(MouseEvent.MOUSE_UP, select3);
for(var i=0; i<circ_num; i++) {
circle_ary[i] = new Circle(); //linkage in the library
circle_ary[i].x=0;
circle_ary[i].y=0;
stage.addChild(circle_ary[i]);
}
}
public function netStatusHandler(event:NetStatusEvent):void
{
trace("connected is: " + nc.connected );
trace("event.info.level: " + event.info.level);
trace("event.info.code: " + event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
trace("Congratulations! you're connected");
so = SharedObject.getRemote("ballPosition", nc.uri, false);
so.connect(nc);
so.addEventListener(SyncEvent.SYNC, syncHandler);
break;
case "NetConnection.Connect.Rejected":
case "NetConnection.Connect.Failed":
trace ("Oops! you weren't able to connect");
break;
}
}
private function stageInit():void
{
for(var i=0; i<circ_num; i++) {
pos_ary[i] = new Array();
pos_ary[i][0] = Math.random()*stage.stageWidth;
pos_ary[i][1] = Math.random()*stage.stageHeight;
so.setProperty("dots", pos_ary);
}
}
// update clients when the shared object changes
private function syncHandler(event:SyncEvent):void
{
// when a sync event is fired
// update the information for all clients
//here we update states for all players
for (var i:String in so.data) //run through all players in the data array
{
if (i == "dots")
{
for(var j=0; j<circ_num; j++)
{
circle_ary[j].x = so.data["dots"][j][0];
circle_ary[j].y = so.data["dots"][j][1];
//pos_ary[j][0] = so.data["dots"][j][0];
//pos_ary[j][i] = so.data["dots"][j][i];
}
}
else if(player_ary[i] == undefined)
makePlayer(i); //if the player does not exist we create it
else if (i!=me) //we do not need to update our selves from the server, just everyone else
{
player_ary[i].x = so.data[i][0]; //here I am treating data like a 2d array
player_ary[i].y = so.data[i][1]; //where [i][0] is x poition data and
} //[i][1] is y position data
}
}
// function eatCircle --------------------------------------------------------------
function eatCircle():void {
for (var j:int = 0; j<circ_num; j++)
{
if (player_ary[me].hitTestObject(circle_ary[j]))
{
trace ("I ate the circle");
circle_ary[j].y = -100;
pos_ary[j][1] =-100;
so.setProperty("dots", pos_ary);
}
}
}