[Resolved] Random spawning and movement of an object

Hi,
I am to trying code a game where ‘objects’ spawn in random locations and move about the screen in a random fashion, unaffected by gravity (similar to the characteristics of a bubble.)

I have been able to make the ‘object/s’ spawn in random locations, but they appear exclusively within the confines of the device screen… is there a way to make them spawn outside the view in random locations, and in a “weightless” type motion float into the view? [import]uid: 148623 topic_id: 27457 reply_id: 327457[/import]

I’m unsure on the type of movement you want however likely a transition with easing.

For spawning outside the screen, certainly - are you using math.random for the x and y of the newly created objects? [import]uid: 52491 topic_id: 27457 reply_id: 111636[/import]

Thanks for responding!

I want the ‘objects’ to float in and out of the view, constantly moving in a fluid&random pattern unrestricted by gravity.

Yes, I am using the math.random for the x and y, but I forgot how to increase the area in which they spawn…:]

Sorry for the newbish question. My programming experience extends to nothing more than simple coding in JAVA. [import]uid: 148623 topic_id: 27457 reply_id: 111704[/import]

Hey again :slight_smile:

RE math.random for your spawning area, do so like this;

myObj = display.newImage(“whatever.png”)
myObj.x = math.random(1, 320)
myObj.y = math.random(1, 480)[/lua]

That is assuming an iPhone screen in portrait, which would be 320 wide by 480 wide and then objects could spawn anywhere on the screen.

RE floating, you’d want to use math.random and transition.to to move them randomly about - have you tried this yet? :slight_smile:

Peach [import]uid: 52491 topic_id: 27457 reply_id: 111915[/import]

Thank you!
lol I could not remember how to do that. :]

I tried using the transition.to, but the objects just appear at the top left of the screen? O.o
Basic, I know :] I wanted to start as simple as I could.
Here is my code:
[lua]

–Constants
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
b = 0;
time_remain = 10;
time_up = false;
total_bubbles = 30;
ready = false;

display.setStatusBar(display.HiddenStatusBar);
–prepare sound/s to be played when called
local pop_sound = audio.loadSound (“media/bubble_pop.caf”);

–adding physics
local physics = require (“physics”);
physics.start();
physics.setGravity(0.0 , 0.0)
local function trackBubbles(obj)
obj:removeSelf();
end

–Spawning multiple objects in randoms locations
local function spawnBubble()

local bubble = display.newImageRect(“images/smallbubble.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
transition.to( bubble, { time = 1500, delay = 2500, x=mRand, y=mRand, onComplete = listener});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22});

function bubble:touch(b)
if (b.phase == “ended”) then
–play pop sound
audio.play(pop_sound);
–Remove bubbles
trackBubbles(self);
end

return true;
end
–Adding touch event
bubble:addEventListener(“touch”, bubble);
end

tmr = timer.performWithDelay(90, spawnBubble, total_bubbles);

–Background image
local background = display.newImage (“images/landscape.png”);

–Floor image
local floor= display.newRect (0,0,_W,20);
floor:setReferencePoint(display.CenterReferencePoint);
floor.x = _W/2; floor.y = 489.5;
physics.addBody(floor, “static”, {friction = 0.4, bounce = 0.3});
–physics.setDrawMode( “debug” ) – shows collision engine outlines only
–physics.setDrawMode( “hybrid” ) – overlays collision outlines on normal Corona objects
–physics.setDrawMode( “normal” ) – the default Corona renderer, with no collision outlines
[import]uid: 148623 topic_id: 27457 reply_id: 111946[/import]

Line 6 you have;
[lua]mRand = math.random[/lua]

Change it to;
[lua]mRand = math.random(1,300)[/lua]
Or the like. That would be a value from 1 to 300.

Also you may wish to add at the start of your code;
[lua]math.randomseed(os.time())[/lua]
Else your random numbers will repeat the same pattern each time.

Lastly, in your transition.to, you use mRand but don’t change it - so it would be set at the start as a random number and then keep that number throughout - so all bubbles would move to the same place.

To fix this you should add;
[lua]mRand = math.random[/lua]
again right before you do the transition.

Are you coding with the terminal open? I suspect you may have gotten an error message from the above code, so if you aren’t running it you should start - it’s very useful :slight_smile:

Peach [import]uid: 52491 topic_id: 27457 reply_id: 111962[/import]

Here I used this to do a similar thing
This should work how you want it to, If you want it to spawn / move outside the screen also change the math.random’s to go less then 0 and in x > 320 and y > 480

[code]

local speed = 1000
local enemy = display.newCircle( 100, 100, 17 )
enemy:setFillColor(0,255,255)
enemy.x = 20 --you could make this random to I guess
enemy.y = 20
enemy.myName = “enemy”

local function move()
transition.to(enemy, { time = speed, x = math.random(320), y =math.random(480), onComplete = move}) – like change math.random to x = math.random(-100,420), y = math.random(-100,580)
end
timer.performWithDelay(1,move)
physics.addBody(enemy,{radius = 17})
–sorry if this wraps weird in the post :frowning: [import]uid: 113909 topic_id: 27457 reply_id: 112057[/import]

Well, I tried what you suggested (Peach) and it sort of does what I want…
I have it set to spawn 30 bubbles, which it does, but they all spawn in the upper left corner, then move to a specific point, and stop. O.o

I am trying to get it to where all the bubbles are ‘floating’ around the screen, and do not stop until they are ‘popped’.
And when I say ‘floating’ I mean each bubble moves around in a random fluid motion, coming in and out of the view etc… similar to the random nature of a real bubble.

I changed line 6 to what you suggested and added the os.time bit of code.
For the transition.to I wasn’t quite sure what you meant, but this is what I did:

