Hi,
I “pinned” some physics object(name cross) on the screen.
How to make them draggable?
I try change the bodyType to kinematic but it doesn’t work.
If I change the bodyType to dynamic, it works, but it’s not what I want, the objects is not “pinned”.
Looks like I make some stupid mistake that I didn’t realize.
Somebody help. Thank you.
Soo
(sorry, I try to the code below between[lua] but it looks mess.)
main.lua
_W = display.viewableContentWidth;
_H = display.viewableContentHeight;
local physics = require(“physics”);
physics.setDrawMode(“hybrid”);
physics.start();
– basic setting
local background = display.newImageRect(“images/background.png”, 320, 480);
background.x = _W * 0.5;
background.y = _H * 0.5;
local ground = display.newImageRect(“images/ground.png”, 320, 10);
ground.x = _W * 0.5;
ground.y = _H - 5;
local celling = display.newImageRect(“images/ground.png”, 320, 10);
celling.x = _W * 0.5;
celling.y = 5;
local leftWall = display.newImageRect(“images/leftWall.png”, 10, 480);
leftWall.x = 0;
leftWall.y = _H * 0.5;
local rightWall = display.newImageRect(“images/rightWall.png”, 10, 480);
rightWall.x = _W;
rightWall.y = _H * 0.5;
physics.addBody(ground, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(celling, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(leftWall, “static”, {friction = 0.2, bounce = 0.3});
physics.addBody(rightWall, “static”, {friction = 0.2, bounce = 0.3});
– end of basic setting
local ball = display.newImageRect(“images/ball.png”, 100, 100);
ball.x = _W * 0.5-10;
ball.y = 40;
ball.xScale = 0.25;
ball.yScale = 0.25;
physics.addBody(ball, {friction = 0.3, bounce = 0.5, radius = 12.5});
– ball editing
–smallCross function
local NewSmallCross = {};
NewSmallCross.new = function(params)
smallCross = display.newImageRect(“images/cross.png”, 10, 10);
smallCross.x = cross.x;
smallCross.y = cross.y;
physics.addBody(smallCross,“kinematic”, {friction = 0.2, bounce = 0.1});
return smallCross;
end
–cross function
local NewCross = {};
NewCross.new = function(params)
cross = display.newImageRect(“images/cross.png”, 50, 50);
cross.x = _W * 0.5;
cross.y = _H * 0.5;
function cross:touch(e)
if(e.phase == “began”) then
display.getCurrentStage():setFocus(self);
self.hasFocus = true;
oldX = self.x;
oldY = self.y;
print(“beginning…”);
elseif (self.hasFocus) then
if(e.phase == “moved”) then
self.x = (e.x - e.xStart) + oldX;
self.y = (e.y - e.yStart) + oldY;
print(“moving…”);
elseif(e.phase == “ended” or e.phase == “cancelled”) then
display.getCurrentStage():setFocus(nil);
self.hasFocus = false;
print(“ending…”);
end
end
return true;
end
cross:addEventListener(“touch”, cross);
local pBody = {};
pBody.cross = {
{
friction = 0.2,
bounce = 0.2,
shape = {-5,-25, 5,-25, 5,-5, -5,-5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {5,-5, 25,-5, 25,5, 5,5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {5,5, 5,25, -5,25, -5,5}
},
{
friction = 0.2,
bounce = 0.2,
shape = {-5,5, -25,5, -25,-5, -5,-5}
}
};
physics.addBody(cross, “dynamic”, unpack(pBody.cross));
return cross;
end
crossTable = {};
smallCrossTable = {};
pJoints = {};
for i = 1, 3 do
crossTable[i] = NewCross.new();
print(#crossTable);
crossTable[i].x = (i * 80);
crossTable[i].y = _H * 0.5;
smallCrossTable[i] = NewSmallCross.new();
print(#smallCrossTable);
smallCrossTable[i].x = crossTable[i].x;
smallCrossTable[i].y = crossTable[i].y;
pJoints[i] = physics.newJoint(“pivot”, smallCrossTable[i], crossTable[i], smallCrossTable[i].x, smallCrossTable[i].y);
pJoints[i].isMotorEnabled = false;
pJoints[i].motorSpeed = 100;
pJoints[i].maxMotorTorque = 100;
print("pJoints = "…#pJoints);
end
function ball:tap(e)
ball.x = _W * 0.5 + 10;
ball.y = 50;
end
ball:addEventListener(“tap”, ball);