i cant make how i was making, try this code, it works perfectly, but i cant resolve this problem:
- When the object is moving and collides with other object he rotates, because the objects physics, because when i am moving a object his type is dynamic, because if it is kinematic, the objects avoid physics, and it is perfect for me, but he dont make the collision and is not removed by the object that his function is remove objects.
basically, i need the physics at 9.8 because i need physics gravity to the ball, but i dont want gravity to the draggable objects.
Can anyone tell me your opinions and help me with this problem plz?
try this code
main.lua
display.setStatusBar( display.HiddenStatusBar )
---------------------------------------------------------------
--Screen
\_W = display.contentWidth;
\_H = display.contentHeight;
-- Import director class
director = require("director")
-- Create a main group
local mainGroup = display.newGroup()
-- Main function
local function main()
-- Add the group from director class
mainGroup:insert(director.directorView)
-- Change scene
director:changeScene("game")
-- Return
return true
end
-- Begin
main()
game.lua
[code]
module(…, package.seeall)
function new()
local physics = require “physics”
–Physics
physics.start();
physics.setGravity (0,9.8)
physics.setDrawMode (“hybrid”)
–Drag and collision
–Objects bar
local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)
–remove function
function spawner:collision (event)
event.other:removeSelf()
end
–Objects and Drag
local function spawnMyObject (event)
if event.phase == “began” then
–objects and physics
local myObject = display.newImage(“block.png”, 200, 300)
physics.addBody(myObject, “kinematic”, { density=2, friction=0, bounce=0})
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– make the bodys dynamic without gravity
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
event.target.bodyType = “dynamic”
physics.setGravity (0,0)
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”
end
end
end
return true
end
myObject:addEventListener(“touch”, startDrag)
end
end
spawner:addEventListener(“touch”, spawnMyObject)
end
[/code] [import]uid: 26056 topic_id: 15221 reply_id: 57002[/import]