Post

Replies

Boosts

Views

Activity

How to use UIPanGestureRecognizer to continuously track swipes?
Hello,I'm working on a game that is controlled by swipes. I have the following function to track pan swipes:@objc func handlePanFrom(_ recognizer: UIPanGestureRecognizer) { if recognizer.state != .cancelled { let translation = recognizer.translation(in: recognizer.view) // TODO: use the x and y coordinates of endPoint and beginPoint to determine which direction the swipe occurred. let xDif = translation.x let yDif = translation.y if abs(xDif) > abs(yDif) { if xDif > 0 { changeDirection(direction: .east) print("change east") } else { changeDirection(direction: .west) print("change west") } } else if abs(yDif) > abs(xDif) { if yDif > 0 { changeDirection(direction: .south) print("change south") } else { changeDirection(direction: .north) print("change north") } }This will track the user's swipes and change the direction effectively, but only if the user lets go of the screen after every swipe. I was wondering, is there a way to "reset" the translation positions every time the direciton is changed so the user does not have to release from the screen after swiping? I tried calling recognizer.setTranslation(CGPointZero, inView: self.view) every time the direction is changed, but that didn't work.
2
0
548
Apr ’20
Adjusting my background image for different phone sizes?
Hello,I have my GameScene.sks frame set to 750x1334 pixels (iphone 8). Inside of it I have one image which I want to stay the same across all devices, and a "border" image that surrounds the screen that I want to expand for different phone sizes. I have created new images for all screen resolutions bigger than the iphone 8, but couldn't figure out how to get them to display for different phones. How would I go about doing that?
1
0
1.2k
Apr ’20
How to pause the game when the user receives a notification?
Hi, I am working on an app and I need it to call my pauseAfterNotif() method whenever the user receives a push notification (text message, instagram dm, etc.)Here's what I have right now:AppDelegate:func applicationWillResignActive(_ application: UIApplication) { NotificationCenter.default.post(name: NSNotification.Name(rawValue: "goToBackground"), object: self) print("notif read") }GameScene.didMove():let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(pauseAfterNotif), name: UIApplication.willResignActiveNotification, object: nil)GameScene.pauseAfterNotif():@objc func pauseAfterNotif() { if !isGamePaused { pauseGame() print("notif detected") } }Right now, the code only executes when I exit the game and enter the homescreen or if I receive a call while playing. Is it possible to have it execute when I receive a push notification too?
0
0
430
Apr ’20
Can't hide or unhide GameScene elements from touchNode input
Hello,I am making my first game and am running into an issue. I have a pause button in my game and when it's pressed I want my "pauseMenu" to unhide, but it's not working. Here's code from my GameScene:if touchNode == pauseButton { pauseGame()}func pauseGame() { self.view?.isPaused = true print("Before isHidden is set to false: \(pauseMenu?.isHidden)") pauseMenu?.isHidden = false print("After isHidden is set to false: \(pauseMenu?.isHidden)")}Here's what prints:Before isHidden is set to false: Optional(true)After isHidden is set to false: Optional(false)As you can see, the pauseGame() function gets called successfully, and the view pauses, but the pauseMenu stays hidden. It's weird because it successfully unhides if I call pauseMenu?.isHidden = false elsewhere, like in my function that is called when the game ends:func gameOver() { ... toggleViews()}func toggleViews() { ... print("Before isHidden is set to false: \(pauseMenu?.isHidden)") pauseMenu?.isHidden = false print("After isHidden is set to false: \(pauseMenu?.isHidden)")}This successfuly unhides the pauseMenu and prints the same code:Before isHidden is set to false: Optional(true)After isHidden is set to false: Optional(false)I think the problem has something to do with not being able to hide or unhide something from a touchNode, if I call toggleViews() like this, nothing happens to the gameScene.if touchNode == pauseButton { toggleViews()}I'm at a loss here, I've been trying to fix this for a few hours. Any help is greatly appreciated
10
0
986
Apr ’20