Dragging Cut-Out

Hi everybody!

I’m currently experiencing an issue where I can’t stop the ability to drag.

Let me explain, your dragging things about with your finger THEN you press the play button and you sit back and watch what you’ve done, while no longer being able to drag - you must have seen this a thousand times before.

I’ve got most of that done however once you’ve clicked the play button instead of stopping the dragging from working completely it just ‘drops’ whatever your holding after a second or two then puts it back in the air, then drops it again and so on.

If anyone knew a good way of correcting this, you’d be doing me a real favour :slight_smile:

All help is appreciated as always!

  • Sam Jackson

[code]

local transparentball = display.newImage(“graphics/transparentball.png”) – displays image
transparentball.xScale = 1 – sets image scale
transparentball.yScale = 1

local btn2 = display.newImage(“graphics/button.png”)
btn2.x = 300
btn2.y = 10
btn2.xScale = 0.300
btn2.yScale = 0.300

local arguments =
{
{ x=50, y=10, w=100, h=100, r=10, red=255, green=0, blue=128 },
{ x=10, y=50, w=100, h=100, r=10, red=0, green=128, blue=255 },
{ x=90, y=90, w=100, h=100, r=10, red=255, green=255, blue=0 }
}

local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
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
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end

local function physicsStart(event)
physics.start()
onTouch:removeSelf()
onTouch = nil
end

physics.addBody(transparentball, “dynamic”, {density=0.9, friction=0.3, bounce=0.2})

transparentball:addEventListener( “touch”, onTouch )
btn2:addEventListener(“touch”, physicsStart)

end[/code] [import]uid: 90223 topic_id: 16425 reply_id: 316425[/import]

@ samuelej165

I made an app called Dragging Physics earlier this year and what I
did was make 2 buttons a Start and a Stop button.

– Start Button
changes ball to “dynamic” body and removes all Event Listeners
(Drag function , Rotating function, ect)

– Stop Button
Adds Event Listeners that you Removed with the Stop Button.
Removes current ball and places a new one.

It did the trick but if you don`t want to do this, base on your code
ill guess your btn2 is the Start button and you want to drag the
ball and when you hit btn2 you want ball to drop but not be able
continue being dragged? If this is the case on your
function PhysicsStart() comment out onTouch:removeSelf() and try
this instead

transparentball:removeEventListener( “touch”, onTouch )

i hope this helps if not post again and ill try be more helpful!

-LeivaGames
[import]uid: 30314 topic_id: 16425 reply_id: 61324[/import]

Thanks!

That certainly did the trick, working great now!

Just as a ‘side-question’, do you know how I can make a display object with a physics body ‘ghost-like’?

What I mean by this is objects will be able to pass through it without hitting it and bouncing off each other in opposite directions, however it won’t just be an image because it would still register forces and collisions.

All help is appreciated as always!

  • Sam Jackson [import]uid: 90223 topic_id: 16425 reply_id: 61597[/import]

Glad it worked out and as for your 2nd question have you looked into
.isSensor = true? That can help you out on your 2nd question if I
understood you correctly! [import]uid: 30314 topic_id: 16425 reply_id: 61651[/import]

Sam, check out sensors - they’re “ghost like” in the way you describe and can register collisions. [import]uid: 52491 topic_id: 16425 reply_id: 61652[/import]

Hey thanks guys!

I’ve looked into sensors, specifically .isSensor = true however this causes the ball to drop through the floor since its ability to travel through objects.

Is there anything I can do about this to choose specifically what it can and can’t travel through?
Or any other methods? [import]uid: 90223 topic_id: 16425 reply_id: 62157[/import]

Another method if the floor is flat would just be to manually keep the ball above it - but I don’t know how your floor looks :wink: [import]uid: 52491 topic_id: 16425 reply_id: 62176[/import]

Hehehe, the ground is flat but I’m not sure how to keep it manually above, could you please provide an example?

Also, I have platforms which hang in the air at different angles, is there anything I can do with these as well? [import]uid: 90223 topic_id: 16425 reply_id: 62218[/import]

Hmmmm, no, what I was thinking of was just for the floor. (Eg, if ball.y < 50 then ball.y = 50)

Why do you want it to be “ghost like”? Is this to pass through enemies? Could the enemies not be physics bodies? Or be physics bodies but sensors? [import]uid: 52491 topic_id: 16425 reply_id: 62230[/import]

I tried the code although unfortunately haven’t managed to get it to work, the ball just falls forever off the screen and you never see it reappear.

I don’t think there are errors in the listener but I’m not sure.

local function aboveGround(event)  
 if yellow.y \< 50 then   
 yellow.y = 50  
 end  
end  
  
Runtime:addEventListener("enterFrame", aboveGround)  

Yes, the objective is for it to pass through enemies which are also physics bodies. They could be sensors but there would still be the same problem of keeping them above ground. [import]uid: 90223 topic_id: 16425 reply_id: 62712[/import]

You have;
[lua]if yellow.y < 50[/lua]

For it to be less than 50 it would be at the very top of the screen. If the floor is at the bottom of the screen, try using this instead;
[lua]if yellow.y > display.contentHeight-30[/lua]

I’m a little rusty on this kind of thing but that should do it. Else, > 430 (for portrait) or > 270 (for landscape) should do the trick.

Peach :slight_smile: [import]uid: 52491 topic_id: 16425 reply_id: 62743[/import]

Thanks Peach, unfortunately that leaves it jumping from 1 spot to about 1 pixel beneath over and over again at what I assume is every frame.

Luckily I have found a small solution where I make the body type kinematic and manually set its position on the floor in the code, this means it will never respond to gravity but due to the nature of my game it probably won’t ever need to.

Thanks to everyone who helped! :slight_smile: [import]uid: 90223 topic_id: 16425 reply_id: 63208[/import]