Feed me Oil -> icons of objects to drag

hello corona comunity :slight_smile:

i am trying to make a system used by feed me oil, that is it, i have the icon on the screen and when i touch or press the icon to drag it will appear the object that i select and can drag to the game area, and when i drag the object up to icon the object disappear to the icon, it is like feed me oil objects system

someone has done something similar? or know any place where I can go see it to learn?

Thank you very much [import]uid: 26056 topic_id: 15221 reply_id: 315221[/import]

I’m not sure if I’ve understood you perfectly or not, but I wrote this code that I think will help you. Try it out in a new project just to see how it goes :slight_smile:

[lua]require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)

local function spawnMyObject (event)
if event.phase == “began” then
local myObject = display.newCircle( spawner.x, spawner.y, 25 )
physics.addBody(myObject, “dynamic”)
local function dragObject (event)
myObject.x = event.x
myObject.y = event.y
end
myObject:addEventListener(“touch”, dragObject)
end
end
spawner:addEventListener(“touch”, spawnMyObject)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 15221 reply_id: 56250[/import]

Thank you very much Peach :slight_smile: is really this that i need. i am very grateful, corona is great [import]uid: 26056 topic_id: 15221 reply_id: 56261[/import]

Excellent! I was concerned about whether or not I understood correctly what you were trying to achieve.

Very happy to have been able to help :smiley:

Peach [import]uid: 52491 topic_id: 15221 reply_id: 56358[/import]

Hello, I edited this code, but now I’m having some problems, I’ve been researching and have found what may be wrong but I can not solve these problems. is the following:
when I drag across the red button or clicking on the red button, the object would appear as if out of this button, but it appears at the top of the screen, in the example showed me that the peach is correct but in my example I can not equal.

the second question is: when the object collides with the pipe he should disappear, but it only disappears if the type of object is “dynamic” because if it is “kinematic” it does not disappear, but if I use the “dynamic” the object is affected by gravity and I do not want. someone can give me a hand?

Thank you very much corona community

[code]

require ( “physics” )
physics.start()
physics.setGravity( 0, 10 )

local pipe = display.newRect( 150, 0, 20, 80 )
physics.addBody(pipe, “static”, {isSensor = true})

pipe:addEventListener(“collision”, pipe)

function pipe:collision (event)
event.other:removeSelf()
end

local spawner = display.newRect( 140, 400, 40, 40 )
spawner:setFillColor(150, 0, 0)

local function spawnMyObject (event)
if event.phase == “began” then
local myObject = display.newImage(“block.png”, x, y )
physics.addBody(myObject, “dynamic”)
local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target.bodyType = “kinematic”

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

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”
end

end
end

– Stop further propagation of touch event!
return true

end
myObject:addEventListener(“touch”, startDrag)
end
end
spawner:addEventListener(“touch”, spawnMyObject)
[/code] [import]uid: 26056 topic_id: 15221 reply_id: 56577[/import]

Do you need to use gravity? If not you could set it to (0,0) - would that work? Otherwise how about making the object a sensor?

For your object, do you mean it spawns at the top of the screen as in, it’s y value would be 0? Or are you talking about layers?

Peach [import]uid: 52491 topic_id: 15221 reply_id: 56637[/import]

hi peach, yes i need gravity and it is my problem, because when i drag the object he is affected my gravity because his body type is dynamic when he is moving, because if is not dynamic he isnt removed by colision with pipe, i can change the body type to kinematic and the object is avoid by gravity, but when he collides with pipe he isnt removed, who i can do?use sensors?

the second question:
when i touch on the red button to create the object, the object should appear around the buttom, and he appear on top of the screen. but i think that i know what is the problem and in a few hours when i will work, i will try what i am thinking.
[import]uid: 26056 topic_id: 15221 reply_id: 56639[/import]

The secound question is resolved, now i only have the problem with gravity, because when i drag the object he is affected by gravity, because his body type is dynamic, because if i change to kinematic he isnt affected by gravity but isnt remove by pipe collision, can help me plz? [import]uid: 26056 topic_id: 15221 reply_id: 56678[/import]

all problems are resolved :slight_smile: [import]uid: 26056 topic_id: 15221 reply_id: 56757[/import]

Glad to hear it’s all resolved, sorry I wasn’t here in time to help - Australia is in a very different time zone :wink:

Peach [import]uid: 52491 topic_id: 15221 reply_id: 56879[/import]

dont makes bad pech, you helped me a lot with the initial code :slight_smile: [import]uid: 26056 topic_id: 15221 reply_id: 56889[/import]

:frowning: 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]

Ah, OK - you don’t want him to rotate when he hits other objects while dragging?

When the drag starts;
[lua]obj.isFixedRotation = true[/lua]

When it ends;
[lua]obj.isFixedRotation = false[/lua]

Try that :wink:

Peach [import]uid: 52491 topic_id: 15221 reply_id: 57079[/import]

that’s just what I want, i am a new corona user and i am learning sometimes easy question i dont know xD, thank you very much again peach :slight_smile: [import]uid: 26056 topic_id: 15221 reply_id: 57085[/import]

topic closed

thanks for all the help peach :slight_smile: [import]uid: 26056 topic_id: 15221 reply_id: 57114[/import]

No worries; will close this for you :slight_smile: [import]uid: 52491 topic_id: 15221 reply_id: 57229[/import]