Keeping items in certain areas

Ok, I need pointed in the right direction here.

I’m working on a game here for work. The idea is the top half of the iPad will have certain characters. The bottom have will have other characters. The bottom characters are draggable similar to “Flight Path” - but they can’t come into the top half.

Is there a way to set it so those bottom objects can’t pass a certain line? I’m a HTML/CSS guy and in my brain I can do it … but I can’t really figure it out in Lua.

Designer … not a developer … go figure. [import]uid: 97023 topic_id: 19237 reply_id: 319237[/import]

you can set a function to check in what place was drag ended, like that:
[lua]local function foo(event)
if event.phase == “ended” then
if thing.y >= display.contentHeight/2 then
thing.y = display.contentHeight - 50
end
end
end[/lua] [import]uid: 16142 topic_id: 19237 reply_id: 74201[/import]

Added the function, call it with the right object … but itu doesn’t actually do anything. I can still drag and drop the item anywhere in the scene. [import]uid: 97023 topic_id: 19237 reply_id: 74202[/import]

plug and play

[lua]local thing = display.newRect(0,0,50,50)
thing.x = 50; thing.y = 400

local bound = display.newRect(0,0, display.contentWidth, 10)
bound.y = display.contentHeight/2

local function foo(event)
if event.phase == “moved” then
thing.x = event.x
thing.y = event.y

if thing.y <= display.contentHeight/2 then
thing.y = display.contentHeight/2 + 100
end
end
end
thing:addEventListener(“touch”, foo)[/lua] [import]uid: 16142 topic_id: 19237 reply_id: 74204[/import]

Oh, oh that’s perfection … PERFECTION I SAY!

Thank you, very much. I was so … close. No, no I wasn’t. I was completely lost. [import]uid: 97023 topic_id: 19237 reply_id: 74207[/import]

you can do that with event.phase == “ended” too by the way

anyway, glad to help) [import]uid: 16142 topic_id: 19237 reply_id: 74209[/import]

I added it to the “DragPlatforms” script. Here is what I gots:

  
 -------------------  
 -------------------   
 -- Start Drag stuff  
 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 body type temporarily "kinematic" (to avoid gravitional forces)  
 event.target.bodyType = "kinematic"  
  
 -- 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  
  
 if t.y \<= 350 then  
 t.y = 350 + 35  
 end  
  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
 -- Switch body type back to "dynamic", unless we've marked this sprite as a platform  
 if ( not event.target.isPlatform ) then  
 event.target.bodyType = "dynamic"  
 end  
  
 end  
 end  
  
  
 return true  
end  
 -- End Drag Stuff  
 -----------------  
 -----------------  
  

Thanks agains for the help! [import]uid: 97023 topic_id: 19237 reply_id: 74213[/import]

Nice to see this get solved with Darkconsoles help :slight_smile:

I loved your “Oh, oh that’s perfection … PERFECTION I SAY!” response. Sounds like something I’d randomly yell XD

Peach :slight_smile: [import]uid: 52491 topic_id: 19237 reply_id: 74350[/import]

If that’s the type of things you’d say, than we’d probably get along swimmingly! [import]uid: 97023 topic_id: 19237 reply_id: 74691[/import]

I like your style my good man! I hope shall encounter you many a time in future threads. [import]uid: 52491 topic_id: 19237 reply_id: 74846[/import]