[SOLVED] Problem With Multitouch

Hi again thanks for the people that help me on the last post here https://developer.anscamobile.com/forum/2012/03/24/help-adding-xcode-432

Anyways back to what I got to say. What I’m trying to do is have a plane shoot at the same time while the person moves it. The problem is when I try to move the plane and try to shoot at the same time the bullets does not come out. Or the plane just starts to go offscreen, or center screen I don’t know why

Here is the code

[code] system.activate( “multitouch” )
local ui = require(“ui”)

limits = 0

local function powertwin2 ()
extrabull = loadFile (“extra.txt”)
if extrabull == “empty” then
extrabull = 2
saveFile(“extra.txt”, extrabull)
end
end
powertwin2()

local function bulletsgood2()
if tonumber(extrabull) == 2 then
limits = 50
limitsbullet = display.newText(“Ammo:50”, 30, 30,“Arial”, 18)
localGroup:insert(limitsbullet)
end
end
timer.performWithDelay(100, bulletsgood2, 1)


------------Right Below Here is were I move the Plane by drag Up and Down------------
local canBeDragged = true

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

local phase = event.phase
if canBeDragged == true then
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 = “static”

elseif t.isFocus then
if “moved” == phase then
–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

– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “static”
end

end
end
end

– Stop further propagation of touch event!
return true
end

local box = display.newRect(-40, 100, 30, 30)
localGroup:insert(box)

–local test = require (“game2”)
–local data = test.getSpriteSheetData()
–local game = sprite.newSpriteSheetFromData( “hells2.png”, data )
local sheet1 = sprite.newSpriteSheet( “work4.png”, 92, 63 )
– Defining the sprite we’re going to be using
local heroset = sprite.newSpriteSet (sheet1, 1, 7)
sprite.add (heroset, “hero”, 1, 7, 200, 0)

local heli = sprite.newSprite (heroset)
heli.x = 50
heli.y = 100
heli.hit = “heli”
physics.addBody(heli ,“static”,{radius = 20})
localGroup:insert(heli)
heli:prepare(“hero”)
heli:play()
heli:addEventListener( “touch”, startDrag )


------------Right Above Here is were I move the Plane by drag Up and Down------------


------------Right Below Here is were I try to shoot while I’m dragging the Plane Up and Down------------

local function shoot (event)
if event.phase == “began” then
if limits == 0 then
great = true
end
if great == false then
if tonumber(twinb) == 1 then
local bullet = display.newImageRect(“bullet.png”, 10, 10)
bullet.x = heli.x + 40
bullet.y = heli.y
bullet.hit = “bullet”
bullet.isFixedRotation = true
transition.to (bullet, {time = 2000, x=550})
physics.addBody (bullet, {bounce=0.6})
limits = limits - 1
limitsbullet.text = “Ammo:” …limits
shooting = audio.play( shots)
localGroup:insert(bullet)
bullet:addEventListener(“collision”, bullet)
function bullet:collision (event)
if event.other.hit ~= “heli” then
bullet:removeSelf()
end
end
elseif tonumber(twinb) == 2 then
local twin = display.newImageRect(“bullet.png”, 10, 10)
twin.x = heli.x + 40
twin.y = heli.y
twin.hit = “twin”
twin.isFixedRotation = true
transition.to (twin, {time = 2000, x=550})
physics.addBody (twin, {bounce=0.6})
localGroup:insert(twin)
local twin2 = display.newImageRect(“bullet.png”, 10, 10)
twin2.x = heli.x + 40
twin2.y = heli.y + 15
twin2.hit = “twin”
twin2.isFixedRotation = true
transition.to (twin2, {time = 2000, x=550})
physics.addBody (twin2, {bounce=0.6})
localGroup:insert(twin2)
twin:addEventListener(“collision”, twin)
function twin:collision (event)
if event.other.hit ~= “heli” then
twin:removeSelf()
twin2:removeSelf()
end
end
elseif tonumber(twinb) == 3 then
local bullet3 = display.newImageRect(“rocket.png”, 25, 25)
bullet3.x = heli.x + 45
bullet3.y = heli.y
bullet3.hit = “bullet3”
bullet3.isFixedRotation = true
transition.to (bullet3, {time = 2000, x=550})
physics.addBody (bullet3, {bounce=0.6, isSensor = true})
localGroup:insert(bullet3)
limits = limits - 1
limitsbullet.text = “Ammo:” …limits
shooting = audio.play( shots)
bullet3:addEventListener(“collision”, bullet3)
function bullet3:collision (event)
if event.other.hit ~= “heli” then
bullet3:removeSelf()
end
end

