How do I set a sprite to random positions in the layout?

0 favourites
  • 9 posts
From the Asset Store
10 Beautiful Pokemon-like Building Pixel Art Sprite Sets
  • How do I set a sprite to random positions on the layout?

    Technically, on the start of the layout, the sprite will spawn in a random position on the layout. also I want there to be a select of postions and construct will randomize where to put the sprite in these positions. For example if the layout starts then the sprite will spawn randomly on one of the 3 positions I want it to.

    Please help! I'm new to construct lmao.

  • If you want the sprite truly anywhere on the layout, you can set the position to:

    random(layoutWidth)

    random(layoutHeight)

    Unless your layout is entirely open, as in no solids or areas sprites shouldn't be (walls, for example), you probably don't want this.

    If you want to do more of a concept of spawning zones, I would make spawn zone sprites. Set the sprites to invisible, so that you can see them in the editor but not while playing.

    In the condition part of your action, use the System condition (select System, not the sprite), and use "Pick Random Instance". This will pick a random spawn zone sprite each time. (if new, you might want to read about "picking" in events)

    If you want them in random spots in the spawn zone, you can use the same concept as I started off with, using set position to:

    random(spawnZone.BBoxLeft, spawnZone.BBoxRight)

    random(spawnZone.BBoxTop, spawnZone.BBoxBottom)

    I do a similar thing, except for movement instead of spawning, in this game: loubagel.com/games/sunnyside-smash

    I wanted the characters to move around randomly, but there are areas sprites can't go - in the water, in solid objects such as houses, and areas I don't want them to be such as right in front of the house doors. So I made a sprite object using the same concept I explained above. I covered the map of areas I wanted them to go with instances of this sprite. The nice thing was that they didn't have to be the same size - if there was a big wide open area, I could stretch one instance of this sprite over it all.

    When a character is ready to move a random "move zone" sprite is picked. Then, a random coordinate inside that sprite is picked. Then I just use the pathfinding behavior to move them to that coordinate.

    I know the example is a bit off topic, but hope it helped.

  • hey, don't really understand this lol

    could you please show an example? sorry

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Do these help?

    Like I said, I'm using pathfinding here, but same concept anytime you are picking an x,y location.

  • Oh, I switched it from using to Border box properties (BBox). Can't remember why, but important to know if following what I did in the images:

    The origin point for my pathfindingZones sprites are in the top left corner. If the origin is anywhere else that math wouldn't make sense.

  • Could u like put values in the spaces where they are meant to be? bit confusing lol

  • I'm not sure what you mean.

    Those images are screenshots of my code, which works to be clear. So not sure why I would change out any values.

  • Create a sprite called "spawner". Make it invisible if you don't want to see it and place it in the spots you want to potentially spawn from on your layout.

    At start of layout use the Pick Random Object Instance condition for your "spawner" sprite.

    Then use the "Create Object" action to create your sprite at spawner.X & spawner.Y.

    This will create "spawn" your sprite at one of the random "spawners" you have positioned on the layout

  • Thanks a lot! I'll use this in a lot of my games. You're a saint bro

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