--create local bg = display.newRect( centerX, centerY, 768, 1024 ) bg.fill = data.greenWood sceneGroup:insert( bg ) bg:addEventListener( "touch", moveRect)
Up my background created in the scene:create.
I already uploaded my first app and now I’m trying to make my first game. In which I need to use a touch listener in the background image to move a rectangle. I’m doing my first game based on the Corona game example “Star Explorer” in which a touch listener is used to move the ship when you touch the ship, I want to use the background touch listener to move my rectangle when the uses move his finger in the screen.
The problem is that I am moving the background when I touch the screen. How can I move the “rect” using a touch listener in the background?
Here is the function code, basically identical to the “Star Explorer”
local function moveRect( event ) local rect= event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the rect display.currentStage:setFocus( rect ) -- Store initial offset position rect.touchOffsetX = event.x - rect.x elseif ( "moved" == phase ) then -- Move the rect to the new touch position rect.x = event.x - rect.touchOffsetX elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the rect display.currentStage:setFocus( nil ) end return true -- Prevents touch propagation to underlying objects end