Corona body removing itself from the physic world - lua

I currently using the game template created by Corona SDK, to develop my game, what i trying to do is something very simple, i want the crate to stay where it was spawned, and only move after the crate is tapped. To achieve that I tried to remove the gravityScale of the crate. This is what i have done:
local function crateTap( event )
print( "gravityScale : " .. event.target.gravityScale)
event.target.gravityScale = 1
print( "gravityScale : " .. event.target.gravityScale)
end
local crate = display.newImageRect( "crate.png", 45, 45 )
crate.x, crate.y = 160, 20
crate.rotation = 15
-- add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
crate.gravityScale = 0
crate:addEventListener( "tap", crateTap )
The application loads ok, and if i click really fast on the crate, like in the first second on the screen, the crate will fall off, but if i wait a little and click, nothing will happen. And i have no idea why.
Here's the complete level1.lua file http://pastebin.com/yjxmGqw5
Thanks

Then the issue is elsewhere in your code. There is nothing wrong with the code as shown. Gravity scale can be changed at any time, and takes effect as soon as the event handler returns. The fact that it works early on shows this. That it then stops working indicates that something else happens somewhere after a second of play, like perhaps the physics engine gets paused by another event, or something in an enterFrame event handler.

Related

Why doesn't corona composer let you go back to the main.lua page

When I'm using
composer.gotoscene("main")
An error pops up with the message:
"Attempt to concatenate global 'sceneName' stack traceback".
If I use another page to redirect instead of main.lua (example "scene2") then it works.
All of the code for the game should be in its own Composer scene rather than main.lua. If the Composer scene with the play button is called HomeScene.lua, in main.lua you would put
local composer = require "composer"
composer.gotoScene( "HomeScene" )
This makes the scene with the play button load and appear first. If you want to initialize your game scene before the player can have a chance to press Play and start the game, you can add composer.loadScene("GameScene") in main.lua as well. This just creates the scene and stores it without showing it.
In HomeScene.lua, if the play button is a DisplayObject called playButton, you would do something like
local composer = require "composer"
local function handleTouch( event )
if event.phase == "began" then
composer.gotoScene( "GameScene" )
end
end
playButton:addEventListener( "touch", handleTouch )
Hope this helps.

Reloading a game with corona sdk

I am creating my first iPhone game and cannot figure out how to reload my game.
I am using coronaSDK and have tried to use their composer API. When the player dies a button with "play again" appears and when he touches it I direct him to a scene called "reload.lua" with composer.gotoScene("reload").
In this scene I have the following code:
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif (phase == "did") then
local replay = display.newImage('star1.png', 100, 300)
composer.removeScene("level1")
composer.gotoScene("level1")
end
end
However, this only adds level1 on top of the existing one and does not remove the one that has been used. Any ideas on how I could successfully remove level1 or reload my game?
You should read this article slowly and carefully.
The source explains:
For Composer to “manage” your scene and the display objects within, all of these objects must be inserted into the scene’s view display group or a child display group of it.
The group is referenced in your code by the line at the very top of the scene:show function:
local sceneGroup = self.view
You must add all display objects into this group, for example your local replay should be:
local replay = display.newImage('star1.png', 100, 300)
sceneGroup:insert( replay )
Once above design is followed you can start utilising the other features of the composer as you request.
While this is based on Composer's older brother Storyboard, the concepts still apply to Composer. It explains the various issues with trying to reload scenes, recommendations on how to manage it:
http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

How to successfully pause game in Corona SDK?

