How to successfully pause game in Corona SDK? - coronasdk

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.

Related

Is there another way to pause the music in my LOVE2D game?

I am working with a LOVE2D game and I am implementing a pause feature. I actually have this function in my Lua file. It includes a scrolling movement and I want to press P to stop the game for a bit.
My code is shown here:
if love.keyboard.wasPressed('p') and scrolling == true then
scrolling = false
sounds['music']:pause()
sounds['pause']:play()
end
My problem is on line 3, because I added a field called pause. It doesn't make sense since I looked at the wiki that it is there in vversions 11.0 above.
Is there any other function I can call?

turning Fire Animation to actionscript 3

How can I turn Fire animation from Timeline to ActionScript 3 Code?
If I use fire animation on timeline for my game, characters position and level details would reset.
Do you have any advice about how can I handle this issue?
My experience on flash is too low, sorry for this kind of question
Thanks
Make a Fire movieclip that contains all the fire timepline animation. Then simply call it's play() function to play it.

How to effectively pause a game in spritekit?

This seems like such a basic concept but there is really no good answer.
I currently have a pretty basic setup, in my game's scene, I have an enum gameState which is currently either inGame or gamePaused.
I have my button set up, and I have it so pressing simply toggles gameState.
Then, to make it work, I have two separate update methods: One for when gamePaused, one for when inGame.
This works for 95% of my game, as the updates in my game can be pretty readily started and stopped without any issues. There are some issues I cannot tackle though.
First and foremost are actions. I use SKActions for a bit of my project (some movement, scaling, fading...) and this method does NOT pause them. This can be catastrophic in certain points. Secondly, this doesn't handle particles, physics, and a few other things that are not directly associated to my update methods.
The only clue I have is self.view.paused = YES, but that isn't going to work either.
First off, this DOES fix my actions, particles, and physics problem... kinda. But This question suggests that SKActions aren't actually PAUSED by this, but actually STOPPED completely. Is this true? If it is, then it won't work smoothly either because my actions are thrown out of whack.
Also, pausing the view seems to do what it says: stop everything. EVERYTHING. Newbie question here, but how am I supposed to get any code to run at that point? It's all cut off. Is this where I need subViews? I have never used a subView yet, but it sounds like its what I want in this case.
Simply put, there are a lot of unanswered questions for me, and I don't know how to proceed. I'm hoping there's some 'standard procedure' for pausing in Sprite Kit, but with pausing the view halting actions I truly have no idea where to proceed.
Should I move away from using actions? Is my idea of a pause subView sound?
I don't want to babble, all I want to know is how you go about pausing in your average Sprite Kit project. Additional info provided upon request.
Pausing a node will pause (not stop) the node's actions, and i suppose pause will also be applied to all children of the paused node in the same way. All other unpaused nodes will continue to run their actions to completion.
You should have a "game layer" node with all game nodes in them that you want to pause, and then just pause that "game layer" node. Then have a "pause game menu" node where you add all the pause menu stuff you need which will continue to function normally even if the game layer is paused.
Be very sure you do not use performSelector or NSTimer or GCD or any other scheduling methods besides actions or the scene's update: method because those will not adhere to a node's paused state.
Pausing the view effectively freezes everything in it, pausing the scene according to some will not pause the update: calls - but I have not verified this myself. If this were the case you can always check inside the update method whether a given node that you send a message to is paused and if so, decide not to send certain messages to that node.
I just called a map function on all children of the scene and set the paused property to true
self.children.map{($0 as SKNode).paused = false}
By setting it to true you can easily undo the freeze.
EDIT:
Better use a for-loop to iterate over the children.
for node in self.children as [SKNode] {
node.paused = false
}
I can tell you what I did in my game:
Pause the SKView on -applicationWillResignActive: (or equivalent, using NSNotifications),
Un-pause the SKView on -applicationDidBecomeActive: (or equivalent, using NSNotifications),
For the actual game scene only, I set a boolean flag _isPaused when resigning active, and if it is true I just skip the -update: method (frame updates). (I also show the "paused" menu when the user resumes the app).
...But my game is a simple puzzle game, and there's no long actions that should continue past the pause/resume. I can not confirm right now that all actions are aborted, but I think it's not the case.
I also used self.children.map{($0 as SKNode).paused = false} and it does work if you create a var layer of type SKSpriteNode and add it on top of the scene. But I also have an NSTimer I'm using to spawn sprites on the scene. Everything currently on the scene is paused, but sprites keep appearing and moving across the screen. It is not pausing the spawning.
I tried pausing the NSTimer when I call the above code by setting repeats to false, then setting it to true when I remove the layer but it doesn't work. I also tried self.scene?.view?.paused = true but that freezes everything and the layer I create does not even appear onscreen.
You need to set the delegate for the View!
func PauseGame(){
self.scene.view.paused = true
}
fun playGame(){
self.scene.pause = false
}
This function is work perfectly if you are not using the NSTimer to call a function. If you use NSTimer your view will pause perfectly but when you play the game again by play game functionality you see that all the functions that use the NSTimer function that runs in the back end and your screen full with your sprite-kits. So when you use NSTimer in the function you have to first pause the NSTimer functions also after that this function work perfectly.
//For Pause
func pauseGame() {
scene?.view?.paused = true
}
//For play.
func playGame() {
scene?.view?.paused = false
}

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

Trouble toggling the userInteractionEnabled property in iOS

I am developing a tic tac toe game for iOS and I am using a combination of UIButtons and UIImageViews to allow for user interaction and to display the moves made. My problem is that the buttons continue to accept user input before the cpu makes it's move, which breaks my game logic. I have made several attempts to toggle the userInteractionEnabled property, but I have only been able to turn it off. The engine that gets everything started in the game is my buttonPressed method. I also toggle the userInteractionEnabled property within this method and therein lies my problem: How do I re-enable the property after disabling user interaction? Is there a method that is called in between events that I can overwrite?
I have searched the web and I have searched through the developer documentation provided by Apple and I found information on the touchesBegan and touchesEnded methods. However, from what I understand, those methods need to be explicitly called which brings me back to my original problem of not being able to call those functions without the user's interaction.
If anyone can help, I would greatly appreciate it! I have been racking my brain over this for the past couple of weeks and I am just not seeing a solution.
I'd think that for a game like tic-tac-toe, calculating the countermove should be so fast that it can be done immediately in response to the first button press. If you're doing something complicated to calculate the next move, like kicking off a thread, you might want to reconsider that.
Let's say, though, that your game is something like chess or go, where coming up with a countermove might take a bit longer. Your view controller should have a method to make a move for the current player, let's call it -makeMove:. Your -buttonPressed action should call that method to make a move for the user. In response, -makeMove: should update the state of the game, switch the current player to the next player. If the new current player is the computer, it should then disable the controls and start the process of calculating the next move. Let's imagine that's done by invoking some NSOperation, so that coming up with the next move is an asynchronous task. Once the operation has come up with a move, it should again invoke -makeMove: (by calling -performSelectorOnMainThread:), which will again update the game state and the current player. This time, though, it should see that the new current player is not the computer, and so it should re-enable the controls.

Resources