2 questions. timers and collisions

First off, Thanks to Danny for giving me much needed help and examples. They make a difference in understanding things.

Now, enough butt kissing :slight_smile:

Here are my 2 problems:

  1. I have some code that spawns diamonds. The problem being that they will not spawn without me clicking a button. I would like the spawned images to appear automatically ( I’m assuming a timer.
    I’ve tried different timers in different areas but nothing I do works. Any help on this appreciated.

Here is my spawn code:

local function spawndiamond( event )  
 local phase = event.phase  
 if "ended" == phase then  
 audio.play( popSound )  
  
 displays number of different images  
randImage = diamondGfx[math.random( 1, 4 )]  
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )  
local diamond = alldiamonds[#alldiamonds]  
 diamond.x = 60  
 diamond.y = 50  
  
diamond.xScale = 1.8; diamond.yScale = 0.8  
transition.to(diamond, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic })   
physics.addBody( diamond, { density=0.0, friction=0.0, radius=20.0, bounce=0.0 } )  
diamond.linearDamping = 0.4  
diamond.angularDamping = 0.6  
  
local spawndiamond = timer.performWithDelay( 500, diamond, 6 )  
  
local diamondmove = diamond  
physics.addBody( diamondmove, { density=1.5, friction=0.6, radius=00.3, bounce=0.3 } )  
diamondmove.x=40  
diamondmove.y=60  

Second question:
The way my code is set up ( Thanks again Danny) is that when I click on a diamond and then click on a different area of the screen, the diamond moves to the new area. That works great.
However, is it possible to click on a diamond and then click on a second diamond and have the original diamond replace the second diamond? Hope it makes sense. Thanks.

[code]
local diamondmove = diamond—MOVE DIAMOND FROM ONE POINT TO ANOTHER
physics.addBody( diamondmove, { density=1.5, friction=0.6, radius=00.3, bounce=0.3 } )
diamondmove.x=40
diamondmove.y=60

local targetToMove = nil
local function setTarget(event)
local target = event.target
targetToMove = target
return true
end

local function movediamondmove(event)
if targetToMove ~= nil then
targetToMove.x = event.x
targetToMove.y = event.y
targetToMove = nil
end
return true
end
diamondmove:addEventListener(“tap”, setTarget) [import]uid: 127842 topic_id: 22444 reply_id: 322444[/import]

For your spawn code you need something like this.
display.setStatusBar( display.HiddenStatusBar )

_W,_H = display.contentWidth, display.contentHeight

local sqW = 10
local sqH = 10

local myX = _W / 2
local myY = _H / 2
local color = 100
local function main()

local function spawndiamond()

myX = myX + 10
myY = myY + 10
sqW = sqW + 10
sqH = sqH + 10
color = color + 30
randImage = display.newRect(myX, myY, sqW, sqH)
randImage:setFillColor(color, 255, 0)

end

local spawndiamondTimer = timer.performWithDelay( 500, spawndiamond, 6 )
return true

end

main()

Your timer needs to be outside your function. Just put this come in a main.lua to test.

I’m not sure what your asking on your second issue. [import]uid: 80890 topic_id: 22444 reply_id: 89510[/import]

Thanks. I’m gonna start working with that.
My second question:
Right now I have an object (diamond) that when I click on it and then click on another area of the screen, the diamond moves to that new location.
What I’m looking to accomplish is that if there is already a diamond in the new location, how could I have the original diamond replace it.

Another way:
I have one red square and one green square. I want to be able to click on the red square and then on the green square. The original red square will disappear and the green square will turn into the red square. [import]uid: 127842 topic_id: 22444 reply_id: 89513[/import]

This post might help you on your second issue

http://developer.anscamobile.com/forum/2011/01/18/change-image-touch [import]uid: 80890 topic_id: 22444 reply_id: 89518[/import]

weberkeith,
Thanks for the code but I think that it’s a little over my head to understand.

I thought I could just make a call to spawndiamond and set it to a timer… [import]uid: 127842 topic_id: 22444 reply_id: 89532[/import]