end
end
end
end

ButtonA = ui.newButton{
default = “buttonoverA.png”,
over = “buttonA.png”,
x = 360,
y = 280,
onEvent = shoot,
}
localGroup:insert(ButtonA)

[/code]

Now How does it need to be fix? I try to find it myself and no use [import]uid: 17058 topic_id: 23846 reply_id: 323846[/import]

can anyone please help me [import]uid: 17058 topic_id: 23846 reply_id: 96089[/import]

I really want to help you, cause I find myself in the same boat, helping myself, lack of support, etc. But you have so many images and ancillary material in your code it is hard to take on the task of getting it up and running to deduce the issue(s).

See if you can bake this down into using simple ojects (rects, circles, etc) and not using any ancillary material. Something that simply needs to be copied and pasted to execute. When people take the time to actually do this (few and far between), I will jump right in and try to solve. Even when they don’t, I’ll attempt if I only need to swap a few assets with vector objects.

See what you can do… :slight_smile: [import]uid: 21331 topic_id: 23846 reply_id: 96099[/import]

@TheRealTonyK

Ok Here I broke it down not using images but rectangles and cricles Is now a plug and play code

 system.activate( "multitouch" )  
   
limits = 0  
 --[[  
local function powertwin2 ()  
 extrabull = loadFile ("extra.txt")  
 if extrabull == "empty" then  
 extrabull = 2  
 saveFile("extra.txt", extrabull)  
 end  
end  
powertwin2()  
 -]]  
   
 local limits = 0  
 local shoot = true  
   
 local physics = require ("physics")  
physics.start()  
--physics.setDrawMode("hybrid")  
physics.setGravity(0,0)  
local function bulletsgood2()  
if shoot == true then  
 limits = 50  
limitsbullet = display.newText("Ammo:50", 30, 30,"Arial", 18)  
end  
end  
timer.performWithDelay(100, bulletsgood2, 1)  
   
--------------------------------------------------------------------  
------------Right Below Here is were I move the Plane by drag Up and Down------------  
local canBeDragged = true  
   
local function startDrag( event )  
 local t = event.target  
   
 local phase = event.phase  
 if canBeDragged == true then  
 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 = "static"  
  
 elseif t.isFocus then  
 if "moved" == phase then  
 --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  
  
 -- Switch body type back to "dynamic", unless we've marked this sprite as a platform  
 if ( not event.target.isPlatform ) then  
 event.target.bodyType = "static"  
 end  
   
 end  
 end  
 end  
   
 -- Stop further propagation of touch event!  
 return true  
end  
   
local heli = display.newCircle (0,0, 50,50)   
heli.x = 50  
heli.y = 100  
heli.hit = "heli"  
physics.addBody(heli ,"static",{radius = 20})  
heli:addEventListener( "touch", startDrag )  
   
local button = display.newRect(200, 280, 50, 50)  
   
local function shoot (event)  
if event.phase == "began" then  
great = true   
if great == true then  
local bullet = display.newRect(0,0, 10, 10)  
bullet.x = heli.x + 40  
bullet.y = heli.y  
bullet.hit = "bullet"  
bullet.isFixedRotation = true  
transition.to (bullet, {time = 2000, x=550})  
physics.addBody (bullet, {bounce=0.6})  
limits = limits - 1  
limitsbullet.text = "Ammo:" ..limits  
shooting = audio.play( shots)   
bullet:addEventListener("collision", bullet)   
 function bullet:collision (event)  
 if event.other.hit ~= "heli" then  
 bullet:removeSelf()  
 end  
 end  
