How to make an object draggable without touching it !?

Hi,
i’m building a game, it depends on throwing rocks; and i want to drag the rock and throw it without touching it … i want to drag it from anywhere when i’ve just click … and of course the rock will still be under the click event control … i’ve tried to use the ‘tempJoint’ with the Runtime event listener instead of callin’ the function through adding event listener to the object … but it doesn’t work well

Any thoughts !!?

thanks, :slight_smile: [import]uid: 96659 topic_id: 18029 reply_id: 318029[/import]

Hi akamel

So to be clear you want to determine the x and y changes which aren’t dependent on touching the specific rock object but by clicking anywhere on the screen.

If I have understood this correct could you not just determine the start and end positions of the the drag for both x and y and then apply the offset to the rock object to perform the velocity etc

Apologies if i have misunderstood the problem [import]uid: 103970 topic_id: 18029 reply_id: 68995[/import]

I understand the problem the same as @mygamingproject, and if that is correct…they are correct.

Calculate the delta between the x and y of the touch event, and the x and y of the rock. Then move the rock to the x and y of the touch event, plus the delta.

If you need an example, let me know. [import]uid: 5317 topic_id: 18029 reply_id: 69079[/import]

thanks to both of u … u got my point
@mike: actually yeah… if u have an example i will appreciate that … and also to clear any misunderstanding coz i was going to use the tempjoint

thanks for ur interest [import]uid: 96659 topic_id: 18029 reply_id: 69122[/import]

@mike: actually yeah… if u have an example i will appreciate that … and also to clear any misunderstanding coz i was going to use the tempjoint

thanks for ur interest [import]uid: 96659 topic_id: 18029 reply_id: 69710[/import]

Here is some quick code to show you how to do it. This is not the be all and end all solution, feel free to modify to suit your needs, or improve upon it.

Dump this in a main.lua and supply a background image, and an image for the enemy object and have a play. There is a strange hitch in the movement of the object that I didn’t have time to debug. It still functions properly, but you will notice the hitch. Even with the hitch, the principle is sound.

Hope it helps.

[lua]–I wrote the code to display on the iPad in the simulator, so background image is 768 x 1024
–and enemy is an image of 36x32

– I commented to help explain what is going on

_W = display.contentWidth; _H = display.contentHeight; – globals for the screen width and height
local deltaX, deltaY; --forward declarations

local background = display.newImage(“background.jpg”, 0,0);
background.x = _W*0.5; background.y = _H*0.5;
local enemy = display.newImage(“Alien.png”, 0, 0);
enemy.x = _W*0.5; enemy.y = _H*0.5;

local function touchAndMove(event) – touch event listener for the background

– here we calculate the delta (difference) between where the user touches, and where the enemy image is on screen.
–We subtract the touches x and y position from the enemies x and y position.
local function calculateDelta()
deltaX = enemy.x - event.x;
deltaY = enemy.y - event.y;
end

–this function moves the object around by moving it to the location of the touches x and y, plus the delta
local function moveObject()
enemy.x = event.x + deltaX;
enemy.y = event.y + deltaY;
end

if(event.phase == “moved”) then
Runtime:addEventListener(“enterFrame”, moveObject);
end

if(event.phase == “began”) then
calculateDelta();
end

if(event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveObject);
end
end

background:addEventListener( “touch”, touchAndMove )[/lua] [import]uid: 5317 topic_id: 18029 reply_id: 69837[/import]

thanq so much mike … i notice the hitch … and i solved it by doing this

if(event.phase == “moved”) then
Runtime:addEventListener(“enterFrame”, moveObject);
end

if(event.phase == “began”) then
calculateDelta();
Runtime:addEventListener(“enterFrame”, moveObject);
end

if(event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveObject);
end

i’ve just called the ‘moveObject’ function at the “began” case … and it works well … no hitch occurs
…i’m not sure if this is efficiency right or wrong, but i think it’s cool and it works :wink:

anyway thnx for help [import]uid: 96659 topic_id: 18029 reply_id: 69903[/import]

Well, that does fix it up nicely.

I will need to test to see if we are getting multiple eventListeners attached to the Runtime, because we are attaching one in both the “began” and the “moved” phase.

Just wanna make sure the memory is managed.

Glad the example helped out. [import]uid: 5317 topic_id: 18029 reply_id: 69904[/import]

removing duplicate post. note, if you click on the save button to fast, it will multi-post. [import]uid: 5317 topic_id: 18029 reply_id: 69905[/import]

Mike,

Were your draggable objects kinematic or dynamic?

-David [import]uid: 96411 topic_id: 18029 reply_id: 69913[/import]

Neither. They are just display objects.
[import]uid: 5317 topic_id: 18029 reply_id: 69957[/import]