Multitouch (Multiple Touch Events at Once) in Corona - lua

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

Related

Pause/Unpause Jump Effect

I am making a game were the player sprite must jump over incoming obstacles.I have implemented a pause button and have set the un-pause function to occur when the user taps anywhere on the screen. My problem is that when the user tries to un-pause the game, he both un-pauses and makes the player jump. I have included all player movement in a tap gesture while all the button functions (pause, restart etc.) in the touchesEnded function. I also tried putting both in the touchesEnded but I saw no better result. Thank you all in advance.

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

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.

AS3 Run code continuously while holding a Button down - Air For iOS/Android

I am developing an iOS game in Flash CS6.
I have a basic movement test that I put in an Event.MOUSE_DOWN handler.
What I'm expecting/wanting is when I held my finger down on the button, that the player would keep moving until I stop touching the screen.
What happens though, is I have to keep constantly tapping to keep the player moving - rather than just hold my finger on the button and the player keeps moving.
What code should I use to accomplish what I want?
To accomplish this, you'll need to run a function continuously in between MouseEvent.MOUSE_DOWN and Event.MOUSE_UP as MouseEvent.MOUSE_DOWN will only get dispatched one time per press.
Here is a simple script to do just that:
myButton.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,tick); //while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp(e:Event):void {
removeEventListener(Event.ENTER_FRAME,tick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
function tick(e:Event):void {
//do your movement
}
As an aside, you may want to use the TOUCH events, as it gives more flexibility with multi-touch control. Though if you're only ever going to allow one item to be pressed at any given time, it's not an issue.
TO do that, just add Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT in your document class, then replace your MouseEvent listeners with the appropriate touch event:
MouseEvent.MOUSE_DOWN becomes: TouchEvent.TOUCH_BEGIN
MouseEvent.MOUSE_UP becomes: TouchEvent.TOUCH_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