# How do I make an object stay within a certain radius of another?

0 favourites
• 15 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
• I'm creating a game where the player has to avoid red balls for as long as possible. The red balls are flying around the screen in random directions and to do this, I've used the bullet and wrap behaviours.

One of the power-ups in my game is a magnetic bomb. What I want to do is when the player collects that, they can drop the bomb with a second touch and red balls are attracted to it as it's magnetic. After a few seconds and when multiple red balls are surrounding it, it will explode and destroy them.

I've set the red balls' angle of motion to the bomb when it's created but I don't know how to do the following:

1. When the player gets the power-up, use a second touch to drop the bomb anywhere on screen.

2. When red balls get to the bomb, I want them to hover around it until the timer finishes and it explodes. At the moment, they fly away from the bomb after a second or so, how would I get them to remain within 100 pixels of the X and Y of the bomb?

I've attached an image to show what I'm trying to achieve here.

• Try 'pin at distance - rope style' with the pin behavior and see if that's what you're looking for.

• ## Try Construct 3

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

Construct 3 users don't see these ads
• That works great - thanks! Do you know how I would set it so that only the red balls within a radius of 100 are destroyed? At the moment it's destroying all of them even if they are the opposite end of the screen.

Do you know how I would drop the bomb to where the player touches? I'm using drag and drop to move the player so it's dropping it straight away at the X and Y of the player.

• Add the system condition pick (ball) by comparison.

Distance(ball.x,ball.y,bomb.x,.bomb.y)<100

I do not understand your second question. To create a bomb where the player touches, on touch start - create bomb at touch.x, touch.y. That doesn't have anything to do with moving your player.

• The user will already be touching the device as I'm using drag and drop to move the player.

How do I set it so that the bomb is dropped on a second touch and not the current one?

• Then you would use the On Nth touch start condition, and touch. Xforid(1) and yforid(1) expressions to get the x and y for the second touch.

• Thanks Oosyrag! It's working now :D

• I did a test, and indeed it seems that the coordinates of the second touch are not found, and as a result the new object is created at coordinates [0, 0].

Try to save the value of "Touch.XForID(1)" and check if it returns a correct value.

Edit:

Oh, I noticed you edited your previous post. So I take it that you were able to solve your problem. Great! ^^

• Yeah you're right - it was creating the object at 0,0 so I used Touch.XAt(1), Touch.YAt(1) which created the object at the X & Y of the second touch.

Cheers for your help guys! :)

• Oops sorry, forID() uses specific IDs per touch, not the index. So at() would be the right expression to use for the index.

• No problem! :)

Sorry to be a pain, I have another query now I'm wondering if you could help with. I have power ups (which give the player an advantage against red balls) and 'power downs' (which make the game more difficult for the player). I have 5 of each and they're activated for 10 secs when a player collides with specific balls. I'm currently using a global variable number to represent each power up/down and on collision with the balls setting the variable to round(random(1,5)) but it's not very effective as I'm finding that a lot of the same power ups/downs are appearing too often compared to others. E.g. The player collides with the power up ball and will get the same power up 3 times in a row - this is not what I want. The occasional repeat is fine but it's happening way too much currently.

How can I make it so that the power ups are more randomised?

• For the random number from 1 to 5, you need to modify your code. The solutions are different. You can solve with `floor(random(1, 6))`. This will correctly generate an integer number between 1 and 5.

The numbers are random, so it may be that sometimes the number 3 is repeated many times.

If you want to avoid this, before creating the power up, you could insert a control event: if there are already 3 balls of a certain power up, then create a ball of the opposite power up.

I hope this helps you.

• Thanks I'll try floor(random(1,6)) and see what happens.

I'm using the same ball and one global variable and trying to randomise the power ups this way. Should I be using advanced random for this?

• You can go either way.

If you want to use the Advance Random object then you can use its "Probability tables".

• OK thanks very much - I'll have a play around with probability tables. They don't look too difficult :)

• 15 posts