Adding a function to an object

All,

I am struggling a bit with functions. I have an object in my program that I am trying to provide feedback to my main program through. The program is such:

I have a man figure wich the user can move the arms up or down, and I wan to get the shape based on that position so I can properly add the man to the physics engine. My code for the man is in a seperate file titles BlockMan.lua. If I want to add a function that detects the position of the arm and and returns the appropriate body points, how do I define it and then allow access form my main program?

My BlockMan.lua file is here:

[code]-- This module is designed to provide a block man and associated functions to
– the main.lua program.

module(…, package.seeall)

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)

function rightArmShape()
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

man.rightArmShape = rightArmShape()
return man
end[/code]

My Mian file is here:

[code]local physics = require “physics”
local blockMan = require( “BlockMan” )
physics.start()

maxMen = 3
currentMan = 1
local ground = display.newRect(-50,display.contentHeight-50, display.contentWidth+100, 20)
ground:setFillColor(125,125,125,255)
physics.addBody(ground, “static”, {friction=0.1, bounce=0.2})

local loadSquare = display.newRect(5,20, 160,160)
loadSquare:setFillColor(255,255,255,255)

local man1 = blockMan.newMan{}
man1.x = 85
man1.y = 45

function makeMan()
aMan = blockMan.newMan{}
aMan.x = 85
aMan.y = 45
aMan:addEventListener(“touch”, moveMan)
currentMan = currentMan + 1
end

function moveMan( event)
local phase = event.phase
local t = event.target
if (phase == “ended”) then
if (t.x > 160 and t.y > 180) then
physics.addBody(t, {density=0.5, bounce=0.2, shape=t.rightArmShape()})
if (currentMan < maxMen) then
makeMan()
end
end
end
end

man1:addEventListener(“touch”, moveMan)[/code]

Thanks for the help…

Ray [import]uid: 16830 topic_id: 6978 reply_id: 306978[/import]

i would try and get your head around this to start off with:

https://developer.anscamobile.com/forum/2011/01/19/big-refactor#comment-17393

[import]uid: 6645 topic_id: 6978 reply_id: 24467[/import]