I know I’m doing something wrong here, I just can’t figure out what it is… In the code below, there is a “door” attached to 2 “frames” on each end of the door. When you touch the door it swings open. There is a timer to close it after 1 second.
That all works, but there are 2 problems:
- When the door swings back into place it gets misaligned and looks goofy.
- The joint doesn’t re-attach even though the code is there to reconnect it.
Any help would be much appreciated! 
Dave
-- main.lua
local physics = require("physics")
physics.start()
local test = display.newText("touch the door", 75, 40, "Courier", 20)
test:setTextColor(255, 255, 255, 255)
-- globals
joint1 = {}
joint2 = {}
frame1 = {}
frame2 = {}
local door = {}
local doorHeight = 10
local frameWidth = 20
local frameHeight = 10
local frame1X = 100
local frame1Y = 150
local frame2X = 210
local frame2Y = 150
local doorX
local doorY
local doorResetTimer = {}
function doorResetTimer( event )
-- return the door back to original position
transition.to( door, { time=500, rotation=0.2, x=doorX, y=doorY } )
-- reconnect the joint
door["joint2"] = physics.newJoint( "pivot", frame2, door, doorX + door.width/2, doorY )
door["joint2"].isLimitEnabled = true
door["joint2"]:setRotationLimits( -90, -0 )
timer.cancel( doorResetTimer )
end
function openDoor( event )
local t = event.target
local phase = event.phase
if "ended" == phase then
door["joint2"]:removeSelf()
-- reset the door position
doorResetTimer = timer.performWithDelay( 1000, doorResetTimer, 1 )
end
end
local frame1 = display.newRect(0, 0, frameWidth, frameHeight)
frame1:setFillColor(0, 0, 200, 255)
frame1.x = frame1X; frame1.y = frame1Y
physics.addBody( frame1, "static", { friction=0.5 } )
local frame2 = display.newRect(0, 0, frameWidth, frameHeight)
frame2:setFillColor(0, 0, 200, 255)
frame2.x = frame2X; frame2.y = frame2Y
physics.addBody( frame2, "static", { friction=0.5 } )
door = display.newRect( frame1X, frame1Y - doorHeight/2, frame2X - frame1X, doorHeight )
door:setFillColor(0,255,0,255)
physics.addBody( door, { density=1.8, friction=0.3, bounce=0.3 } )
doorX = door.x; doorY = door.y
door["joint1"] = physics.newJoint( "pivot", frame1, door, door.x - door.width/2, door.y )
door["joint1"].isLimitEnabled = true
door["joint1"]:setRotationLimits( 0, 90 )
door["joint2"] = physics.newJoint( "pivot", frame2, door, door.x + door.width/2, door.y )
door["joint2"].isLimitEnabled = true
door["joint2"]:setRotationLimits( -90, -0 )
door:addEventListener( "touch", openDoor )
-- config.lua
application =
{
content =
{
width = 320,
height = 480,
scale = "zoomEven"
},
}
[import]uid: 8194 topic_id: 1923 reply_id: 301923[/import]