Player Spawning at Different Points

0 favourites
  • 13 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • I'm having trouble getting my player sprite to spawn correctly.

    The set up is like a multi-room house where each layout is a room. If you walk to a door it changes layouts to the room you walked to. Each room has multiple doors, so when it loads a new layout the game should spawn the player sprite at door leading to the room you just exited.

    Here's what I tried: I have an exit sprite with an instance variable called "LeadsTo" that has the layout name that it leads to. When the player sprite collides with the exit sprite, it calls a function that uses that instance variable to know which layout to load. That part works fine. In "On Start of Layout" I have it spawn a player sprite at try to position it on the exit sprite that has the instance variable with the same value as the layout that the player just left. Nothing is spawning.

    I'm not at a computer with Construct 2 right now, so I can't show exactly what I did.

    Anybody have any ideas? I can't make one layout cover all rooms because that is just an example and was the easiest analogy for what I'm trying to accomplish.

  • Here's a screenshot of what I'm doing:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Are you creating the player object at the start of the layout, or is it on a global layer?

  • At first I had it just on the first level on the game layer (not global) and not on the second level. Spawning it at the exit wasn't working, so I'd have a sprite on the first level but not the second. Then I tried having the sprite on each level and now at least you can play each level, but it isn't spawning in the correct location except for the initial load into the first layout. Subsequent loads into that layout start where the sprite is placed, not where the exit is.

    Does that answer your question? Sorry, my mind is fried right now after work.

  • Without being able to look at a capx of what's going on it's almost impossible to diagnose what's going on from a screen of some events. It's not clear where your CurrentLevel variable is set... If that is after the position function then that could explain why it's not working as you hope.

  • CurrentLevel is a global variable

  • As you have the events, when you go to a different layout from Layout1, you have not recorded where you came from, so on the next layout you try to create an object at Exit.LeadsTo where neither LeadsTo equals the previous layout's name. You could use On End of Layout and store the current layout's name in a variable, then compare that variable to the Exit.LeadsTo in the create function. There's another problem you will encounter when you fix that issue - when you create a player on top of an exit, the on collision trigger will fire and you'll end up travelling to a different room, only for the player to be created on top of an exit again, and so on, every tick...

  • Hi!

    I´m actually trying to do the same thing, and what I´ve done and which seems to work ok so far (even if I´m sure there is a smarter way of doing it), is something like this (pretty close to what you have done)

    I have two global variables: DestinationX and DestinationY

    I have an "Exit Sprite" with three instance variables: nextX, nextY and NextLayout.

    I place the sprite where I want an exit to be.

    Then I set the variables in that sprite instance to where I want the player to "teleport" to.

    Then I have an event saying something like (if I recall correctly):

    Player -On collision with ExitSprite -> set DestinationX to ExitSprite.NextX

    -> set DestinationY to ExitSprite.NextY

    -> go to Layout(ExitSprite.NextLayout)

    And on each layout I have an event saying:

    On start of layout->create player at DestinationX, DestinationY

    Also, I noticed that I had to have the player sprites (I have a hitbox, a "movebox" and the actual animated sprite) on a separate layout not used in the game to avoid duplicate players to spawn in the layouts.

  • As you have the events, when you go to a different layout from Layout1, you have not recorded where you came from, so on the next layout you try to create an object at Exit.LeadsTo where neither LeadsTo equals the previous layout's name. You could use On End of Layout and store the current layout's name in a variable, then compare that variable to the Exit.LeadsTo in the create function. There's another problem you will encounter when you fix that issue - when you create a player on top of an exit, the on collision trigger will fire and you'll end up travelling to a different room, only for the player to be created on top of an exit again, and so on, every tick...

    I actually have a CurrentLevel which holds where the player left from and an EnteringLevel (both global variables), and the Exit.LeadsTo is supposed to match the CurrentLevel, which the player just left. I can't check the capx right now, so I'm not sure if that is showing that. I did run into the problem you described once and it kept bouncing me back and forth between two layouts. I didn't know what was happening, so thank you for that explanation, it makes perfect sense!

  • Hi!

    I´m actually trying to do the same thing, and what I´ve done and which seems to work ok so far (even if I´m sure there is a smarter way of doing it), is something like this (pretty close to what you have done)

    I have two global variables: DestinationX and DestinationY

    I have an "Exit Sprite" with three instance variables: nextX, nextY and NextLayout.

    I place the sprite where I want an exit to be.

    Then I set the variables in that sprite instance to where I want the player to "teleport" to.

    Then I have an event saying something like (if I recall correctly):

    Player -On collision with ExitSprite -> set DestinationX to ExitSprite.NextX

    -> set DestinationY to ExitSprite.NextY

    -> go to Layout(ExitSprite.NextLayout)

    And on each layout I have an event saying:

    On start of layout->create player at DestinationX, DestinationY

    Also, I noticed that I had to have the player sprites (I have a hitbox, a "movebox" and the actual animated sprite) on a separate layout not used in the game to avoid duplicate players to spawn in the layouts.

    So you're recording the X and Y coordinates of the destination and just sending the sprite there instead of onto an object? I'll give that a shot when I get home, thanks. I have had the duplicate players issue, so I'll move them all do a separate layer also. Thank you!

  • I should mention that I set the x and y to be slightly on the side of next level's exit object, to avoid the infinite teleporting. But I suppose it's actually more versatile to have a destination object ( like you do) instead of setting the x and y. That way if you change the level you don't have change the coordinates in a lot of places, but rather just move the object.

  • I should mention that I set the x and y to be slightly on the side of next level's exit object, to avoid the infinite teleporting. But I suppose it's actually more versatile to have a destination object ( like you do) instead of setting the x and y. That way if you change the level you don't have change the coordinates in a lot of places, but rather just move the object.

    Yeah, that versatility and making level-building easy was what I was going for, but if I can't get that to work, at least your method works!

    Although, you could probably set something up so that when the layout loads, or on start of layout, each exit variable is set to whatever the sprite's X and Y coordinates are, so it is automated like my method but thorough enough to work like your method...

    EDIT: Ignore the preceding paragraph because I'm an idiot.

    Also, I got it to work using mindragon73's method, but adding a variable called ExitAvailable that is made false on start of layout, and then made true once the player is no longer overlapping an exit sprite. Thanks everybody!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)