How to do CurrentTouch?

Hi all!

I have been working with an iPad app that uses Lua, but it left a lot to be desired when it comes to making apps. Anyway, in Lua on that app if you had a button that was 256x128 at WIDTH/2 and HEIGHT/2 and you wanted to read if the button was touched, you would:

if CurrentTouch.state==BEGAN then

    if CurrentTouch.x>WIDTH/2-128 and CurrentTouch.x<WIDTH/2+128 and

    CurrentTouch.y>HEIGHT/2-64 and CurrentTouch.y<HEIGHT/2+64 then

         – insert any action you want the button to do

    end

end

How would I go about doing this in Corona?

Edit: random side question so I don’t have to start a new thread: How can I make an image move in a certain direction at a certain velocity? I don’t want acceleration, just a fixed velocity.

In the Lua I am used to, the draw() function refreshes at 60 frames per second, so to move an object you would just put image.x = image.x +2 and the image would move 2 in the x axis every draw. Corona Lua doesn’t seem to work with 60 draws per second though.

Any help would be greatly appreciated!

Hi @crumble619,

In your first code block, there are some syntax issues. To check the touch “state” as you write, it’s actually “phase”. Have you read through the guide on Tap/Touch/Multitouch?

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

On that note, if this is a basic button, you should consider using widget-based buttons:

http://docs.coronalabs.com/api/library/widget/newButton.html

As for your second question (moving an image), you should probably start a new post on that. You can move objects in 3 core ways:

  1. Update the position in a Runtime listener or loop.

  2. Use the transition library: http://docs.coronalabs.com/api/library/transition/index.html

  3. Make the object into a physics object and use the physics library: http://docs.coronalabs.com/api/library/physics/index.html

Hope this helps,

Brent

Thanks much for the info! Still trying to digest all of this information.

I used Tap for the button, but now I’m getting an error when I use the button to switch to the mainGame() scene… says that mainGame is a nil value. I’m getting the same error when I use collision to change scenes from the mainGame() scene to the tryAgain() scene. Not sure what I’m doing wrong there…

Here is the code, bolded where I get errors, able to see what I’m doing wrong?

Really appreciate the help!


display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setGravity(0,0)

local W = display.contentWidth  
local H = display.contentHeight

local asteroid  
local frog  
local background  
local button  
local text

local mRandom=math.random

**local function astCollision(self, event)  
&nbsp;if event.phase == "began" then  
&nbsp;&nbsp;if event.target.type == "asteroid" and event.other.type == "frog" then  
&nbsp;tryAgain()  
&nbsp;return true  
&nbsp;end  
&nbsp;end  
end**

local function asteroids()  
&nbsp;asteroid = display.newImage("Images/ast.png")  
&nbsp;asteroid.width = 256  
&nbsp;asteroid.height = 256  
&nbsp;asteroid.x = mRandom(128,W-128)  
&nbsp;asteroid.y = - 256  
&nbsp;physics.addBody(asteroid, {0,0,0})  
&nbsp;asteroid:setLinearVelocity(0,600)  
&nbsp;asteroid.collision = astCollision  
&nbsp;asteroid:addEventListener("collision", asteroid)  
&nbsp;asteroid.type = "asteroid"  
end

local function frog()  
&nbsp;frog = display.newImage("Images/Frog6.png")  
&nbsp;frog.x = W/2  
&nbsp;frog.y = H/1.2  
&nbsp;frog.width = 96  
&nbsp;frog.height = 128  
&nbsp;physics.addBody(frog, "static", {0,0,0})  
&nbsp;frog.type = "frog"  
end

local function background()  
&nbsp;background = display.newImage("Images/Background.png")  
&nbsp;background.x = W/2  
&nbsp;background.y = H/2  
&nbsp;background.width = W  
&nbsp;background.height = H  
end

**local function myTapListener(event)  
&nbsp;if (event.target) then  
&nbsp;MainGame()  
&nbsp;return true  
&nbsp;end  
end**

local function button()  
&nbsp;button = display.newImage("Images/bluebutton.png")  
&nbsp;button.x = W/2  
&nbsp;button.y = H/2  
&nbsp;button:addEventListener("tap",myTapListener)  
end

local function mainMenu()  
&nbsp;background()  
&nbsp;button()  
&nbsp;text = display.newText("Play Game",W/2,H/2,nativesystemFont,64)  
end

local function mainGame()  
&nbsp;background()  
&nbsp;frog()  
&nbsp;timer.performWithDelay(1000,asteroids,0)  
end

local function tryAgain()  
&nbsp;background()  
&nbsp;button()  
&nbsp;text = display.newText("Try Again",W/2,H/2,nativesystemFont,64)  
end

mainMenu()

There are a couple things wrong with your code:

  1. In Lua, all local functions have to be declared above where they are used. You call tryAgain in astCollision() but it isn’t defined until near the end of your file. The same issue exists with mainGame.

