Reputation: 1
I'm trying to print taps from 3 different display objects but more than the one tapped are printed in the Terminal. They need their own specific function but that's not possible as of now. What should I do? I am new to Corona and LUA.
-- BOBLER DISPLAY OBJEKTER
local sport1 = display.newImage("images/sport1.png")
sport1.id = "mySport1"
sport1.x = 120
sport1.y = 90
sport1:scale(1,1)
local gossip1 = display.newImage("images/gossip1.png")
gossip1.id = "myGossip1"
gossip1.x = 400
gossip1.y = 120
gossip1:scale(1,1)
local kultur1 = display.newImage("images/kultur1.png")
kultur1.id = "myKultur1"
kultur1.x = 250
kultur1.y = 200
kultur1:scale(1,1)
local function onSport1Tap( self, event )
print(self.id .. " was tapped." )
end
local function onGossip1Tap( self, event )
print(self.id .. " was tapped." )
end
local function onKultur1Tap( self, event )
print(self.id .. " was tapped." )
end
-- TAP addEVENTLISTENER
sport1.tap = onSport1Tap
sport1:addEventListener( "tap", sport1 )
gossip1.tap = onGossip1Tap
gossip1:addEventListener( "tap", gossip1 )
kultur1.tap = onKultur1Tap
kultur1:addEventListener( "tap", kultur1 )
Upvotes: 0
Views: 1569
Reputation: 124
just return true
at the end of all your tap functions, like this:
local function onKultur1Tap( self, event )
print(self.id .. " was tapped." )
return true -- ** just it **
end
Upvotes: 0
Reputation: 53
I suggest you do the following things:
Use this function for tap/touch events:
function sport1:touch(e)
if e.phase == "ended" then
print(self.id.." was tapped")
end
end
Use tables to create your objects more effectively, especially if you plan on adding more objects.
Here is an example of how:
local objects = {}
object[ 1 ] = {id = mySport1, x = 120, y = 90} --by the way, the scale is (1,1) on default
object[ 2 ] = {...}
object[ 3 ] = {...}
Then one can easily create the information for all of them using for loops:
for i = 1, #objects do
local object[i].img = display.newImage("images/"..object[i].id..".png")
object[i].img.x, object[i].img.y = object[i].x, object[i].y
local shape = object[i].img
function shape:touch(e)
if e.phase == "ended" then
print(object[i].id.."was tapped")
end
shape:addEventListener("touch")
end
I hope this was not too advanced... It took me a while to understand the potency of tables, but they become very effective when you have a lot of paramaters or objects needed to be created. Regarding the touch function, I have not really worked with tap - I just believe touch is better and simpler to use.
Upvotes: 1