Different Touch Events

Hello,

So, I’m just messing around with Lua to see if I can get a handle on things, and am trying to write a basic program. When you touch, new physics bodies are created. But I want to add a different touch event specific to each body, so that if they are touched, an impulse is applied to them. My functions are as follows:

local function touched(event)  
 if event.phase == "began" or event.phase == "moved" then  
 local dot = display.newCircle(event.x, event.y, 20, 20)  
 dot:setFillColor(math.random(80),math.random(110),math.random(250))  
 physics.addBody(dot, { bounce = 0.8, friction = 1.0})  
 end  
end  

and

local function touchDot(event)  
 local dot = event.target  
 dot:applyLinearImpulse( 0, -0.2, event.x, event.y )  
end  

Then, I have the runtime event to call the touched function:

Runtime:addEventListener("touch", touched)  

So, now, how can I add an event listener to each dot that is created, so if they are touched, the touchDot function is called?

Thanks!
Logan [import]uid: 44896 topic_id: 7808 reply_id: 307808[/import]

Hi.

Change this :

local function touched(event)  
 if event.phase == "began" or event.phase == "moved" then  
 local dot = display.newCircle(event.x, event.y, 20, 20)  
 dot:setFillColor(math.random(80),math.random(110),math.random(250))  
 physics.addBody(dot, { bounce = 0.8, friction = 1.0})  
 end  
end  

To this

  
local function touchDot(event)  
 local dot = event.target  
 dot:applyLinearImpulse( 0, -0.2, event.x, event.y )  
end  
  
local function touched(event)  
 if event.phase == "began" or event.phase == "moved" then  
 local dot = display.newCircle(event.x, event.y, 20, 20)  
 dot:setFillColor(math.random(80),math.random(110),math.random(250))  
 physics.addBody(dot, { bounce = 0.8, friction = 1.0})  
 dot:addEventListener("touch", touchDot)   
 end  
end  

And obviously remove the runtime listener.

Hope that helps :slight_smile: [import]uid: 6981 topic_id: 7808 reply_id: 27702[/import]

Thanks! But, I can’t remove the Runtime listener, or there is no way to initial create the dots. But, if I leave it in, now I can apply an impulse to the dots, but I still create a new dot… [import]uid: 44896 topic_id: 7808 reply_id: 27703[/import]

Whoops of course, sorry I read wrong the first time.

Hope it works for you now :slight_smile: [import]uid: 6981 topic_id: 7808 reply_id: 27704[/import]

Well, it kind of works. The problem, though, is that I want to be able to detect two different touches. With the code as it is now (changed to what you suggested), no matter what happens, the touch event creates a new dot. I only want there to be a new dot created if I am NOT touching an existing dot. Does that make sense?

If the touch is applied to an existing dot, I only want the impulse applied. [import]uid: 44896 topic_id: 7808 reply_id: 27705[/import]

Alright, thanks :wink:

Thank you so much for your help! [import]uid: 44896 topic_id: 7808 reply_id: 27707[/import]

Sorry it was my fault i didn’t read the whole post when i posted.

Gimme a sec :slight_smile:

Out of interest, you create a new dot when you touch anywhere on the screen ? [import]uid: 6981 topic_id: 7808 reply_id: 27706[/import]

Yep. New dot created when you touch anywhere. The hope is to not create a new dot only when touching another dot. [import]uid: 44896 topic_id: 7808 reply_id: 27708[/import]

Right first off.

I’ve just realized that the physics engine isn’t being enabled also. (unless your code is longer than what you specified ?

If not here is the updated code :

  
local physics = require("physics")  
physics.start()  
  
local function touchDot(event)  
 local dot = event.target  
 dot:applyLinearImpulse( 0, -0.2, event.x, event.y )  
end  
   
local function touched(event)  
 local dot = display.newCircle(event.x, event.y, 20, 20)  
 dot:setFillColor(math.random(80),math.random(110),math.random(250))  
  
 physics.addBody(dot, { bounce = 0.8, friction = 1.0})  
  
 dot:addEventListener("tap", touchDot)   
end  
  
Runtime:addEventListener("tap", touched)  

We just need to add a check now to ensure that another dot doesn’t get generated if one exists in it’s current position. [import]uid: 6981 topic_id: 7808 reply_id: 27709[/import]

Oh, yeah. It was already enabled. There’s more code than what I listed. But, ok, this makes sense. But how do I check to see if there is already a dot in the current position?

I know it’s some sort of if statement, but I don’t know how to check for an object.

Thanks! [import]uid: 44896 topic_id: 7808 reply_id: 27710[/import]