Using storyboard seems to disable touch events? - coronasdk

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().

Related

keep decrementing value on long press in Corona SDK

Hi i am working on a game and i have one object. On this object i have touch event. i have added a check on the touch even if the touch is less then 250ms consider it a tap otherwise consider it touch. On tap event i am incrementing a value by 10 point. i want to keep decrementing values by value of 1 as long as the object i being pressed, i have added the code but it is only doing it once.
The question is How do i keep decrementing a value as long as a touch event is recorded. the code is provided for reference below
local function countTaps(event)
if event.phase == "began" then
beganTime = event.time
elseif event.phase == "ended" then
endedTime = event.time
if (endedTime - beganTime) < 250 then
climbUp()
return true
else
climbDown()
end
end
However this code determine the tap/touch once it is finished. What can be the better runtime alternative
When the touch event begins, set a timer to fire at the frequency you would like the value to change. When the timer fires, call a function that changes your counter (maybe your climbDown()).
When the touch ends, cancel the timer. You will want to make sure you detect the end of this touch even if it is no longer on your button. You accomplish this by managing the focus on the stage as explained in this guide.
For more information, consult the documentation for timer.*, specifically timer.performWithDelay() and timer.cancel().

Triggering touch event when object gets to a location where the player is holding the screen

Basically, I am making a mini game where you have to catch snowflakes which are falling from the sky. Now, I want to make it so that when the user is holding the screen and once a snowflake gets to a location where user's finger is, it will trigger the touch event.
EDIT:
This is the code I got.
Snowflake spawns every few seconds. When it does, I simply add event listener to it.
function SnowflakeTouch(event)
print("touched")
end
Snowflake:addEventListener("touch", SnowflakeTouch)
But yea, this doesn't work and I am interested if someone has another way.
The "Touch" event in Corona has more than one phase to it and you need to check for which state it is first as you can find here. Anyway try this code:
function SnowflakeTouch(event)
if (event.phase == "began") then
print("touched")
elseif (event.phase == "ended") then
end
end

Corona body removing itself from the physic world

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.

Multitouch (Multiple Touch Events at Once) in Corona

I am creating an app in corona that has a button to shoot, reload, and a joystick to move the character around. I want the user to be able to shoot while he/she and be moving the character with the joystick simultaneously. I tried using Corona's built-in multitouch:
system.activate("multitouch")
but it does not seem to have any affect.
Does anyone have any ideas to how I can make the multitouch work, or any other ideas of how I should approach this problem?
Imagine, you are registering event listener on "touch" event:
Runtime:addEventListener("touch", touchManager)
In this event, you have event id, which is differs for different touches during multitap. Here's a part of my code which allows user to move and fire at the same time.
local function touchManager(event)
if (event.id == ignore_event_id) then return; end; -- should protect when user fired, and the same event messes with joystick
-- ....
end

how to trigger event on stopping a moving ball in corona sdk

I am trying to make a game in Corona which involves hitting the ball in a certain direction using the force vector. I am trying to trigger an event when the ball stops. I cannot use the "touch" event as the touch event is called several times when I touch the ball and set the direction for releasing it.
You can use Corona's custom events to dispatch your event when something occurs, in your case when the ball stops. The code below will dispatch an event to the Runtime object.
local event = { name = "ballHasStoppedMoving", target = Runtime }
Runtime:dispatchEvent( event )
The following code would be used to listen for the "ballHasStoppedMoving" event and call your function "ballStoppedMoving" when the event fires.
local function ballStoppedMoving(event)
print("The ball has stopped moving")
end
Runtime:addEventListener("ballHasStoppedMoving", ballStoppedMoving)
Use the phase property of the touch event, and only react on the "began" phase.
In an enterFrame event, check for the velocity of the ball using ball.getLinearVelocity. If they do not equal (0,0), execute the method/event. If you want to execute a custom event at this time, follow Michael's answer.

Resources