Issue with random movement

I am trying to create a group of money objects that will generate randomly and continuously move to different locations across the screen.

\_W = 320  
mRand = math.random  
  
local money = {}  
  
local function addMoney()  
 money[#money + 1] = display.newImage("images/money.png")  
 money[#money].x = mRand(60, \_W -60);  
 money[#money].y = 20;  
 money[#money].rotation = mRand(0,360);  
 money[#money].name = "money"  
 --physics.addBody(money[#money],{density=1.0, friction=0.5, bounce=.5})   
 moveMoney()  
end  
  
timer.performWithDelay(500, addMoney(), 25)  
  
local function moveMoney()  
 print ("move money")  
 transition.to(money[], {time = 1000, y = mRand(5,80), x = mRand(5, \_W-5)})  
end  

First off with this code for some reason the move money function will not work.

In addition how do I specify the specific money object that is being modified in the move money function, and how can I get the move money function to run again whenever the transition is ended?
Also if there would be a better way of running this, I just want a constant random movement until some later collision [import]uid: 59183 topic_id: 10442 reply_id: 310442[/import]

Here’s an example. I don’t know if this is what you’re looking for, but it might give you an idea.

Have fun and good luck.
Make sure you read all the notes in the code.

Note: this may not be the most practical.

[lua]local money = {}
– collision for the drop box
– this is for testing only
local onCollide = function (self, event)
if event.phase == “began” then
self:removeSelf()

– set the flag for the coin that comes in collision
– with the drop box

event.other.washit = true

– check the money table for coin that need to be remove
– because of collision
for i = #money, 1, -1 do

local oldCoin = money[i]

– see flag above
if oldCoin.washit then
oldCoin:removeSelf()
table.remove(money, i)

print("money now has: "…#money)
end
end
end
end

– testing box
local createBox = function()
box1 = display.newRect(0,0,50,50)
box1:setFillColor(0,40,200)
box1:translate(mRand(20,300), 20)
physics.addBody(box1, “dynamic”, {density = 1, friction = 0.2, bounce = 0.4})

box1.collision = onCollide
box1:addEventListener(“collision”, box1)

end

– create the money/coins
local theMoney = function ()

local x = mRand(30,290)
local y = mRand(30,450)

local move1
local move2
local coinTran
local coinTran1
local coinTran2

– keep the coin local
local coin = display.newCircle(0,0,20)
coin:setFillColor(mRand(0,255),mRand(0,255),mRand(0,255))
coin:translate(mRand(30,290),mRand(30,450))

– create the was hit flag for each coin
coin.washit = false

physics.addBody(coin, “dynamic”, {density = 0.2, friction = 0.1, bounce = 0.5})

money[#money + 1] = coin
– test if the table is being populated
print("Total coin in money: "…#money)

if coinTran then transition.cancel(coinTran) end

move2 = function (obj)
local obj = obj

local x = mRand(30,290)
local y = mRand(30,450)

– cancel transition for the hit coin
if obj.washit == true then

– check to see if there’s any transition for the hit coin
– if there’s any transitions then cancel it.
if coinTran2 then transition.cancel(coinTran2) end
if coinTran1 then transition.cancel(coinTran1) end

else
coinTran2 = transition.to(obj, { time = 3000, x = x, y = y, onComplete = move1})
end
end

– transition part 2
move1 = function (obj)
local obj = obj

local x = mRand(30,290)
local y = mRand(30,450)

– check if coin is hit
if obj.washit == true then
– check to see if there’s any transition for the hit coin
– if there’s any transitions then cancel it.
if coinTran2 then transition.cancel(coinTran2) end
if coinTran1 then transition.cancel(coinTran1) end

else

coinTran1 = transition.to(obj, { time = 3000, x = x, y = y, onComplete = move2})
end
end

– initial transition
coinTran = transition.to(coin, {time = 3000, x = x, y = y, onComplete = move1})
end

local tTimer = timer.performWithDelay(100, theMoney, 25)
local t2Tiemr = timer.performWithDelay(5000,createBox, 0)[/lua]
check out my game:

Touch Cannon – a **FREE GAME**

please rate :slight_smile: [import]uid: 12455 topic_id: 10442 reply_id: 38032[/import]

I am trying to figure out the example that you posted, while it prints the two initial objects nothing further seems to happen.

It would be much appreciated if I could get any help figuring out how to get this code to run. [import]uid: 59183 topic_id: 10442 reply_id: 38101[/import]