使用Flash创建一个pacman风格的游戏(AS3)

时间:2010-09-22 01:06:06

标签: flash shared-objects

使用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);
        }


    }

}

1 个答案:

答案 0 :(得分:1)

所有玩家都试图在那里写游戏到游戏状态。如果是这样,那么当一个玩家吃点并在那里写入地图到游戏状态时,那么另一个玩家可能没有意识到游戏状态已经改变并用那个地图覆盖地图。这意味着吃掉的点会重新出现。