keep decrementing value on long press in Corona SDK - coronasdk

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

Related

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

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

When does the touch event in Corona have a "cancelled" phase?

The touch event in Corona has 4 phases: "began", "moved" , "ended" and "cancelled". When does the event receive the "cancelled" phase? (I didn't find a function that you can cancel the event with it, you can just remove the listeners).
And how can I use the "cancelled" event phase in application?
Corona SDK is an abstraction layer upon the top of iOS and Android; most design decisions will reflect upon the underlying platform.
It would seem the touch event implements the UITouch object (and whatever the equivalent is on Android). Searching Google for "iphone uitouch cancelled" resulted in this question, which should answer yours.
If you need to manually "cancel" an event, simply store a flag in an associated object (or in the touch event, if it's a simple table) and check it when "moved" or "ended" is called.
(Disclaimer: I have never used Corona, nor developed for mobile platforms.)
Basically if you are holding an object, button, etc. and you slide your finger off instead of releasing it that will be registered as 'canceled' which you can do what you wish with, usually the same as 'ended'
Example:
if event.phase == "began" then --Pressing the button
move = true
elseif event.phase == "canceled" then --sliding your finger off
move = false
elseif event.phase == "ended" then --Releasing the button
move = false
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.

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