I have the following code which I have adapted from the a couple of the tutorials.
What I am trying to achieve is adding joints based on a Y axis movement. Dependent on its direction increase or decrease the length of the rope. I get this to work but the joints just appear. What techniques could I employ to make it look like the rope is moving down (increasing in size) or moving up (decreasing) in size?
Is it just a case of decreasing the size of the image and adding more smaller joints in but concerned amount memory management of optimisation
[lua]local physics = require(“physics”)
– Load The Remote
local remote = require(“remote”)
– Start The Remote On Port 8080
remote.startServer( “8080” )
physics.start()
physics.setScale(60)
display.setStatusBar(display.HiddenStatusBar)
local ceiling = display.newRect (0, 0, display.contentWidth, 1)
physics.addBody( ceiling, “static”, { density=0, friction=0.5,bounce=0.2 } )
local prevBody = ceiling
local xCenter = 160
local wCeil = 120
local hCeil = -5
local totalYMovement = 0
local w,h = 10,10
local halfW,halfH = 0.5*w,0.5*h
– center of body
local x = xCenter
local y = hCeil - halfH
local yJoint = y - halfH
for i = 1, 20 do
y = y + h
yJoint = yJoint + h
local body = display.newImage(“rope.png” ,x-halfW, y-halfH) --) display.newRect( x-halfW, y-halfH, w, h )
–body:setFillColor( 128, 0, 0 )
physics.addBody( body, { density=15, friction=0.5, bounce = .2 })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, yJoint )
prevBody = body
end
– Get The Latest Accelerometer Values
local function updateAccelerometer()
– This Runtime Listener Is An Example Of How To
– Access The Remote You Can Query remote.xGravity
– Or Any Other Value From Anywhere In Your Application
local xGravity = remote.xGravity
local yGravity = remote.yGravity
totalYMovement = totalYMovement + yGravity
if(totalYMovement > 17) then
print(“Add link”)
totalYMovement = 0
y = y + h
yJoint = yJoint + h
local body = display.newImage(“rope.png” ,x-halfW, y-halfH) --) display.newRect( x-halfW, y-halfH, w, h )
–body:setFillColor( 128, 0, 0 )
physics.addBody( body, { density=15, friction=0.5, bounce = .2 })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, yJoint )
prevBody = body
elseif(totalYMovement < -17) then
print(“Remove link”)
totalYMovement = 0
end
print ("TotalYMovement: " … totalYMovement … " yGravity: " … yGravity)
end
–Runtime:addEventListener(“accelerometer”, acc)
– Add Enter Frame Listener
Runtime:addEventListener( “enterFrame” , updateAccelerometer )[/lua] [import]uid: 103970 topic_id: 17957 reply_id: 317957[/import]