Jump to content

player sprite not starting at correct coordinates (and I'm not sure why)


Yacob
 Share

Recommended Posts

So, I've been working on a game with an overhead perspective, using Tiled maps to make the environment. Some of my sprites aren't tiles, so to make placing them easier, I made a Tiled object containing the coordinates for the player's starting position. My player is a boy in a kayak, so I want that kayak to appear at that coordinate, as well as a sprite of a wooden dock. In create(), I find search the object layer for that "start box" object, store it in a variable, then set positions of my sprites with "startbox.x" and "startbox.y". When I draw the dock, it appears in the place on the map where it should be. However, when I give these values to my movable player, he almost always spawns way at the other end of the map. I say almost always because--mystifyingly enough--a few times some other people that tried my game had reported that the player began next to the dock, but I haven't experienced that, and it was rare for those select players as well.

 

I don't think it's an issue with how I'm getting the map object. I put in the literal coordinates into the player, and I still got the same problem. As soon as I put in coordinates that go over 2000, the sprite seems to start going backwards. Why is this only happening to my player? And how is it that it appeared correctly a handful of times when most of the time it didn't?

Link to comment
Share on other sites

I recall experiencing same issue when teaching students Phaser/Tiled. In that instance, because of time limits, we just did a workaround (coded explicit coords), but am interested in the correct solution. If you haven't yet, have a look inside the exported Tiled JSON file for clues. Perhaps it's a matter of parsing of the right data and setting player.x/y.

Link to comment
Share on other sites

So my player is supposed to spawn near the top right corner of my map. Specifically (3407.57, 214.42) since that's where my "Start" object is on the map.

 

I search for the object like this:

 map.objects['Meta Layer'].forEach(function(element){            if (element.name === "Start")                startBox = element; //contains coords for where dock should be        });

Then I create my player like this:

//player spriteplayer = game.add.sprite(startBox.x,startBox.y,'boat_anim');player.anchor.setTo(.5,.5);
Link to comment
Share on other sites

Since you're using Tiled... did you resize your physics world? Something like "layer.resizeWorld();" somewhere in your largest layer? Is there any physics code at all for the player in the update method?

 

My general tactic for stuff like this is to start reducing: no physics. Don't create the object from the map (i.e. not in your forEach or your map loading code). Don't animate. What if you drew the position directly using a debug method?

 

You say the dock is fine – is the dock a sprite with physics on it?

 

It might help to see your game, if you're down with that.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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