Hi, this is my code:
physics.addBody(ground, "static", {friction = 0.1})
-- CREATE 'BOXES' DISPLAY GROUP & BOXES
-- A display group we can loop through -
-- works just like a table, but allows us to perform other display functions upon the boxes as well.
-- Be sure to use boxes.numChildren instead of #boxes when getting the number of boxes in the group,
-- but otherwise it works just like a table (which is really just an array)
local boxes = display.newGroup()
-- It adds that box to the 'boxes' display group
local function addBox()
local box = display.newRect(0, 0, boxSize, boxSize)
box.x =boxSize
box.y =\_H/2
box.area = box.height \* box.width
box.density = 3.0
box.mass = box.area \* box.density
box:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255) )
local olho=display.newCircle(box.x, box.y, 10 )
physics.addBody( box, { density=3.0, friction=0.5 } )
end
-- CREATE SET NUMBER OF BOXES (DEFINED IN SETTINGS ABOVE)
for i=1, numberOfBoxes do
-- addBox()
end
-- TOUCH TO DRAG FUNCTION
local function dragBody(e)
local body = e.target
local phase = e.phase
local stage = display.getCurrentStage()
if(phase == "began") then
stage:setFocus(body, e.id)
body.isFocus = true
body.tempJoint = physics.newJoint("touch", body, e.x, e.y)
elseif(phase == "moved") then
if(body.tempJoint ~= nil) then
body.tempJoint:setTarget(e.x, e.y)
end
elseif(phase == "ended" or phase == "cancelled") then
if(body.tempJoint ~= nil) then
stage:setFocus(body, nil)
body.isFocus = false
body.tempJoint:removeSelf()
body.tempJoint = nil
end
end
return true
end
for i=1, boxes.numChildren do
boxes[i]:addEventListener("touch", dragBody)
end
When i drag the box i want the shapre “olho” moves together with the box…
Cheers, [import]uid: 77944 topic_id: 17460 reply_id: 66266[/import]