Forum ActionScript 3.0 Adding New Objects problem

Problems and discussions relating to ActionScript 3.0 here.

Adding New Objects problem

Postby Evan999333 » October 30th, 2011, 10:25 pm

I am very new to using player IO and Serverside code so I am sure this is probably me just not knowing something simple but what I want is for when a player joins the game they get a movieclip that they can control and see the other people in the room moving.

What I have on the serverside (using Microsoft Visual Studio) is
Code: Select all
// This method is called whenever a player joins the game
      public override void UserJoined(Player player) {
            // Create a Player
            Broadcast("createPlayer", player.Id);
         // Create init message for the joining player
         Message m = Message.Create("init");

         // Tell player their own id
                  // Send init message to player

Then in my main .as file I have the following code:
Code: Select all
connection.addMessageHandler("createPlayer", function(m:Message, id:int){

To get the message from the server to create the player and the createPlayer() function looks like this:
Code: Select all
private function createPlayer():void{
         // Create Player
         var playerMovieClipVar:PlayerMovieClip = new PlayerMovieClip
         //Add listener dispatched when the player is moved
         playerMovieClipVar.addEventListener(Event.CHANGE, function(e:Event){
            var target:PlayerMovieClip = as PlayerMovieClip;
            //Inform server that the player was moved
            connection.send("movePlayer",target.Id, Math.round( target.x ), Math.round( target.y ))
         //Add player to stage

In the I have some code that simply makes it go form the top of the screen to the bottom.

The result of this adds the player to the stage but with a few problems.

1. The player that joins can not see any of the previous playerMovieClips of the previous players that joined before them.
For intsance player one sees its movieclip, player twos movieclip, and player threes movieclip
player two sees its movieclip and player threes movieclip
player three only sees its movieclip.

2. The movement looks ok but if I try to make it so the movieclips are contorted with the arrow keys it causes compiler errors that I will explaine if I can get problem 1 fixed.

Any help would be greatly appreciated, I hope the information provided makes sense.
Last edited by Toby on November 11th, 2011, 9:43 pm, edited 1 time in total.
Reason: Added code tags for readability.
Posts: 1
Joined: October 30th, 2011, 1:36 am

Re: Adding New Objects problem

Postby Toby » November 11th, 2011, 9:42 pm

The player wasn't in the room when the other players joined, of course, so how would it know that the other players are there? (It never got a join message).

You can amend it by adding
Code: Select all
foreach( Player p in Players ) {
    if(p == player) continue;
    player.Send("createPlayer", p.Id);

to your UserJoined method. Basically it just sends every joined player to the new guy when they join. The p == player skip is so they don't get the player that you've already broadcast again.
User avatar
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Return to ActionScript 3.0