Create grass at X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width()), Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height())
It should work, it picks a position between the Chunk’s X position (it is at the top left corner of the sprite) and Chunk’X Position + it’s width. So, if the Chunk’s X position is 100 and it’s width is 50. it will create the grass between X 100 and 150. It applies same for the height
That won’t quite work. Grass could end up with the origin on the very right of Chunk or below it. You’ll need to take the width of grass into account. After the grass object has been created, update it’s position as :
Set position of grass as X: RandomInRange(Chunk.X(), Chunk.X()+Chunk.Width()- grass.Width()), Y: RandomInRange(Chunk.Y(), Chunk.Y()+Chunk.Height()- grass.Height())
Hey, i just tried it out and wrote
x: sin(ToRad(RandomInRange(0, 360))) * RandomInRange(0, 10) and
y: cos(ToRad(RandomInRange(0, 360))) * RandomInRange(0, 10) but all it does is spawn the object in the top left corner of the screen everytime. Im kinda new to gdevelop so sorry if im making dumb mistakes.
Firstly, if you do it that way it’ll place the objects in a square. You need to put the random angle and random length into variables, and use those variables in the position expression.
As an example why, say that the x random angle is 0, and the y random angle is 90. Sin(0) = 0, and cos(90) = 0, so the position would be (0,0), and not within the circle you’re after.
To why your object is spawning at (0,0), you’ll have to make the correction I mentioned at the start of this post. I suspect it still won’t work, so please post a screen snip of the events that set the random angle & length, and where the object is created using those variables.
EDIT: So i realized my mistake, basically i didnt add the circles center to the calculations, i fixed it by just adding “+ circle_center” at the end. Thanks MrMen for the help you provided.