Detecting Multi Touch

Hello,

i am building a shooter game and am trying to detect a multi touch event where the player can drag the sprite with none finger as well as shoot bullets with the other finger…i can;t seem to figure out how to use the touchID…

at the start of the class i activate the multitouch

system.activate(“multitouch”)

i then declare some variables

local alien
local bullet

local bg = display.newImage(“bg.png”)
bg:setReferencePoint(display.TopLeftReferencePoint)
bg:scale(10,10)
bg.x = -50
local bullets = display.newGroup()

as well as functions

local Main = {}
local addAlien = {}
local listeners = {}
local shoot = {}
local moveAlien = {}
local update = {}

the constructor and other functions:

function Main()
    addAlien()
end

 function addAlien()
    alien = display.newImage(‘alien.png’)
    alien:scale(0.5,0.5)
    alien.x = display.contentCenterX
    alien.y = display.contentCenterY
    listeners(‘add’)
end

function listeners(action)
    if(action == ‘add’) then
    bg:addEventListener(‘touch’,moveAlien)
    bg:addEventListener(‘touch’,shoot)
    Runtime:addEventListener(‘enterFrame’,update)
    else
    bg:removeEventListener(‘touch’,moveAlien)
    bg:removeEventListener(‘touch’,shoot)
    Runtime:removeEventListener(‘enterFrame’,update)
  end
end

and here is where i think the problem is:

function moveAlien:touch(e)
    if(e.phase == ‘began’)then
    display.getCurrentStage():setFocus(bg,e.id)  
    lastX = e.x - alien.x
    lastY = e.y - alien.y
  elseif(e.phase == ‘moved’)then
    alien.x = e.x - lastX
    alien.y = e.y - lastY
    elseif(e.phase == ‘ended’)then
    display.getCurrentStage():setFocus(nil)   
   
 end
end

here is the shoot function:

function shoot:touch(e)
    if(e.phase == ‘began’)then

    

    bullet = display.newImage(‘bullet.png’)
    bullet:scale(0.2,0.2)
    bullet.x = alien.x
    bullet.y = alien.y-15
    bullet.name = ‘bullet’
    bullets.insert(bullets,bullet)
    
    elseif(e.phase == ‘ended’) then
    
   end    
end
 

and the rest of the code:

function update(e)
    if(bullets.numChildren ~= 0)then
    for i = 1,bullets.numChildren do
      if(bullets[i] ~= nil)then
       bullets[i].x = bullets[i].x+10
        end
     end
   end
end

----call main

Main()

any help would be appreciated,

thanks

Hi dvelez60…

I’m trying to make multitouch also…

I want a big map and when people “pinch” their fingers

they can zoon in or out in the map

make it bigger or smaller so they need 2 fingers…

if you know how let me know, please.

One thing I can share with you that I know it will help you a lot

