[Solved]How do I For Each Family Member

0 favourites
  • 6 posts
From the Asset Store
A collection of various zombie characters sprites for creating a 2D platformer or sidescroller game
  • Hey guys,

    First, let me set the scene. I am making a 2D RPG. There is a player and there are monsters. Each Monster is stored in a Family called Monsters.

    So my issue is... When a Player is below a Monster and they walk up to said monster, the Player sprite is on top of the Monster (based on Z-order) which is what I want, but, if the player is above and walks down to the monster, the Player sprite once again is on top instead of below the monster sprite (based on Z-order). I created a quick fix by using "Every .5 seconds > Compare Monster.Y to Player.Y and Move them accordingly. Unfortunately this changes every Monsters Z-order. For now this is fine because the player can only be in one spot at one time but, down the line, I know I am going to have issues with this and I would like to fix it now.

    I am considering using "For Each" with the Family Monsters Object. Will "For Each" apply to each value inside the Family or will it apply changes to the entire Family at once. Do you have any suggestions for how to handle this?

    If this has been answered, I apologize. Searching for "For" and "Each" is considered too common by this website, haha.

  • I think you won't need to use "For" or "Each" for this. You could create a variable to record the monster's UID. I just tested it and it works for families too. So you can apply your events only on that monster by selecting its UID.

  • Tekniko

    I would check which monsters are overlapping the player, and only move those... or maybe an invisible sprite two or three times bigger than the player called "MosterDetector" and use that to select only the monsters near the player.

    Then if the the moster's Y is greater than player's Y then move the monster in front of player.

    if the monster's Y is less than player's Y then move the monster behind the player.

    that way you don't have to do any looping - just pick monsters overlapping, check the Y coordinates and move the z-order accordingly. Using that method, if your player walked through a whole group of monsters he would pass through them correctly (any higher up the screen would be behind him, and any farther down the screen would be in front).

  • The problem is, I don't know how to refer to each individual monster. The way I have it set now, they either all are above the Player or all of them are below.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Tekniko - what I was saying is use "Is Overlapping Another Object" to select only monsters near your player. The only thing I would keep an eye on is generating too many collision checks. You don't have to refer to each monster individually (you can loop through picked monsters if you want), but you can process them in groups: all monsters near player get z-order updated, all monsters outside the area you are checking get ignored. So, what you do is: pick monsters near player, then in a sub event pick monsters with Y less than play and set their z-order behind player, else set z-order in front of player. (the "else" clause would only select the near by monsters that don't have Y less than player)

    Since it sounds like you are talking about a relatively small number of monsters at any one time, you could also use "is on-screen" to select only visible monsters. Or compare X coordinates: pick monsters where abs(player.x - monster.x) < 200 (abs is the absolute value function - that would select monsters within 200 pixels to the left or right of player).

    So, that is three ways to only select monsters near the player and omit other monsters on the layout...

  • As soon as you said, "Is overlapping" I saw the solution in my head. Sometimes I just draw a blank on how to fix a problem. Thank you!

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