One way to get around this is to declare all your local functions at the top of the file by simply doing

local tryAgain local mainGame

and then later you can actually define them

tryAgain = function() &nbsp;&nbsp; ... end mainMenu = function() &nbsp;&nbsp; ... end
  1. I don’t know if it’s just copy/paste error but Lua is case-sensitive, and in myTapListener() you call MainGame, but it is defined as mainGame down below.

Hope that helps.

Awesome! Thanks much! This takes a little getting used to, appreciate it.

Hi @crumble619,

In your first code block, there are some syntax issues. To check the touch “state” as you write, it’s actually “phase”. Have you read through the guide on Tap/Touch/Multitouch?

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

On that note, if this is a basic button, you should consider using widget-based buttons:

http://docs.coronalabs.com/api/library/widget/newButton.html

As for your second question (moving an image), you should probably start a new post on that. You can move objects in 3 core ways:

  1. Update the position in a Runtime listener or loop.

  2. Use the transition library: http://docs.coronalabs.com/api/library/transition/index.html

  3. Make the object into a physics object and use the physics library: http://docs.coronalabs.com/api/library/physics/index.html

Hope this helps,

Brent

Thanks much for the info! Still trying to digest all of this information.

I used Tap for the button, but now I’m getting an error when I use the button to switch to the mainGame() scene… says that mainGame is a nil value. I’m getting the same error when I use collision to change scenes from the mainGame() scene to the tryAgain() scene. Not sure what I’m doing wrong there…

Here is the code, bolded where I get errors, able to see what I’m doing wrong?

Really appreciate the help!


display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setGravity(0,0)

local W = display.contentWidth  
local H = display.contentHeight

local asteroid  
local frog  
local background  
local button  
local text

local mRandom=math.random

**local function astCollision(self, event)  
&nbsp;if event.phase == "began" then  
&nbsp;&nbsp;if event.target.type == "asteroid" and event.other.type == "frog" then  
&nbsp;tryAgain()  
&nbsp;return true  
&nbsp;end  
&nbsp;end  
end**

local function asteroids()  
&nbsp;asteroid = display.newImage("Images/ast.png")  
&nbsp;asteroid.width = 256  
&nbsp;asteroid.height = 256  
&nbsp;asteroid.x = mRandom(128,W-128)  
&nbsp;asteroid.y = - 256  
&nbsp;physics.addBody(asteroid, {0,0,0})  
&nbsp;asteroid:setLinearVelocity(0,600)  
&nbsp;asteroid.collision = astCollision  
&nbsp;asteroid:addEventListener("collision", asteroid)  
&nbsp;asteroid.type = "asteroid"  
end

local function frog()  
&nbsp;frog = display.newImage("Images/Frog6.png")  
&nbsp;frog.x = W/2  
&nbsp;frog.y = H/1.2  
&nbsp;frog.width = 96  
&nbsp;frog.height = 128  
&nbsp;physics.addBody(frog, "static", {0,0,0})  
&nbsp;frog.type = "frog"  
end

local function background()  
&nbsp;background = display.newImage("Images/Background.png")  
&nbsp;background.x = W/2  
&nbsp;background.y = H/2  
&nbsp;background.width = W  
&nbsp;background.height = H  
end

**local function myTapListener(event)  
&nbsp;if (event.target) then  
&nbsp;MainGame()  
&nbsp;return true  
&nbsp;end  
end**

local function button()  
&nbsp;button = display.newImage("Images/bluebutton.png")  
&nbsp;button.x = W/2  
&nbsp;button.y = H/2  
&nbsp;button:addEventListener("tap",myTapListener)  
end

local function mainMenu()  
&nbsp;background()  
&nbsp;button()  
&nbsp;text = display.newText("Play Game",W/2,H/2,nativesystemFont,64)  
end

local function mainGame()  
&nbsp;background()  
&nbsp;frog()  
&nbsp;timer.performWithDelay(1000,asteroids,0)  
end

local function tryAgain()  
&nbsp;background()  
&nbsp;button()  
&nbsp;text = display.newText("Try Again",W/2,H/2,nativesystemFont,64)  
end

mainMenu()

There are a couple things wrong with your code:

  1. In Lua, all local functions have to be declared above where they are used. You call tryAgain in astCollision() but it isn’t defined until near the end of your file. The same issue exists with mainGame.

One way to get around this is to declare all your local functions at the top of the file by simply doing

local tryAgain local mainGame

and then later you can actually define them

tryAgain = function() &nbsp;&nbsp; ... end mainMenu = function() &nbsp;&nbsp; ... end
  1. I don’t know if it’s just copy/paste error but Lua is case-sensitive, and in myTapListener() you call MainGame, but it is defined as mainGame down below.

Hope that helps.

Awesome! Thanks much! This takes a little getting used to, appreciate it.