All,
I have a class that I am building that represents a “man”. the man has a body, arms, legs and feet. I am trying to use a table to keep track of the positions each limb is in. That is working fine, but I also want to use this position table to determin the shape of the man’s body for the physics engine. for example, the man’s right arm is in the table positions as the first element. If I create a function called rightArmShape, how do I access the position atble of the man that gets passed to it? My current code is below:
function rightArmShape(man)
if (man.positions[1] == 0) then
return {-78,30, -26,30, -26,43, -78,30 }
else
return {-26,30, -26,-22, -13,-22, -13,30 }
end
end
However, this throws a runtime error stating that there is an “attempt to index field ‘positions’ (a nil value)”
I create the man with the following code, and set the positions table as a local variable. Any help is greatly appreciated.
Code to create a “new Man”:
[code]function newMan()
local man = display.newGroup()
– Set rgb color scale for man.
local r = math.random(0,255)
local g = math.random(0,255)
local b = math.random(0,255)
– Build Body Parts.
local body = display.newRect(-26,30, 52,52)
body:setFillColor(r,g,b,255)
local head = display.newRect(-13,4, 26,26)
head:setFillColor(r,g,b,255)
local rightArm = display.newRect(-78,30, 52,13)
rightArm:setFillColor(r,g,b,255)
local leftArm = display.newRect(26,30, 52,13)
leftArm:setFillColor(r,g,b,255)
local leftLeg = display.newRect(13,82, 13,52)
leftLeg:setFillColor(r,g,b,255)
local rightLeg = display.newRect(-26,82, 13,52)
rightLeg:setFillColor(r,g,b,255)
local rightHand = display.newRect(-78,20, 5,10)
rightHand:setFillColor(r,g,b,255)
local leftHand = display.newRect(73,20, 5,10)
leftHand:setFillColor(r,g,b,255)
local rightFoot = display.newRect(-36,129, 10,5)
rightFoot:setFillColor(r,g,b,255)
local leftFoot = display.newRect(26,129, 10,5)
leftFoot:setFillColor(r,g,b,255)
man:insert(body)
man:insert(head)
man:insert(rightArm)
man:insert(leftArm)
man:insert(rightLeg)
man:insert(leftLeg)
man:insert(rightHand)
man:insert(leftHand)
man:insert(rightFoot)
man:insert(leftFoot)
–Create a table called positions to hold the current position of each limb.
– Fill table at creation with all zeros to represent original limb location.
– positions table position and coresponding limb:
– 1 - Right Arm - 0 = down, 1 = up
– 2 - Right Hand - 0 = top, 1 = bottom
– 3 - Left Arm - 0 = down, 1 = up
– 4 - Left Hand - 0 = top, 1 = bottom
– 5 - Right Leg - 0 = down, 1 = up
– 6 - Right Foot - 0 = right, 1 = left
– 7 - Left Leg - 0 = down, 1 = up
– 8 - Left Foot - 0 = left, 1 = right
– 9 - Head - 0 = up, 1 = down ***(future use)
local positions = {0, 0, 0, 0, 0, 0, 0, 0, 0}
function rightArm:touch(event)
if (event.phase == “ended”) then
if (positions[1] == 0) then
rightArm.height = 52
rightArm.width = 13
rightArm.x = -19
rightArm.y = 4
print("positions: "…positions[1])
positions[1] = 1
print("positions: "…positions[1])
rightHand.height = 5
rightHand.width = 10
rightHand.x = -8
rightHand.y = -20
else
rightArm.height = 13
rightArm.width = 52
rightArm.x = -52
rightArm.y = 36
print("positions: "…positions[1])
positions[1] = 0
print("positions: "…positions[1])
rightHand.height = 10
rightHand.width = 5
rightHand.x = -75
rightHand.y = 25
end
end
end
function leftArm:touch(event)
if (event.phase == “ended”) then
if (leftArm.height == 13) then
leftArm.height = 52
leftArm.width = 13
leftArm.x = 20
leftArm.y = 4
leftHand.height = 5
leftHand.width = 10
leftHand.x = 8
leftHand.y = -20
else
leftArm.height = 13
leftArm.width = 52
leftArm.x = 52
leftArm.y = 36
leftHand.height = 10
leftHand.width = 5
leftHand.x = 76
leftHand.y = 25
end
end
end
function rightLeg:touch(event)
if (event.phase == “ended”) then
if (rightLeg.height == 52) then
rightLeg.height = 13
rightLeg.width = 52
rightLeg.x = -52
rightLeg.y = 75
rightFoot.height = 10
rightFoot.width = 5
rightFoot.x = -75
rightFoot.y = 64
else
rightLeg.height = 52
rightLeg.width = 13
rightLeg.x = -19
rightLeg.y = 108
rightFoot.height = 5
rightFoot.width = 10
rightFoot.x = -31
rightFoot.y = 131
end
end
end
function leftLeg:touch(event)
if (event.phase == “ended”) then
if (leftLeg.height == 52) then
leftLeg.height = 13
leftLeg.width = 52
leftLeg.x = 52
leftLeg.y = 75
leftFoot.height = 10
leftFoot.width = 5
leftFoot.x = 76
leftFoot.y = 64
else
leftLeg.height = 52
leftLeg.width = 13
leftLeg.x = 20
leftLeg.y = 108
leftFoot.height = 5
leftFoot.width = 10
leftFoot.x = 31
leftFoot.y = 131
end
end
end
function body:touch( event )
local t = event.target
local phase = event.phase
if “began” == phase then
man.isFocus = true
man.x0 = event.x - man.x
man.y0 = event.y - man.y
elseif (phase == “moved”) then
man.x = event.x - man.x0
man.y = event.y - man.y0
elseif (phase == “ended” or phase == “canceled”) then
man.isFocus = false
end
end
body:addEventListener( “touch”, body)
rightArm:addEventListener( “touch”, rightArm)
leftArm:addEventListener( “touch”, leftArm)
rightLeg:addEventListener( “touch”, rightLeg)
leftLeg:addEventListener(“touch”, leftLeg)
return man
end[/code] [import]uid: 16830 topic_id: 6035 reply_id: 306035[/import]