How do I pick a random spot on a sprite?

1 favourites
  • 7 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • Hello everyone,

    Not sure if this is possible or will be complicated. Basically, I have a regular square sprite.

    Nothing fancy, just a square colored blue for example.

    What I want to do is spawn an object at a random spot on that square. So if the square is 16x16, I'd be able to pick (0, 16), (1, 3), (4, 8), etc... points on that sprite and spawn an object there. I don't need to pick a specific spot, just a random one that is within the surface area of the sprite.

    Is that doable? If so, how?

  • for x random(Sprite.X-Sprite.Width/2,Sprite.X+Sprite.Width/2)

    for y random(Sprite.Y-Sprite.Height/2,Sprite.Y+Sprite.Height/2)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • for x random(Sprite.X-Sprite.Width/2,Sprite.X+Sprite.Width/2)

    for y random(Sprite.Y-Sprite.Height/2,Sprite.Y+Sprite.Height/2)

    Wow, that was super easy. I don't know why I didn't think of that. Thank you very much. For some odd reason, I thought it would be something built in and had a brain freeze when I didn't see it as a premade thing.

  • There is another method if your object is different from a square.

    You can spawn an object like this and check if it overlaps the spawn field, if so, leave it, if not, delete it and try to spawn it again. It is convenient to do it through a function. For example, you can create pimples on the face where the form of of collision is complicated.

  • There is another method if your object is different from a square.

    You can spawn an object like this and check if it overlaps the spawn field, if so, leave it, if not, delete it and try to spawn it again. It is convenient to do it through a function. For example, you can create pimples on the face where the form of of collision is complicated.

    I've also done that before. Funny enough though, it's theoretically speaking a terrible solution, because it can happen that the object just never spawns in a good spot and your program hangs. Practically though it's just never going to happen... but it can happen. I always assumed there must be a solution for this "spawn on object" that doesn't involve this kind of trial and error approach, but I also never bothered to really look into it.

  • There will be no hangs, the worst thing is that two objects may be created.

    Of course, you can create a protection, for example, check for the number of objects or limit the number of attempts to create an object.

    But yes this is not a good solution, because the created objects may be unnecessary.

    There is another option to define random coordinates on the object and pick the object by them, if the expression is true then only then create an object there.

  • Yes I should emphasize it is extremely unlikely that it hangs, like 1000 lottery wins in a row unlikely. But if the object gets created in the wrong spot a trillion times in a row, the game will hang. Limiting the number of attempts can in turn lead to an object not spawning if after 1000 attempts it never got the right spot (which you could add a failsafe for if it runs out, just spawn on a predefined spot)

    It's just that my brain complains that this seems like such an "incorrect" way to do it.

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