Move box on tap

I just starting working with the Corona platform with a friend. My background with a bit of programming experience but a good deal of knowledge of concepts and other languages. 

Anyways, just for a simple text to create movement on tap I decided to use the game template that comes with Corona. I added a simple event listener for tap and tested it with no issues. Now I’ve tried to make the crate move vertically with a tap but with no success. I’ve looked over the API and did a bit of reading on other projects created by Corona. I’ve tried to applyForce(), applyTorque(), and applyImpulseForce() but none of them had the effect I wanted. I thought it would be very simple, gravity and all of physics effect should be in action but the crate should just move up a bit on tap.

Thank you for your help.  :slight_smile:

Hi @partounian,

Welcome to Corona! This sounds like an issue that will be easily solved, but I would like to see your basic code first. Can you show how you’re creating the object, its physics body, and how you’re handling the tap and move upward? For clarity in the forums, please surround code with “lua” tags like this:

[lua] --code [/lua]

Thanks,

Brent Sorrentino

Hello Brent,

I have used the source example source given when creating a new project and selecting game.

Here is the code for creating the crate:

[lua]

– make a crate (off-screen), position it, and rotate slightly

crate = display.newImageRect( “crate.png”, 90, 90 )

crate.x, crate.y = 160, -100

crate.rotation = 0

– add physics to the crate

physics.addBody( crate, { density=10, friction=1, bounce=.2 } )

[/lua]

The event listener for the screenTap() function.:

[lua]

display.currentStage:addEventListener( “tap”, screenTap )

[/lua]

and for the screenTap function I’ve tried setting different values into these functions:

[lua]

applyForce()

applyLinearImpulse()

applyTorque()

[/lua]

Do you just want the crate to jump or like shoot up and never come back? Or like when your holding the screen it goes up but when you let go it comes down again.??

–SonicX278

Just a little jump on tap.

Thanks,

Patrick

Here is some sample code. Just test it out.

local physics = require("physics") physics.start() centerX = display.contentCenterX centerY = display.contentCenterY actualH = display.actualContentHeight actualW = display.actualContentWidth floor = display.newRect( centerX, centerY + actualH/2 - 10, actualW, 20 ) physics.addBody( floor, "static", { bounce = 0 } ) crate = display.newRect( centerX, centerY, 20, 20 ) crate.rotation = 0 physics.addBody( crate, "dynamic", { bounce = 0 } ) local function jump(event) if event.phase == "began" then crate:applyLinearImpulse( 0, -0.03, crate.x, crate.y )-- Increase the "0.03" for higher jump. end end crate:addEventListener( "touch", jump )

–SonicX278

I tried testing that itself with my current code (which has a menu scene before) and I received and error. I also tried modifing my code to work more similarly to yours but got a few errors when I added crate:addEventListener( “touch”, jump ), when replacing crate for scene the issue was gone, but now the crate is still not effected. 

Here is the entire level1 scene code.

[lua]


– level1.lua


local composer = require( “composer” )

local scene = composer.newScene()

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local xpos, ypos

local crate

local function jump( event )

if event.phase == “began” then 

crate:applyLinearImpulse( 0, -0.10, crate.x, crate.y )-- Increase the “0.03” for higher jump.

end

end

function scene:create( event )

– Called when the scene’s view does not exist.

– 

– INSERT code here to initialize the scene

– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

local sceneGroup = self.view

– create a grey rectangle as the backdrop

local background = display.newRect( 0, 0, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

background:setFillColor( .5 )

– make a crate (off-screen), position it, and rotate slightly

crate = display.newImageRect( “crate.png”, 90, 90 )

crate.x, crate.y = 160, -100

crate.rotation = 0

– add physics to the crate

physics.addBody( crate, “dynamic”, { bounce = 0 } )

– create a grass object and add physics (with custom shape)

local grass = display.newImageRect( “grass.png”, screenW, 82 )

grass.anchorX = 0

grass.anchorY = 1

grass.x, grass.y = 0, display.contentHeight

– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)

local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }

physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )

– all display objects must be inserted into group

sceneGroup:insert( background )

sceneGroup:insert( grass )

sceneGroup:insert( crate )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

physics.start()

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

physics.stop()

elseif phase == “did” then

– Called when the scene is now off screen

end

end

function scene:destroy( event )

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

local sceneGroup = self.view

package.loaded[physics] = nil

physics = nil

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

scene:addEventListener( “touch”, jump )


return scene

[/lua]

@SonicX278

I opened up a new project with just your code and that seems to work. I’m going to continue debugging and see why integrating the 2 does not work.

Hi @partounian,

It sounds like @SonicX278 has helped you get the basic implementation in place. The issue you may now be facing could be related to scope… in other words, Lua does not know what the reference to “crate” is when you apply force/implulse because it’s not in the proper scope.

If scope is not a familiar topic for you, I suggest you study it considerably before proceeding too much further. Lua scope affects so many aspects of coding across so many levels, it’s best that you gain a solid understanding of it. This tutorial should help:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Also, as another general piece of advice, I suggest that you do not “stop” the physics engine unless you absolutely will not need to use physics again for most of the app’s functionality. Generally, it’s best to simply pause the engine when you’re not using physics. And, you shouldn’t need to ever nil out its “package.loaded[]” either… that is a really uncommon need and does not apply to 99% of developers. :slight_smile:

