I'm having difficulty pausing the game when I leave and switch back to it.
I'm trying to pause the SKSpriteNode called main, which contains all my sprites, when the view returns from the background. In-game, I can touch the pause button and the game pauses, and the resume button and it resumes.
This is my code:
func didBecomeActive() {
println("didBecomeActive")
main.paused = true
}
The first time this runs is when the app opens for the first time, and everything is paused as it should be. The second time, is when it returns from the background, and suddenly all the animations (SKActions, particles, etc.) start working.
I've confirmed that the method is running, and I've also tried setting main.paused to false and then true, and even self.paused to true. Nothing works.
I'm completely stumped. Anyone know what the issue is here?
Setting self.scene.paused = YES should fix this. I have tried it with a game I am developing and it works fine.
Just set self.scene.paused = YES when the game enters the background, then when it return to the foreground, it should stay paused till you resume it, i.e. set self.scene.paused = NO.
Related
I have an WKInterfaceInlineMovie within an WKInterfaceController. The URL of the video is set at some point after the video file is downloaded. Playing works fine besides this problem which is a different story, I think.
Here's the problem. If I keep the screen open, lower my wrist and then raise it again, I can see the same screen and the video starts playing automatically.
It looks very weird and unexpected especially because I have some custom UI (video progress indicator, animated Play/Pause buttons) which is triggered when I manually start the video but it obviously doesn't react on this unwanted automatic video start. If I close the extension with the Crown button, next time I open the app, it again shows the screen with video and start playing automatically. I can even not using the extension for a while and receive a user notification later – while the custom notification UI is displayed, I can hear the video starts playing somewhere below for a short period of time.
When it happens I always receive two messages in console:
<<<< PlayerRemoteXPC >>>> remoteXPCPlaybackItem_NotificationFilter: [0x128b86e0] I/NQB.01 Received kFigPlaybackItemNotification_FirstVideoFrameEnqueued
<<<< PlayerRemoteXPC >>>> remoteXPCItem_handleFirstFrameNotificationLatch: [0x128b86e0] I/NQB.01 Posting kFigPlaybackItemNotification_FirstVideoFrameEnqueued
I have the Autoplay checkmark unchecked in storyboard. I also tried to set setAutoplays(_:) to false programmatically when the outlet is initialized, or later when a URL is set to the movie. All of this makes no difference.
The behavior is the same no matter was the video playing or not when the screen get deactivated. I tried to call pause() on willDisappear() and didDeactivate() – it also doesn't do any difference.
I even tried to call pause() on didAppear() and willActivate() – didn't help either. Curiously enough, these methods are not called when I lower the wrist and raise it again (however, willDisappear() and didDeactivate() are both called.) But perhaps, it's a different story, too.
I am trying to get a background sound file to play in the GameScene.swift file. But whenever I do and call the run.SKAction, the screen turns white and I hear a lot of static until it crashes. When I comment out the run.SKAction the game starts normally. Ive tried changing the formats of the file to WAV, MP3, AAC, CAF. But the same thing happens. I checked to see if I mispelled anything but I did not.
It gives me a message saying: "Message from debugger: Terminated due to memory issue"
IMPORTANT NOTE: the "waitForCompletion is set to TRUE it seems to work fine with a little static at the beginning of the soundtrack then it plays normally. but when "waitForCompletion" is set to FALSE I get the white screen. I Also Sometimes get a message saying:
SKAction: Error loading sound resource: "Entry.m4a"
I tried several different soundtrack files but it always happens.
Here is the beginning of the GameScene():
class GameScene: SKScene {
var SpaceShip2 = SKSpriteNode(imageNamed:"IntroSpaceShip")
var GameSceneSound = SKAction.playSoundFileNamed("BackgroundSound.wav", waitForCompletion: false)
}
Here is the function I used to call the sound:
func playsound(soundVariable: SKAction){
run(SKAction.repeatForever(soundVariable))
}
And here is when I call it:
override func didMove(to view: SKView) {
playsound(soundVariable: GameSceneSound)
}
Can someone please help me!
Thank you in advance!
Since waitForCompletion is set to false, the action is considered to be have been completed immediately when run. Since this is a repeatForever action, the next repetition of the action runs before the the first action of playing the sound has completed. The 2nd repetition runs, immediately causing the 3rd repetition, which causes the 4th one, etc. Eventually you fill up too much memory with all the SKActions and the app is terminated by iOS.
I would suggest having waitForCompletion set to true. That way the 2nd repetition of the action won't start until the 1st action has completed playing the sound.
Hope this helps!
I have a ~30 minute mp3 file being played by an AVAudioPlayer. Sometimes, audioPlayerDidFinishPlaying:successfully is called (with the successfully flag set to TRUE), even when it is only 5 minutes into the clip. It happens infrequently, but it can happen at any position in the clip.
I've only noticed that it happens when the user is pressing a button on the user interface, or moving a slider. And the more quickly they press buttons on the user interface, the more likely it is to happen it seems.
Any ideas what could be causing this, or how to fix it?
MORE INFO:
Only 1 sound is played at a time. No sounds are played for button actions. The AVAudioPlayer is declared locally in my main ViewController. I've tested on an iPhone 3GS and an iPhone 4s. The problem happens very very rarely on the iPhone 4s. It's much more frequent on the 3GS.
OK, I had the same problem starting yesterday. Don't whether we were having the same problem but I will throw it here
In the slider ValueChanged event handler, I tried to update the view elements based on the new position of the slider. But within the updateViewMethod, I called another
slider.value = self.player.currentTime;
Removing that solved my problem.
So in short, your timer's callback should update the slider, but slider valuechanged handler should not redo the work.
My iPhone application has two states: UI and Game.
Game is played using device tilting only so I switched auto-sleep off on game start:
[[UIApplication sharedApplication].idleTimerDisabled = TRUE;
But as soon as I return to UI, I want auto-sleep to be active again. So on game finish I restore it:
[[UIApplication sharedApplication].idleTimerDisabled = FALSE;
After a long game playing, it resulted in immediate darkening of the first UI screen that I go after the game.
So it seems that when idle timer was disabled it was still calculating time. And it had fired immediately after "enabling". How can I fix this problem?
I don't know if the idleTimer can be reset programmatically, but an option is to require the user to touch the screen before it goes back.
Another option is set your own timeout once you are back and wait for it to complete before you set idleTimerDisabled = NO. Remember to clear this timeout if you start the game again.
This answer may be usefull.
I also had problems when using Music/Audio players, which seemed to reactivate the timer.
ps: in ObjectiveC you should use YES/NO instead of FALSE/TRUE
Playing around with Timers, and trying to make them behave right when app is sent to background, killed etc.
Judging by the generated comments in xcode, you should disable/invalidate timers on resignActive/enterBackground events.
However, i havent done that yet, but checked what happens to my timer when i click the home button and then re-enter the app.
I basically have a method in my view that the timer triggers every second:
NSLog(#"workedTimTimerTick workedTime; %#", timeString);
And when i exit the app, the output stops, when i re-enter the app, the output starts again...
Since i'm not doing anything code-wise to the timer on those lifecycle events, how come it stops getting called?
Input appreciated!
Your app is suspended when it enters background mode (Application States and Transitions).
Your timer won't fire when the app is in background and the time spent in background isn't taken into account for the timer delay.