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.
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:
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.??
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
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:
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.
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:
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.??
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
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:
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.