Multitouch while shooting

Hello,

I have a game where the player moves by touching,holding, and moving there finger across the screen. There is also a button that the player can shoot while they are moving. I activate multitouch but some things happen. When I click the button while moving, the player shoots twice and sometimes even shoots all its ammo. Is there a way to have it where the player can have their finger touching to move (moving the character) and tap once without tap on touch and release so that the player only shoots once. 

Thanks for your help,

-Brandon

Hi Brandon,

Handling multitouch can be tricky, but almost every scenario is possible. For you, it may be a matter of carefully checking which “ID” of the touch is where, i.e. is the touch ID that hit the “shoot” button the same ID as the touch point that is being used to move? Most scenarios are solved using this kind of conditional checking.

Take care,

Brent

I think I have everything set up correctly but I’m not entirely sure here is the code for that section.

screenButton = display.newRect(display.contentWidth/2, display.contentHeight/2 - 120, display.actualContentWidth, display.actualContentHeight ) gui.front:insert(screenButton) screenButton:setFillColor(255,255,255, 0.01) screenButton:addEventListener( "touch", screenTapListener ) screenButton2 = display.newRect(display.contentWidth/2 - 110, display.contentHeight/2, display.actualContentWidth, display.actualContentHeight ) gui.front:insert(screenButton2) screenButton2:setFillColor(255,255,255, 0.01) screenButton2:addEventListener( "touch", screenTapListener ) shootbtn = display.newImage( "shootbutton.png") shootbtn.x = display.contentWidth -50 shootbtn.y = display.contentHeight -50 gui.overlay:insert(shootbtn) system.activate( "multitouch" ) function shoot(event) if ammo \>= 1 then local bullet = display.newImage("bullet.png") physics.addBody(bullet, "kinematic", {bounce = 0, isFixedRotation = true}); bullet.x = player.x bullet.y = player.y - 50 bullet.myType = "bullet" transition.to ( bullet, { time = 1000, x = player.x, y =-100} ) ammo = ammo - 1 ammoText.isVisible = false ammoText= display.newText( "Ammo: "..ammo,0,0, ARCADEPIX, 70 ) gui.overlay:insert(ammoText) ammoText.anchorX = 0 ammoText.anchorY = 0 ammoText.x = display.screenOriginX + 30 ammoText.y = display.screenOriginY + 170 + bannerOffset end return true end shootbtn:addEventListener ( "touch", shoot )

Hi Brandon,

I don’t see any usage of the touch ID. I suggest that you read the guide on touch/tap/multitouch and fully understand how it works before implementing it in your game.

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

Brent

I’m pretty new to corona and the event.id thing seems to really confuse me. What would I have to add so that I can touch my shoot button and it only shoots once?

Thanks for understanding my noobishness,

-Brandon

Hi Brandon,

Well unfortunately, it’s not as simple as adding one or two things. When implementing multitouch, you need to conditionally check touches based on their ID, so that you don’t have conflicts occurring.

Consider two touch IDs as “A” and “B” (if you print() the actual IDs of the touch points they will be Lua userdata objects, but don’t worry about that right now). Now imagine the player starts the game, and there are no touches on the screen (he/she is waiting to play). Now the game begins, and the player touches the shoot button… that first touch ID becomes “A”. Now, the user touches the screen with another finger, so that becomes “B”. Next, the user releases the shoot button, so “A” becomes invalid, and you only have “B” on the screen. When the user touches the shoot button again, that touch ID becomes “C”, and “A” is forever gone… you will never get the same touch ID in different touches; they are treated as one ID throughout the entire life of that touch, and then the ID is discarded.

The point is, you need to detect if the user is still touching the shoot button with a specific ID and then, if the user touches the shoot button with a different finger, that ID will obviously  not be the same. So, because you know they’re touching the button with the current touch ID that they touched the button with, you can just ignore the second touch event completely.

Hope this helps,

Brent

Hi Brandon,

Handling multitouch can be tricky, but almost every scenario is possible. For you, it may be a matter of carefully checking which “ID” of the touch is where, i.e. is the touch ID that hit the “shoot” button the same ID as the touch point that is being used to move? Most scenarios are solved using this kind of conditional checking.

Take care,

Brent

I think I have everything set up correctly but I’m not entirely sure here is the code for that section.

screenButton = display.newRect(display.contentWidth/2, display.contentHeight/2 - 120, display.actualContentWidth, display.actualContentHeight ) gui.front:insert(screenButton) screenButton:setFillColor(255,255,255, 0.01) screenButton:addEventListener( "touch", screenTapListener ) screenButton2 = display.newRect(display.contentWidth/2 - 110, display.contentHeight/2, display.actualContentWidth, display.actualContentHeight ) gui.front:insert(screenButton2) screenButton2:setFillColor(255,255,255, 0.01) screenButton2:addEventListener( "touch", screenTapListener ) shootbtn = display.newImage( "shootbutton.png") shootbtn.x = display.contentWidth -50 shootbtn.y = display.contentHeight -50 gui.overlay:insert(shootbtn) system.activate( "multitouch" ) function shoot(event) if ammo \>= 1 then local bullet = display.newImage("bullet.png") physics.addBody(bullet, "kinematic", {bounce = 0, isFixedRotation = true}); bullet.x = player.x bullet.y = player.y - 50 bullet.myType = "bullet" transition.to ( bullet, { time = 1000, x = player.x, y =-100} ) ammo = ammo - 1 ammoText.isVisible = false ammoText= display.newText( "Ammo: "..ammo,0,0, ARCADEPIX, 70 ) gui.overlay:insert(ammoText) ammoText.anchorX = 0 ammoText.anchorY = 0 ammoText.x = display.screenOriginX + 30 ammoText.y = display.screenOriginY + 170 + bannerOffset end return true end shootbtn:addEventListener ( "touch", shoot )

Hi Brandon,

I don’t see any usage of the touch ID. I suggest that you read the guide on touch/tap/multitouch and fully understand how it works before implementing it in your game.

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html#multitouch

Brent

I’m pretty new to corona and the event.id thing seems to really confuse me. What would I have to add so that I can touch my shoot button and it only shoots once?

Thanks for understanding my noobishness,

-Brandon

Hi Brandon,

Well unfortunately, it’s not as simple as adding one or two things. When implementing multitouch, you need to conditionally check touches based on their ID, so that you don’t have conflicts occurring.

Consider two touch IDs as “A” and “B” (if you print() the actual IDs of the touch points they will be Lua userdata objects, but don’t worry about that right now). Now imagine the player starts the game, and there are no touches on the screen (he/she is waiting to play). Now the game begins, and the player touches the shoot button… that first touch ID becomes “A”. Now, the user touches the screen with another finger, so that becomes “B”. Next, the user releases the shoot button, so “A” becomes invalid, and you only have “B” on the screen. When the user touches the shoot button again, that touch ID becomes “C”, and “A” is forever gone… you will never get the same touch ID in different touches; they are treated as one ID throughout the entire life of that touch, and then the ID is discarded.

The point is, you need to detect if the user is still touching the shoot button with a specific ID and then, if the user touches the shoot button with a different finger, that ID will obviously  not be the same. So, because you know they’re touching the button with the current touch ID that they touched the button with, you can just ignore the second touch event completely.

Hope this helps,

Brent