使用 Flash 创建 pacman 风格的游戏 (AS3)
使用 Flash (AS3) 创建 pacman 风格的游戏。有 3 名玩家竞争吃掉最多的点数。现在,当一个玩家吃掉一个点时,该玩家的屏幕上的点就会消失(但只持续一秒钟)并再次出现在屏幕上。正在玩的其他玩家没有看到该点消失并重新出现。
使用 hitTestObject,当玩家触摸某个点时,该点不应再在舞台上看到。我正在使用共享对象来创建这个多人游戏环境。我是使用 SharedObject 和 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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有玩家都试图将棋盘写入游戏状态吗?如果是这样,那么当一个玩家吃掉一个点并将该地图写入游戏状态时,另一位玩家可能不会意识到游戏状态已更改并用该地图覆盖该地图。这意味着被吃掉的点会重新出现。
Are all players trying to write there board to the gamestate. If so then when one player eats a dot and writes there map to the gamestate, then another player might not realise that the gamestate has changed and overwrite the map with there map. This would mean that the eaten dot would re-appear.