Hello,
I want to add an event listener to half of my background
(i.e. to upper half of the background). How can I implement this?
Take whatever image you have for a background and using a graphic tool like gimp or paintshop or such and split the background image into 2 separate textures/images. Each image will need to be half the height of the display.
local bkgd = display.newGroup() local bkgdTop = display.newImage("top.png") local bkgdBot = display.newImage("bot.png") bkgd:insert( bkgdTop ) bkgd:insert( bkgdBot ) -- position the top and bot images in the group. bkgdTop.x = display.contentWidth \* .5 bkgdTop.y = display.contentHeight \* .25 bkgdBot.x = display.contentWidth \* .5 bkgdBot.y = display.contentHeight \* .75 local function onTouch(e) if e.phase == "ended" then print(" top half touched ") end return true end bkgdTop:addEventListener("touch", onTouch)
you could also use the single background image and in the ‘onTouch’ event check the location of the touch event to determine id the touch is in the top half of the screen.
local function onTouch(e) if e.phase == "ended" then if e.y \< display.contentHeight \* .5 then print(" top half touched ") else -- don't do anything .. as you only want action -- on the top half of background print(" bot half touched ") end end return true end
or add a .isVisible=false + .isHitTestable=true rect over the upper half and put your touch listener on THAT instead
Take whatever image you have for a background and using a graphic tool like gimp or paintshop or such and split the background image into 2 separate textures/images. Each image will need to be half the height of the display.
local bkgd = display.newGroup() local bkgdTop = display.newImage("top.png") local bkgdBot = display.newImage("bot.png") bkgd:insert( bkgdTop ) bkgd:insert( bkgdBot ) -- position the top and bot images in the group. bkgdTop.x = display.contentWidth \* .5 bkgdTop.y = display.contentHeight \* .25 bkgdBot.x = display.contentWidth \* .5 bkgdBot.y = display.contentHeight \* .75 local function onTouch(e) if e.phase == "ended" then print(" top half touched ") end return true end bkgdTop:addEventListener("touch", onTouch)
you could also use the single background image and in the ‘onTouch’ event check the location of the touch event to determine id the touch is in the top half of the screen.
local function onTouch(e) if e.phase == "ended" then if e.y \< display.contentHeight \* .5 then print(" top half touched ") else -- don't do anything .. as you only want action -- on the top half of background print(" bot half touched ") end end return true end
or add a .isVisible=false + .isHitTestable=true rect over the upper half and put your touch listener on THAT instead