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
[import]uid: 12455 topic_id: 10442 reply_id: 38032[/import]