Difference between pausing and resetting a game Spritekit / Swift? - swift

Right now I've got it where if I click on the pause button I've made, it goes to my pause menu. When I go back to the game screen via the button I made to do that, the game has reset itself to the beginning.
I've got a "go back to the game button" and a reset the game button.
How do I get those two to act correctly?
I don't even really know where to begin with this because I don't know if the game is clearing when I'm initially leaving the screen or when I'm going back to it. I thought I had read the default behavior was to save the state of a screen when you navigated off of it.

When you change your game view controller (gameVC) to your menu view controller (menuVC), if there isn't any reference remaining everything gets deallocated. Therefore, when you go back to your gameVC you're allocating new objects (scene, nodes, ...).
What you might be looking for, to pause your game, is the paused property.
On your SKScene (which is a SKNode) : https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instp/SKNode/paused
paused
A Boolean value that determines whether actions on the node and its descendants are processed.
If the value is YES, the node (and all of its descendants) are skipped when a scene processes actions.
On you SKView : https://developer.apple.com/library/prerelease/mac/documentation/SpriteKit/Reference/SKView/index.html#//apple_ref/occ/instp/SKView/paused
paused
A Boolean value that indicates whether the view’s scene animations are paused.
If the value is YES, the scene’s content is fixed onscreen. No actions are executed and no physics simulation is performed.
The update: method won't be called if the SKView is paused.

Related

deactivating/activating a UGUI button leaves it in pressed state

I have a button with "animation" transition type. When I click it, I call SetActive(false) on its gameObject. Then, by clicking another button, I call SetActive(true) to activate the deactivated button, but the animation does not reset: it's still in pressed state (like it was when I deactivated it, because I was pressing it), and it does not back to idle state.
EDIT: I realized I have not set the sprite for "normal" state in the animation. But I don't understand why before deactivate/activate, the button is showing default sprite for idle state, and after deactivate/activate, is showing pressed sprite for idle state.

ccTouchesMoved and selector:#selector(ButtonTapped:)

Ill try and keep this as simple as possible.
I have a guy which you drag around the screen
I have a shoot button which fires bullets
To drag the guy around the screen im using ccTouchesMoved, basically get the users finger movement and set the guy to that place. Done.
I have a button which is a CCMenuItemImage and if it is tapped then is called a selector to run a method. The method simply fires some bullets. Done.
Now my problem is i cant have these two things work at the same time. I would like the player to be able to drag around the guy and shoot at the same time. Im assuming the button is not being tapped becuase the user is touching and dragging around the player.
How can i fix this? Still be able to drag around with one finger and press the shoot button with the other and they both work together?
You might want to implement your fire button using a sprite. You can handle ccTouchesBegan and ccTouchesEnded to check for a press within the bounds of your button, to swap it with a pressed state (on begin) or to implement the fire action (on end).
In your ccTouchesMoved handler, you can iterate over the set of touches and determine if the touch that moved is the one over the button, or the touch that should change the character position.

Cocoa Touch - Handling touches that get interrupted or lost

I have a couple places in my app where UI elements reset themselves when a touch ends on them. For example, hiding a dashed outline, sliding a view back to the default position.
The problem is that on occasion the app loses track of the touches. One example is if I slide the view upward and cause a UIAlertView to show, the view doesn't slide back, because the reset code is in touches ended. The touch ends during the time the UIAlertView is active, and the view doesn't reset. The same example works for hiding the dashed outlines.
My question is, where/how can I handle the reseting of these custom UI elements so that when a touch ends without being noticed it will still reset. TouchesEnded isn't always doing it for me.
Have you try touchesCancelled? I think it is called when the touch get interrupted.
EDIT: If this doesn't work then maybe you can manually add code that cancel the touch when you initiate the alert.

How do I properly clean up a UIButton

