2010-09-22 97 views
0

使用Flash創建pacman風格遊戲(AS3)。有3名玩家爭相吃點最多的點。現在,當一個玩家吃一個點時,在該玩家的屏幕上,該點會消失(但只會持續一秒),然後再次出現在屏幕上。另一位玩家玩,並沒有看到這個點離開了,並再次出現。使用Flash創建pacman風格遊戲(AS3)

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


    } 

} 
+0

什麼問題? – Aaron 2010-09-22 04:42:04

+1

其他玩家是否看不到它消失?這不是100%清楚的,那不是你的意圖。只需重新閱讀這個問題,那可能是什麼? – Aaron 2010-09-22 04:52:40

回答

1

是否所有玩家都試圖在那裏寫棋盤到遊戲狀態。如果是這樣,那麼當一個玩家吃了一個點,並在那裏寫入地圖到遊戲狀態時,則另一個玩家可能不會意識到遊戲狀態已經改變並且用地圖覆蓋了地圖。這意味着被吃掉的點會重新出現。