Try this, I’ve never really used collisions before but this should work.
Collision API document is here if you want to check through it.
http://developer.coronalabs.com/content/game-edition-collision-detection
[lua]–new spring code
_W = display.contentWidth
_H = display.contentHeight
–Our variable that contains number of springs
local springCount = 0
–Our button,that needs to be touched
local button = display.newImage( “springicon.png” )
group:insert(button)
button.x = display.contentWidth / 1.113
button.y = display.contentHeight / 13.2
– bounce function
local function bounce(obj,minScale,maxScale,time)
local oldXScale,oldYScale = obj.xScale, obj.yScale
local minScale = minScale or 0.8
local maxScale = maxScale or 1.2
local time = time or 100
if not obj.isBouncing then
obj.isBouncing = true
transition.to(obj,{time=time,xScale=maxScale,yScale=minScale,transition=easing.inQuad,onComplete=function()
transition.to(obj,{time=time,xScale=minScale,yScale=maxScale,transition=easing.outQuad,onComplete=function()
transition.to(obj,{time=time,xScale=oldXScale,yScale=oldYScale,onComplete=function() obj.isBouncing = false end})
end})
end})
end
end
–Function to move spring, use later in another function
local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
if event.phase == “began” or event.phase == “ended” or event.phase == “cancelled” then
bounce(event.target)
end
end
– collision listener function
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
bounce(self) --if this doesnt work, bounce(event.target) or bounce(spring)
end
end
–Function to create string at needed x and y; add 1 to springCount per created string
–and add event listener to created string
– colision function
function create_string()
if springCount < 1 then --how many springs you can add
local spring = display.newImageRect( “Spring.png”,50,50)
group:insert(spring)
physics.addBody( spring, “static”, { density=1, friction=0, bounce=1 } )
spring.x = 400
spring.y = 130
springCount = springCount + 1
spring:addEventListener(“touch”, move_spring)
– Adds collision listener to spring
spring.collision = onLocalCollision
spring:addEventListener( “collision”, spring)
elseif springCount == 1 then–how many springs you can add
print(“stop”) – there you can do anything you want, i just used print for debugging purposes
end
end
–With this function we touch the button and it changes its alpha and use create_string()
local function touch_button(event)
if event.phase == “began” then
print(“began”)
button.alpha = 0.5
elseif event.phase == “ended” then
print(“ended”)
button.alpha = 1
create_string()
end
end
– At last we adding event listener to button and watch magic happens:)
button:addEventListener(“touch”, touch_button)
—end new spring code[/lua] [import]uid: 62706 topic_id: 34718 reply_id: 138245[/import]