I am having issues with UIButton with the iPhone SDK. I've determined that when I have a UIButton held down when the parent is removed (and the uibutton subsequently removed), I get some strange behavior. In my case, the app stops receiving any input whatsoever, as if the cleaned up held-down button has hijacked the input system somehow.
Does anyone know if there is an appropriate way to clean up the UIButton that would release what I'm guessing is the framework thinking the released button is still held down?
Update: Adding detail to my issue
Basically, the simplified situation is that I have a root view with two characters (actors, not letter characters) and a number of buttons, among other views. The user can use the buttons to affect direction of movement, and when the characters are close to each other, the scene moves forward immediately into another mode, i.e. the buttons slide off, the view cleans up, and I construct a new one.
What this necessitates is the ability to immediately move on and clean up even if the button is still held down. I figured I could pull some tricks to delay cleaning the button up, but I would be surprised if there isn't simply some way to properly clean up the button for removal regardless of what state it is currently in. Hope that clarifies.
It might help if you would explain what behavior you are expecting. If you are calling -removeFromSuperview while holding down the view you want to remove (or its parent), I would suggest you instead hide the view and set some flag you can look for when the user finishes holding that will at that point call -removeFromSuperview.
It turned out the problem wasn't a button being held down but rather a call to set the center coordinate of a mapview with animation on, i.e.
[mMapView setCenterCoordinate:mCoordinates animated:YES];
If the view was cleaned up while the animation was still moving, the app would no longer respond to user input. I'm not certain why this is the case, still, but setting the animated flag to NO ensured the problem didn't arise.

Where's the difference between UIControlEventTouchDragOutside and UIControlEventTouchDragExit?

Both appear to have the exact same effect. They come both when the finger is far enough away from the control. The "bounds" is not really the criteria for UIControlEventTouchDragExit. It gets fired only if it's far away enough...
I came here looking for the same thing and the answer from eOgas did not seem accurate. I did my own test case and here are my results for those who want a detailed answer without having to test it for themselves:
UIControlEventTouchDragExit
gets called only once as the user leaves the control they pressed. Once the user breaks the 'magical boundary'* outside a UIButton (for instance) this event gets called once. If, while still dragging, the user drags back into the control and back out again, this event gets called once again. The reverse can apply to UIControlEventTouchDragEnter.
UIControlEventTouchDragOutside
gets called after UIControlEventTouchDragExit and gets called repeatedly every time the user drags their finger while still holding down the original touch that was used to enter the control. For those familiar with the touchesMoved method of UIView, it works similarly. The reverse can apply to UIControlEventTouchDragInside however this can obviously get called without having to leave the control first.
To understand or remember better, you can compare these events to a person leaving (and coming to) their house wherein they only exit the house once but then proceed to move outside repeatedly. Also, a person only enters their house once but then proceeds to move inside repeatedly.
*the extra space around a UIControl object that takes into consideration the user's potential for imprecise touches.
UIControlEventTouchDragOutside
An event where a finger is dragged just outside the bounds of the control.
UIControlEventTouchDragExit
An event where a finger is dragged from within a control to outside its bounds.
It sounds like with UIControlEventTouchDragOutside is fired when the user touches just outside the bounds, regardless of whether or not the finger was ever within the bounds. UIControlEventTouchDragExit is only fired when the finger is dragged from within the bounds to outside the bounds.
So, UIControlEventTouchDragOutside would be used when resizing a control (an edge tap, then drag), whereas UIControlEventTouchDragExit would be used to move the control around (tap inside and drag).
.touchDragOutside is continous, while .touchDragExit is not.
.touchDragOutside gets called on every drag movement that happens outside bounds (similar to DragGesture.onChanged(_:)), while
.touchDragExit gets called once as the drag leaves the bounds.
They both (!) get called only when the drag is initiated within the view.
In addition, they are only fired when the finger is "far away" (~70 pt) from the control. They won't get fired as soon as the finger leaves the bounds (same goes .touchUpOutside). They seem to be designed for the users to deliberately cancel ongoing touches.