elseif tonumber(twinb) == 2 then  
local twin = display.newImageRect(0,0, 50, 50)  
twin.x = heli.x + 40  
twin.y = heli.y  
twin.hit = "twin"  
twin.isFixedRotation = true  
transition.to (twin, {time = 2000, x=550})  
physics.addBody (twin, {bounce=0.6})  
localGroup:insert(twin)  
local twin2 = display.newImageRect(0,0, 50, 50)  
twin2.x = heli.x + 40  
twin2.y = heli.y + 15  
twin2.hit = "twin"  
twin2.isFixedRotation = true  
transition.to (twin2, {time = 2000, x=550})  
physics.addBody (twin2, {bounce=0.6})  
twin:addEventListener("collision", twin)  
 function twin:collision (event)  
 if event.other.hit ~= "heli" then  
 twin:removeSelf()  
 twin2:removeSelf()  
 end  
 end  
 elseif tonumber(twinb) == 3 then  
 local bullet3 = display.newImageRect("rocket.png", 25, 25)  
bullet3.x = heli.x + 45  
bullet3.y = heli.y  
bullet3.hit = "bullet3"  
bullet3.isFixedRotation = true  
transition.to (bullet3, {time = 2000, x=550})  
physics.addBody (bullet3, {bounce=0.6, isSensor = true})  
limits = limits - 1  
limitsbullet.text = "Ammo:" ..limits  
shooting = audio.play( shots)   
bullet3:addEventListener("collision", bullet3)   
 function bullet3:collision (event)  
 if event.other.hit ~= "heli" then  
 bullet3:removeSelf()  
 end  
 end  
  
end  
end  
end  
button:addEventListener("touch", shoot)  

Done I edited it works here is a plug play version [import]uid: 17058 topic_id: 23846 reply_id: 96105[/import]

@TheRealTonyKYour going to see a square and circle.
Circle is suppose be a plane you drag it up and down.

Square is suppose to be the shooting button. Im trying to do here is shoot and move at the same time [import]uid: 17058 topic_id: 23846 reply_id: 96110[/import]

OK, good job. I have to step out for a few hours, but I promise I will take a look at it (unless someone beats me to it) later on. We’ll get it figured out! :wink:

Edit: After a super quick look at it, you may need to look at the docs for display.getCurrentStage and pass the event.id with it, but I’m not sure… just something you could look into…
[import]uid: 21331 topic_id: 23846 reply_id: 96115[/import]

@TheRealTonyK I looked into the docs of event.id and, display.getCurrentStage. I understand how that would go with my multitouch, but I would not know how to add it becasue I’m still a newbie at this.

And also take your time off what you need to do. [import]uid: 17058 topic_id: 23846 reply_id: 96117[/import]

I’m going to go ahead and paste the whole code again. I actually did not test the code before I made the changes, but I did change your two setFocus points as stated above for you to try and tested on the iPad 1. Works good. Here ya go:

system.activate( "multitouch" )  
   
limits = 0  
 --[[  
local function powertwin2 ()  
 extrabull = loadFile ("extra.txt")  
 if extrabull == "empty" then  
 extrabull = 2  
 saveFile("extra.txt", extrabull)  
 end  
end  
powertwin2()  
 -]]  
   
local limits = 0  
local shoot = true  
   
local physics = require ("physics")  
physics.start()  
--physics.setDrawMode("hybrid")  
physics.setGravity(0,0)  
  
local function bulletsgood2()  
 if shoot == true then  
 limits = 50  
 limitsbullet = display.newText("Ammo:50", 30, 30,"Arial", 18)  
 end  
end  
timer.performWithDelay(100, bulletsgood2, 1)  
   
--------------------------------------------------------------------  
------------Right Below Here is were I move the Plane by drag Up and Down------------  
local canBeDragged = true  
   
