Game Development Reference
In-Depth Information
The object will now bounce when it reaches the bottom of the screen.
We can add some code to move the object around the screen, but before that, let's create a wall
around the corners of the screen so that the physics body does not fall off the side of the screen
when we move it.
function addSegment(x1, y1, x2, y2)
shape = body:addSegment(x1,y1,x2,y2)
shape:setElasticity(1)
shape:setFriction(0.1)
shape:setType(2)
space:insertPrim(shape)
space:shapeForPoint . We can
mx, my = layer:wndToWorld(x,y)
mouseBody:setPos(mx, my)
end
Now, as the mouse moves, the mouseBody object is placed at the location where the pointer currently
is. We can also set a callback for the left mouse button function that can help pick up and move the
object.
function onClick(down)
if down then
pick = space:shapeForPoint(mx, my)
if pick then
body = pick:getBody()
mouseJoint = MOAICpConstraint.newPivotJoint(
mouseBody, body, 0, 0, body:worldToLocal(mx, my))
space:insertPrim(mouseJoint)
else
if mouseJoint then
space:removePrim(mouseJoint)
mouseJoint = nil
end
end
end
end
Search WWH ::




Custom Search