Im trying to learn Corona through someone else's sample code. So this is a game like fruit ninja, and im trying to put a pause/resume function on it. Now since the code uses physics, i thought that i should use physics.pause and physics.start and i also paused the timer for the objects. It does freezes the screen, but when you swipe at one of the objects on the screen(fruits), it still breaks into two. How do i stop that? So i guess the pause works a little, because it stops the fruits from coming up. Thanks so much for the people who will answer my question. Ive read a few forums on here and you guys seem to really know what youre doing. :)
From your line here:
Runtime:addEventListener("touch", drawSlashLine)
Is it possible to remove this event listener in your pause, and re-add it into your resume?
Actually that will just stop you from drawing. You need to loop through all objects and remove their touch event listener.
Alternatively set a global variable to true when paused, and in chopFruit function check it, and do nothing if its set to true.
You should add code similar to the following:
if gameIsActive then
gameIsActive = false
physics.pause()
Runtime:removeEventListener("enterFrame",moveEnemy)
leftarrow:removeEventListener( "touch", moveLeft )
end
Then on resume, you should add the event listeners back.

How do you stop a game after a collision takes place?

I am creating a very simple 2d game where you slide the player across the stage to avoid "rocks" that fall from the top. What I want to happen is for the game to end when my player comes into contact with one of my rocks. This is the code I have right now, I have tried everything so I don't have a clue if I am close to getting it or am way off.
stage.addEventListener(Event.ENTER_FRAME,Collision);
function Collision( e:Event ):void{
if(player.hitTestObject(rock1))
{
gotoAndStop(3);
}
}
when I use this code the player just passes right through the rocks and the frame doesn't change. PLEASE HELP!
Assuming you already have the logic to move the falling rock and to control the player. First you have to put a trace statement to make sure the collision happens, then to end the game just remove the event listener and do whatever is needed like showing a game over screen:
stage.addEventListener(Event.ENTER_FRAME,Collision);
function Collision( e:Event ):void{
if(player.hitTestObject(rock1))
{
trace("player collided with rock1!");
// stop the enter frame event from firing
stage.removeEventListener(Event.ENTER_FRAME, Collision);
// go to game over screen or what ever
gotoAndStop(3);
}
}
Also is better to call the enter frame handler function something better than Collision like onEF, loop...etc

Using storyboard seems to disable touch events?

Im a beginner to the Corona SDK.
I have 2 scenes A & B.
In scene A i have a button with a OnRelease event.
This button is created and added to the group in the scene Create event.
Clicking the button takes me to scene B ( storyboard.gotoScene("B") ).
In scene B i have a touch event on an box (crate image).
The touch listener is added in the scene Started event, and removed in the scene Exited event.
Clicking on the crate takes me back to A ( storyboard.gotoScene("A") ).
So here's the real annoying issue:
After going back to A, all events in this scene are now disabled.
i.e. I can no longer click on the button any more (no event).
Will provide code snippet if I am missing information above.
Thank you.
* Update *
After taking about a break from this, i went back today and started debugging this again. I found the issue fairly quickly. The problem had something to do with my touched event handler (which causes a transition from scene B to A).
snippet below that caused the issue:
function testTouched( event )
-- process cue-touched event...
--local t = event.target -- commenting this was the fix.
local phase = event.phase
if "began" == phase then
print(" -> back to menu")
--display.getCurrentStage():setFocus( t ) -- commenting this was the fix.
--t.isFocus = true -- commenting this was the fix.
storyboard.gotoScene( "menu", "flipFadeOutIn", 500 )
end
-- Stop further propagation of touch event
return true
From what I see here,
enterScene
Dispatched when storyboard.gotoScene() is called, immediately after
the transition has completed. So if you specified a transition effect,
this event is dispatched as soon as the effect has ended. Adding
listeners, or app/game-specific logic should be placed in the listener
function for this event.
exitScene
When storyboard.gotoScene() is called, an “exitScene” event will be
dispatched to the currently shown scene before the transition occurs.
Cleanup duties, such as removing event listeners, stopping timers,
etc. should be placed in the listener function for this event.
Add event listeners in enterScene() event rather than createScene() and remove them at exitScene()?
Edit: I think you would need to display.getCurrentStage():setFocus(nil) in exitScene().

Resources