local function startDrag( event )  
 local t = event.target  
 local phase = event.phase  
  
 if canBeDragged == true then  
 if "began" == phase then  
 --display.getCurrentStage():setFocus( t )  
 display.getCurrentStage():setFocus( t, event.id )  
 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 = "static"  
  
 elseif t.isFocus then  
 if "moved" == phase then  
 --t.x = event.x - t.x0  
 t.y = event.y - t.y0  
  
 elseif "ended" == phase or "cancelled" == phase then  
 --display.getCurrentStage():setFocus( nil )  
 display.getCurrentStage():setFocus(t, 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 = "static"  
 end  
 end  
 end  
 end  
  
 -- Stop further propagation of touch event!  
 return true  
end  
   
local heli = display.newCircle (0,0, 50,50)   
heli.x = 50  
heli.y = 100  
heli.hit = "heli"  
physics.addBody(heli ,"static",{radius = 20})  
heli:addEventListener( "touch", startDrag )  
   
local button = display.newRect(200, 280, 50, 50)  
   
local function shoot (event)  
 if event.phase == "began" then  
 great = true   
 --realize that this is redundant, you set to true above then check for it being true, just fyi  
 if great == true then  
 local bullet = display.newRect(0,0, 10, 10)  
 bullet.x = heli.x + 40  
 bullet.y = heli.y  
 bullet.hit = "bullet"  
 bullet.isFixedRotation = true  
 transition.to (bullet, {time = 2000, x=550})  
 physics.addBody (bullet, {bounce=0.6})  
 limits = limits - 1  
 limitsbullet.text = "Ammo:" ..limits  
 shooting = audio.play( shots)   
 bullet:addEventListener("collision", bullet)   
  
 function bullet:collision (event)  
 if event.other.hit ~= "heli" then  
 bullet:removeSelf()  
 end  
 end  
 elseif tonumber(twinb) == 2 then  
 --realize that this code will never be executed due to the "great" setting marked above  
 local twin = display.newImageRect(0,0, 50, 50)  
 twin.x = heli.x + 40  
 twin.y = heli.y  
 twin.hit = "twin"  
 twin.isFixedRotation = true  
 transition.to (twin, {time = 2000, x=550})  
 physics.addBody (twin, {bounce=0.6})  
 localGroup:insert(twin)  
 local twin2 = display.newImageRect(0,0, 50, 50)  
 twin2.x = heli.x + 40  
 twin2.y = heli.y + 15  
 twin2.hit = "twin"  
 twin2.isFixedRotation = true  
 transition.to (twin2, {time = 2000, x=550})  
 physics.addBody (twin2, {bounce=0.6})  
 twin:addEventListener("collision", twin)  
 function twin:collision (event)  
 if event.other.hit ~= "heli" then  
 twin:removeSelf()  
 twin2:removeSelf()  
 end  
 end  
 elseif tonumber(twinb) == 3 then  
 local bullet3 = display.newImageRect("rocket.png", 25, 25)  
 bullet3.x = heli.x + 45  
 bullet3.y = heli.y  
 bullet3.hit = "bullet3"  
 bullet3.isFixedRotation = true  
 transition.to (bullet3, {time = 2000, x=550})  
 physics.addBody (bullet3, {bounce=0.6, isSensor = true})  
 limits = limits - 1  
 limitsbullet.text = "Ammo:" ..limits  
 shooting = audio.play( shots)   
 bullet3:addEventListener("collision", bullet3)   
 function bullet3:collision (event)  
 if event.other.hit ~= "heli" then  
 bullet3:removeSelf()  
 end  
 end  
  
 end  
 end  
end  
button:addEventListener("touch", shoot)  

Good luck, can’t wait to see what you do! :slight_smile: [import]uid: 21331 topic_id: 23846 reply_id: 96157[/import]

@TheRealTonyK Thanks so much I also tested on mY IPad it works like a charm. Thanks a lot for your help :slight_smile: [import]uid: 17058 topic_id: 23846 reply_id: 96161[/import]

Gratitude much appreciated and you are most welcome. Good Luck, and marked solved if you get a chance. :wink:
[import]uid: 21331 topic_id: 23846 reply_id: 96185[/import]