it’s the use of this – [ – the bracket

when you write a piece of code use this…(only the brackets and the word “code” inside the brackets)

------------- [---- code ----] ------- (please ignore all these lines ------) only the open bracket the word code and the closing bracket.

-------------- and write your code here, or copy and paste from your editor and then when done write the brackes with the "slash" the one under the ? sign like this   -------------

then at the end the opening bracket [the back slash / the word “code” and the closing bracket]

I hope you get it.

if you do that your code looks nicer and it’s easier to read…

like this

local bg = display.newImageRect ("images/bg.jpg", 600, 400)     group:insert(bg)     bg.x = display.contentCenterX     bg.y = display.contentCenterY

and the other thing…

for my experience (very little by the way)

if you write a lot of code, like you did…

most people would not read it at all…

that might be why you have 0 answers…

try to ask one question only, very simple

one question at a time.

make it short, so people can read it fast.

if they want more info, then you post some code

but don’t make it long, they would not read it.

I hope all this helps a little, back in June 2013 I was trying to learn

I’m sharing with you, a few tips.

Victor

As it states in the docs:
" Touch id - A unique identifier of the chosen touch that enables you to distinguish
between multiple touches across different touch events. The id uniquely
identifies a given finger touching the screen as that touch changes
state, generating new touch events (moved, ended, and so forth).
"

Basically
it creates a unique id for each active touch event, so that way you
don’t get conflicting results when trying to use multiple controls at
the same time.
(simple way of saying it anyways;)

You’ve almost got it…

Here’s an example:

T = event.target

display.getCurrentStage():setFocus( T, event.id ) – sets the event.target as the focus and gives the event a unique id
T.isFocus = true

then on the ended phase of the touch -

display.getCurrentStage():setFocus( T, nil) – here we care calling the event.target’s event id we gave it earlier and setting it to nil
T.isFocus = falseand here we are saying that the event.target ( ‘T’ ) should no longer be the focus of an event

In my case I am doing this for my joystick, mine button, and rectangle - that use for detecting touch events for when the play wants to shoot - If I didn’t, then I would have conflicting events firing at the same time which would be a mess.

-Saer

P.S. Also, you can’t see/use multitouch in the simulator. You have to build for a device.
P.P.S. Victor, the forums have quite a few open posts regarding what is you are wanting to accomplish. Just search ’ Pinch and zoom’ and you should find some answers.

Hi dvelez60…

I’m trying to make multitouch also…

I want a big map and when people “pinch” their fingers

they can zoon in or out in the map

make it bigger or smaller so they need 2 fingers…

if you know how let me know, please.

One thing I can share with you that I know it will help you a lot

it’s the use of this – [ – the bracket

when you write a piece of code use this…(only the brackets and the word “code” inside the brackets)

------------- [---- code ----] ------- (please ignore all these lines ------) only the open bracket the word code and the closing bracket.

-------------- and write your code here, or copy and paste from your editor and then when done write the brackes with the "slash" the one under the ? sign like this   -------------

then at the end the opening bracket [the back slash / the word “code” and the closing bracket]

I hope you get it.

if you do that your code looks nicer and it’s easier to read…

like this

local bg = display.newImageRect ("images/bg.jpg", 600, 400)     group:insert(bg)     bg.x = display.contentCenterX     bg.y = display.contentCenterY

and the other thing…

for my experience (very little by the way)

if you write a lot of code, like you did…

most people would not read it at all…

that might be why you have 0 answers…

try to ask one question only, very simple

one question at a time.

make it short, so people can read it fast.

if they want more info, then you post some code

but don’t make it long, they would not read it.

I hope all this helps a little, back in June 2013 I was trying to learn

I’m sharing with you, a few tips.

Victor

As it states in the docs:
" Touch id - A unique identifier of the chosen touch that enables you to distinguish
between multiple touches across different touch events. The id uniquely
identifies a given finger touching the screen as that touch changes
state, generating new touch events (moved, ended, and so forth).
"

Basically
it creates a unique id for each active touch event, so that way you
don’t get conflicting results when trying to use multiple controls at
the same time.
(simple way of saying it anyways;)

You’ve almost got it…

Here’s an example:

T = event.target

display.getCurrentStage():setFocus( T, event.id ) – sets the event.target as the focus and gives the event a unique id
T.isFocus = true

then on the ended phase of the touch -

display.getCurrentStage():setFocus( T, nil) – here we care calling the event.target’s event id we gave it earlier and setting it to nil
T.isFocus = falseand here we are saying that the event.target ( ‘T’ ) should no longer be the focus of an event

In my case I am doing this for my joystick, mine button, and rectangle - that use for detecting touch events for when the play wants to shoot - If I didn’t, then I would have conflicting events firing at the same time which would be a mess.

-Saer

P.S. Also, you can’t see/use multitouch in the simulator. You have to build for a device.
P.P.S. Victor, the forums have quite a few open posts regarding what is you are wanting to accomplish. Just search ’ Pinch and zoom’ and you should find some answers.