Spring not so springy

I’ve created a button that generates a spring. My problem is that my spring looks and acts like a spring but once an object lands on the spring it looks static. IE not bending/contracting like a spring would. Not sure what to add to make it work correctly.

[code]
–new spring code
_W = display.contentWidth
_H = display.contentHeight

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

local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
end

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)

elseif springCount == 1 then–how many springs you can add
print(“stop”)
end
end

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

button:addEventListener(“touch”, touch_button)
—end new spring code
[/code] [import]uid: 72845 topic_id: 34718 reply_id: 334718[/import]

I found some code a while back but can’t remember the source.

here it is though, just pass an object to this function and optional the minimum & maxium scale & time
for example, [lua]bounce(spring)[/lua]
[lua]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[/lua] [import]uid: 62706 topic_id: 34718 reply_id: 137948[/import]

I appreciate the response but I’m not really sure how to incorporate that into my current code though. Still new to Lua. I messed around with it a bit but had no success. [import]uid: 72845 topic_id: 34718 reply_id: 137960[/import]

Replace your move_spring function with the one I made, this should work.

[lua]local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
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
if event.phase == “began” or event.phase == “ended” or event.phase == “cancelled” then
bounce(event.target)
end
end[/lua] [import]uid: 212029 topic_id: 34718 reply_id: 138043[/import]

Thanks for the example deano. I’ve noticed the spring has a more spring like action when I click on it however when my object (sprite) or any other object I create lands on it the spring is still static. I tried replacing “obj” in your code with the name of my sprite, in this case “instance” but to no avail. Not sure what I’m doing wrong here.
Again thank you for posting. [import]uid: 72845 topic_id: 34718 reply_id: 138091[/import]

No problems. Can you post your code that handles your collisions between the spring and whatever other objects collide, as the only code above is for creating and moving the spring. [import]uid: 62706 topic_id: 34718 reply_id: 138135[/import]

Here’s my code for the spring button.

--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  
--Function to move spring, use later in another function  
local function move\_spring(event)  
 event.target.x = event.x  
 event.target.y = event.y  
 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  
 if event.phase == "began" or event.phase == "ended" or event.phase == "cancelled" then  
 bounce(event.target)  
 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  
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)  
  
 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  

Not sure if this helps. Thanks for taking the time to help out a newbie. [import]uid: 72845 topic_id: 34718 reply_id: 138147[/import]

Hi there, I can’t see any other objects, have you got another object that interacts with the spring? [import]uid: 62706 topic_id: 34718 reply_id: 138242[/import]

I have a sprite named (instance) that bounces on the spring. Not really sure how to write the collision in, or however that’s done. [import]uid: 72845 topic_id: 34718 reply_id: 138243[/import]

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]

Works great, thank you so much! [import]uid: 72845 topic_id: 34718 reply_id: 138334[/import]

I found some code a while back but can’t remember the source.

here it is though, just pass an object to this function and optional the minimum & maxium scale & time
for example, [lua]bounce(spring)[/lua]
[lua]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[/lua] [import]uid: 62706 topic_id: 34718 reply_id: 137948[/import]

I appreciate the response but I’m not really sure how to incorporate that into my current code though. Still new to Lua. I messed around with it a bit but had no success. [import]uid: 72845 topic_id: 34718 reply_id: 137960[/import]

Replace your move_spring function with the one I made, this should work.

[lua]local function move_spring(event)
event.target.x = event.x
event.target.y = event.y
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
if event.phase == “began” or event.phase == “ended” or event.phase == “cancelled” then
bounce(event.target)
end
end[/lua] [import]uid: 212029 topic_id: 34718 reply_id: 138043[/import]

Thanks for the example deano. I’ve noticed the spring has a more spring like action when I click on it however when my object (sprite) or any other object I create lands on it the spring is still static. I tried replacing “obj” in your code with the name of my sprite, in this case “instance” but to no avail. Not sure what I’m doing wrong here.
Again thank you for posting. [import]uid: 72845 topic_id: 34718 reply_id: 138091[/import]

No problems. Can you post your code that handles your collisions between the spring and whatever other objects collide, as the only code above is for creating and moving the spring. [import]uid: 62706 topic_id: 34718 reply_id: 138135[/import]

Here’s my code for the spring button.

--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  
--Function to move spring, use later in another function  
local function move\_spring(event)  
 event.target.x = event.x  
 event.target.y = event.y  
 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  
 if event.phase == "began" or event.phase == "ended" or event.phase == "cancelled" then  
 bounce(event.target)  
 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  
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)  
  
 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  

Not sure if this helps. Thanks for taking the time to help out a newbie. [import]uid: 72845 topic_id: 34718 reply_id: 138147[/import]

Hi there, I can’t see any other objects, have you got another object that interacts with the spring? [import]uid: 62706 topic_id: 34718 reply_id: 138242[/import]

I have a sprite named (instance) that bounces on the spring. Not really sure how to write the collision in, or however that’s done. [import]uid: 72845 topic_id: 34718 reply_id: 138243[/import]

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]