--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
The touch listener is attached to bg.
It’s confusing because in moveRect( ) , event.target is called rect but it is moving the bg because that is the event.target.
-
Put the touch listener on the actual rectangle.
-
Change the name of rect in moveRect( ) to something like “target” to avoid confusion.
Or if your rectangle is named “rect” then just get rid of
[lua]
local rect = event.target
[/lua]
Then rect will refer to the rectangle and not the local “rect” which is actually “bg” (the event.target) and then you can use the background as your touch listener. I think that is what you were asking for but I’m not quite sure.
thanks for the response @sporkfin…it’s confusing, I get rid off
local rect = event.target
but nothing move, neither the background nor the rect.
I just want to move the rectangle using the background touch listener. As if the background were the touchpad of the rectangle. The rectangle is very small, I do not want the user to lose sight of the rectangle by putting his finger on the screen.
I say this all the time, but SSK has a solution for this.
-
Use SSK as per instructions: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
In your scene’s create() method add this code:
function scene:create( event ) local sceneGroup = self.view ssk.easyInputs.oneTouch.create( sceneGroup )
-
Add this code to the top of the file where you make your ‘player’ object
local function onOneTouch( self, event ) if(event.phase == “began”) self.x0 = self.x self.y0 = self.y else local dx = event.x - event.xStart local dy = event.y - event.yStart self.x = self.x0 + dx self.y = self.y0 + dy end end
-
When you create the player object do this
local player = … your create code player.onOneTouch = onOneTouch listen( “onOneTouch”, player ) function player.finalize( self ) ignoreList( { “onOneTouch” }, self ) end; player:addEventListener(“finalize”)
Any typos in my code may result in bugs, you should debug them on your own, just follow the clues in the console messages.
One final note:
I can think of about a half-dozen ways to do this. You simply need to understand:
Then you’re good to go.
I downloaded a copy of the star explorer code from here:
https://github.com/coronalabs/GettingStarted03
Then I modified it to allow dragging/tapping on the background to move the ship and fire.
Download this:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/08/GettingStarted03-master.zip
In the file you’ll see two folders:
Diff them to see what I changed.
The touch listener is attached to bg.
It’s confusing because in moveRect( ) , event.target is called rect but it is moving the bg because that is the event.target.
-
Put the touch listener on the actual rectangle.
-
Change the name of rect in moveRect( ) to something like “target” to avoid confusion.
Or if your rectangle is named “rect” then just get rid of
[lua]
local rect = event.target
[/lua]
Then rect will refer to the rectangle and not the local “rect” which is actually “bg” (the event.target) and then you can use the background as your touch listener. I think that is what you were asking for but I’m not quite sure.
thanks for the response @sporkfin…it’s confusing, I get rid off
local rect = event.target
but nothing move, neither the background nor the rect.
I just want to move the rectangle using the background touch listener. As if the background were the touchpad of the rectangle. The rectangle is very small, I do not want the user to lose sight of the rectangle by putting his finger on the screen.
I say this all the time, but SSK has a solution for this.
-
Use SSK as per instructions: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
In your scene’s create() method add this code:
function scene:create( event ) local sceneGroup = self.view ssk.easyInputs.oneTouch.create( sceneGroup )
-
Add this code to the top of the file where you make your ‘player’ object
local function onOneTouch( self, event ) if(event.phase == “began”) self.x0 = self.x self.y0 = self.y else local dx = event.x - event.xStart local dy = event.y - event.yStart self.x = self.x0 + dx self.y = self.y0 + dy end end
-
When you create the player object do this
local player = … your create code player.onOneTouch = onOneTouch listen( “onOneTouch”, player ) function player.finalize( self ) ignoreList( { “onOneTouch” }, self ) end; player:addEventListener(“finalize”)
Any typos in my code may result in bugs, you should debug them on your own, just follow the clues in the console messages.
One final note:
I can think of about a half-dozen ways to do this. You simply need to understand:
Then you’re good to go.
I downloaded a copy of the star explorer code from here:
https://github.com/coronalabs/GettingStarted03
Then I modified it to allow dragging/tapping on the background to move the ship and fire.
Download this:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/08/GettingStarted03-master.zip
In the file you’ll see two folders:
Diff them to see what I changed.