Best regards,

Brent

Thank you Brent, I did not nil it out myself, that is included in Corona as sample game code. Anyways, thank you for the tips and I will review scope. 

Hi @partounian,

Welcome to Corona! This sounds like an issue that will be easily solved, but I would like to see your basic code first. Can you show how you’re creating the object, its physics body, and how you’re handling the tap and move upward? For clarity in the forums, please surround code with “lua” tags like this:

[lua] --code [/lua]

Thanks,

Brent Sorrentino

Hello Brent,

I have used the source example source given when creating a new project and selecting game.

Here is the code for creating the crate:

[lua]

– make a crate (off-screen), position it, and rotate slightly

crate = display.newImageRect( “crate.png”, 90, 90 )

crate.x, crate.y = 160, -100

crate.rotation = 0

– add physics to the crate

physics.addBody( crate, { density=10, friction=1, bounce=.2 } )

[/lua]

The event listener for the screenTap() function.:

[lua]

display.currentStage:addEventListener( “tap”, screenTap )

[/lua]

and for the screenTap function I’ve tried setting different values into these functions:

[lua]

applyForce()

applyLinearImpulse()

applyTorque()

[/lua]

Do you just want the crate to jump or like shoot up and never come back? Or like when your holding the screen it goes up but when you let go it comes down again.??

–SonicX278

Just a little jump on tap.

Thanks,

Patrick

Here is some sample code. Just test it out.

local physics = require("physics") physics.start() centerX = display.contentCenterX centerY = display.contentCenterY actualH = display.actualContentHeight actualW = display.actualContentWidth floor = display.newRect( centerX, centerY + actualH/2 - 10, actualW, 20 ) physics.addBody( floor, "static", { bounce = 0 } ) crate = display.newRect( centerX, centerY, 20, 20 ) crate.rotation = 0 physics.addBody( crate, "dynamic", { bounce = 0 } ) local function jump(event) if event.phase == "began" then crate:applyLinearImpulse( 0, -0.03, crate.x, crate.y )-- Increase the "0.03" for higher jump. end end crate:addEventListener( "touch", jump )

–SonicX278

I tried testing that itself with my current code (which has a menu scene before) and I received and error. I also tried modifing my code to work more similarly to yours but got a few errors when I added crate:addEventListener( “touch”, jump ), when replacing crate for scene the issue was gone, but now the crate is still not effected. 

Here is the entire level1 scene code.

[lua]


– level1.lua


local composer = require( “composer” )

local scene = composer.newScene()

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local xpos, ypos

local crate

local function jump( event )

if event.phase == “began” then 

crate:applyLinearImpulse( 0, -0.10, crate.x, crate.y )-- Increase the “0.03” for higher jump.

end

end

function scene:create( event )

– Called when the scene’s view does not exist.

– 

– INSERT code here to initialize the scene

– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

local sceneGroup = self.view

– create a grey rectangle as the backdrop

local background = display.newRect( 0, 0, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

background:setFillColor( .5 )

– make a crate (off-screen), position it, and rotate slightly

crate = display.newImageRect( “crate.png”, 90, 90 )

crate.x, crate.y = 160, -100

crate.rotation = 0

– add physics to the crate

physics.addBody( crate, “dynamic”, { bounce = 0 } )

– create a grass object and add physics (with custom shape)

local grass = display.newImageRect( “grass.png”, screenW, 82 )

grass.anchorX = 0

grass.anchorY = 1

grass.x, grass.y = 0, display.contentHeight

– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)

local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }

physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )

– all display objects must be inserted into group

sceneGroup:insert( background )

sceneGroup:insert( grass )

sceneGroup:insert( crate )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

physics.start()

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

physics.stop()

elseif phase == “did” then

– Called when the scene is now off screen

end

end

function scene:destroy( event )

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

local sceneGroup = self.view

package.loaded[physics] = nil

physics = nil

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

scene:addEventListener( “touch”, jump )


return scene

[/lua]

@SonicX278

I opened up a new project with just your code and that seems to work. I’m going to continue debugging and see why integrating the 2 does not work.

Hi @partounian,

It sounds like @SonicX278 has helped you get the basic implementation in place. The issue you may now be facing could be related to scope… in other words, Lua does not know what the reference to “crate” is when you apply force/implulse because it’s not in the proper scope.

If scope is not a familiar topic for you, I suggest you study it considerably before proceeding too much further. Lua scope affects so many aspects of coding across so many levels, it’s best that you gain a solid understanding of it. This tutorial should help:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Also, as another general piece of advice, I suggest that you do not “stop” the physics engine unless you absolutely will not need to use physics again for most of the app’s functionality. Generally, it’s best to simply pause the engine when you’re not using physics. And, you shouldn’t need to ever nil out its “package.loaded[]” either… that is a really uncommon need and does not apply to 99% of developers. :slight_smile:

Best regards,

Brent

Thank you Brent, I did not nil it out myself, that is included in Corona as sample game code. Anyways, thank you for the tips and I will review scope.