Jump to content

moveToXY with socket.IO problem


oom1993
 Share

Recommended Posts

socket.on('bulletFromServ',function(bullet){

          activeBullets[bullet.id][bullet.num] = game.add.sprite(connectedSprites[bullet.id].x - 8,connectedSprites[bullet.id].y - 8, 'bulletSprite');

          game.physics.arcade.enable(activeBullets[bullet.id][bullet.num]);

          game.physics.arcade.moveToXY(activeBullets[bullet.id][bullet.num], bullet.xDest, bullet.yDest, 300);

      });

Hi, i am having an issue with socket IO together with the moveToXY physics method. When the client receives the "bulletFromServ" message above. I try to initialize a sprite and then use the information sent with the bullet parameter to fill in the parameters of the moveToXY method. However, the sprites are created in the correct position but do not move at all. This snippet of code is within the create function but i have also tried using it in the update function with no luck.

 

I use the moveToXY function in  the below  code which doesnt involve a socket call and it works fine. The snippet below also includes the emit to the server with the bullet details. Any ideas why the sprite is rendered but does not move to the position i specify with the moveToXY call?

 

  if(!firstFired){
              activeBullets[socket.id] = [game.add.sprite(connectedSprites[socket.id].x - 8,connectedSprites[socket.id].y - 8, 'bulletSprite')]
              firstFired= true;
              }else{
                activeBullets[socket.id].push(game.add.sprite(connectedSprites[socket.id].x - 8,connectedSprites[socket.id].y - 8, 'bulletSprite'));
              }

              activeBullets[socket.id][activeBullets[socket.id].length-1].checkWorldBounds = true;
              activeBullets[socket.id][activeBullets[socket.id].length-1].outOfBoundsKill = true;
              
              game.physics.arcade.enable(activeBullets[socket.id][activeBullets[socket.id].length-1]);
            
              game.physics.arcade.moveToXY(activeBullets[socket.id][activeBullets[socket.id].length-1], game.input.mousePointer.x, game.input.mousePointer.y, 300);

socket.emit('bullet',
              {id:socket.id,
              x:activeBullets[socket.id][activeBullets[socket.id].length-1].x,
              y:activeBullets[socket.id][activeBullets[socket.id].length-1].y,
              xDest:game.input.mousePointer.x,
              yDest:game.input.mousePointer.y,
              num:activeBullets[socket.id].length-1
              });

 

Thanks

Link to comment
Share on other sites

I managed to find the issue. I'n the socket IO segment i never initialized the bullet.ID object to an array like I did in the lower segment of code. Instead,  I just immediately added an index. Initializing the object with an array seemed to fix it. Thanks 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...