[lua] local function spawnBubble()

local bubble = display.newImageRect(“images/smallbubble.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
transition.to( bubble, {mRand = math.random, time = 1500, delay = 2500, x = mRand, y = mRand, onComplete = listener});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22}); [import]uid: 148623 topic_id: 27457 reply_id: 113843[/import]

You are still using “math.random” rather than math.random(1,100) for example, you need to define a range the number can fall in. Make sense?

For moving it when the transition is over I see you have onComplete=listener but I cannot see where listener is defined in your code. [import]uid: 52491 topic_id: 27457 reply_id: 113912[/import]

Oh yes, now I get it. :slight_smile:

I made some changes and it seems to be doing what I want, for the most part… now they spawn and each one moves to a random position within the view. My only issue is once they get to that position they just stop.
Is there a way I can code to where just before they stop the code generates a different position?
(Sorry if this is worded poorly, I was having trouble describing what I am trying to achieve.)

These are the changes I made:
[lua]
local function spawnBubble()

local bubble = display.newImageRect(“images/smallbubble.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
bubble.x = math.random(-321, 600);
bubble.y = math.random(-481, 400);
transition.to( bubble, {mRand = math.random(1000,4000), time = math.random(1000, 4000), x = math.random(1,310), y = math.random(1,470), onComplete = listener});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22}); [import]uid: 148623 topic_id: 27457 reply_id: 113995[/import]

Try running this but replace smile.png with your own bubble image - it should do the trick;

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

local function listener(me)
transition.to(me, {time = math.random(1000, 4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(me)end})
end

local function spawnBubble()
local bubble = display.newImageRect(“smile.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
bubble.x = math.random(-321, 600);
bubble.y = math.random(-481, 400);
transition.to( bubble, {mRand = math.random(1000,4000), time = math.random(1000, 4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(bubble)end});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22});
end
timer.performWithDelay(1000, spawnBubble, 3)[/lua]

Let me know how that goes for you.

Peach :slight_smile: [import]uid: 52491 topic_id: 27457 reply_id: 114038[/import]

I used that, but got an error saying " main.lua:63: ‘’ expected near ‘end’ "
When I comment the line out, the bubbles show up, but any code after does not get implemented.

In this case the problem is on line 43:
[lua]local physics = require (“physics”);
physics.start();
physics.setGravity(0.0 , 0.0)
local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(me)end});
end
local function trackBubbles(obj)
obj:removeSelf();
end

–Spawning multiple objects in randoms locations
local function spawnBubble()

local bubble = display.newImageRect(“images/smallbubble.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
bubble.x = math.random(-321, 600);
bubble.y = math.random(-481, 400);
transition.to( bubble, {mRand = math.random(1000,4000), time = math.random(1000, 4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(bubble)end});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22});

end
tmr = timer.performWithDelay(1000, spawnBubble, total_bubbles);
function bubble:touch(b)
if (b.phase == “ended”) then
--play pop sound
audio.play(pop_sound);
--Remove bubbles
trackBubbles(self);
end

return true;
end
--Adding touch event
bubble:addEventListener(“touch”, bubble);
end

–Background image
local background = display.newImage (“images/landscape.png”);

–Floor image
local floor = display.newRect (0,0,_W,20);
floor:setReferencePoint(display.CenterReferencePoint);
floor.x = _W/2; floor.y = 489.5;
physics.addBody(floor, “static”, {friction = 0.4, bounce = 0.3});

–sorry if it shows up a little weird [import]uid: 148623 topic_id: 27457 reply_id: 114109[/import]

It was looking for the end of a line before you wrote end. In other words you have too many ends.

Try this - as usual put your own images back in;

[lua]local physics = require (“physics”);
physics.start();
physics.setGravity(0.0 , 0.0)

local function listener(me)
transition.to (me, {time = math.random(1000,4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(me)end});
end

local function trackBubbles(obj)
obj:removeSelf();
end

–Spawning multiple objects in randoms locations
local function spawnBubble()

local bubble = display.newImageRect(“smile.png”, 45, 45);
bubble:setReferencePoint(display.CenterReferencePoint);
bubble.x = math.random(-321, 600);
bubble.y = math.random(-481, 400);
transition.to( bubble, {mRand = math.random(1000,4000), time = math.random(1000, 4000), x = math.random(1,310), y = math.random(1,470), onComplete = function()listener(bubble)end});
physics.addBody(bubble, {density = 0.1, bounce = 1.3, friction = 0.3, radius = 22});

function bubble:touch(b)
if (b.phase == “ended”) then
–play pop sound
audio.play(pop_sound);
–Remove bubbles
trackBubbles(self);
end

return true;
end
–Adding touch event
bubble:addEventListener(“touch”, bubble);
end
tmr = timer.performWithDelay(1000, spawnBubble, total_bubbles);

–Background image
local background = display.newRect( 0, 0, 320, 480 )
background:setFillColor(0,200,255)

–Floor image
local floor = display.newRect (0,460,320,20);
floor:setReferencePoint(display.CenterReferencePoint);
–floor.x = _W/2; floor.y = 489.5;
physics.addBody(floor, “static”, {friction = 0.4, bounce = 0.3});[/lua]

Let me know how that goes for you :slight_smile:

Peach [import]uid: 52491 topic_id: 27457 reply_id: 114190[/import]

Yup, that was the problem. :slight_smile:
Now it seems to be doing exactly what I want! : D

Thank you so much for your assistance!

  • Saer [import]uid: 148623 topic_id: 27457 reply_id: 114233[/import]

Not a problem Saer, always happy to help.

Marking as resolved!

Peach :slight_smile: [import]uid: 52491 topic_id: 27457 reply_id: 114281[/import]