问题
I have two problems with pausing and unpausing scene. I have button:
playPause = SKSpriteNode(imageNamed: "buttPause.png")
playPause.name = "pause"
playPause.setScale (0.65)
playPause.position = CGPoint(x: -self.frame.width / 2.55 , y: self.frame.height / 2.27)
playPause.zPosition = 10
self.addChild(playPause)
I have set function to pause and unpause scene + change texture of button:
func buttonpauseplayTouched() {
if isPlaying {
playPause.texture = SKTexture(imageNamed: "buttPlay")
isPlaying = false
self.scene?.view?.isPaused = true
}
else {
playPause.texture = SKTexture(imageNamed: "buttPause")
isPlaying = true
self.scene?.view?.isPaused = false
}
}
I have set touch on button:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
node = self.atPoint(location)
if node.name == "pause" {
buttonpauseplayTouched()
}
else {print ("Blank space")}
}
}
Now when I touch pause button, I can pause and unpause scene, but texture not change? What is wrong? Or if I want to add other spritekitnode to scene when is paused, I can not.
Second problem is, that I have some other SKSpriteNodes on scene and I have set action when I touch them. If I touch them while scene is paused, nothing happen, but when I unpause scene, action on object run. How to prevent, when is scene paused, that I cannot perform action on object. I try :
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
node = self.atPoint(location)
if node.name == "pause" {
buttonpauseplayTouched()
}
else if scene?.view?.isPaused == true && node.name == "object" {print ("Nothing!!")}
}
else {print ("Blank space")}
}
}
Without success. Thanks for any tip.
UPDATE:
Thanks both of you again! Solution with layers is better, I try it and it works fine! One little problem is with moving background:
override func didMove(to view: SKView) {
createBackground()
}
func createBackground(){
for i in 0...3 {
background = SKSpriteNode(imageNamed: "background1.png")
background.name = "background"
background.setScale(0.694)
background.position = CGPoint(x: 0, y: CGFloat(i) * background.size.height)
background.zPosition = 1
gameLayer.addChild(background)
}
}
func moveBackground(){
gameLayer.enumerateChildNodes(withName: "background", using: ({
(node, error) in
node.position.y -= 7
if node.position.y < -((self.background.size.height)) {
node.position.y += (self.background.size.height) * 3
}
}))
}
override func update(_ currentTime: TimeInterval) {
moveBackground()
}
When I pause gameLayer, background still moving. How to edit code to stop move bacground when is gameLayer paused? I hope that only little change of code solve this. Thanks!
回答1:
I put my controls on their own layer (SKNode) controlsLayer.addChild(pauseButton) and game objects on their own layer "gameLayer" then when I want to pause the game, I just pause the gameLayer (gameLayer.isPaused = true). This stops the game objects from moving, gives the appearance of being paused but still allows me to things like actions, add objects etc.
override func update(_ currentTime: TimeInterval) {
if !gameLayer.isPaused {
moveBackground()
}
}
回答2:
I am personally a fan of what Ron said, but just want to show you how you can do this in a few lines:
func buttonpauseplayTouched() {
playPause.texture = SKTexture(imageNamed: isPaused ? "buttonPlay" : "buttonPause")
//pausing the scene rather than a view
self.isPaused = !self.isPaused
}
So, basically, based on scene's isPaused
property, you choose a texture. And after that, you toggle scene's paused property. You don't need isPlaying
variable in this case.
来源:https://stackoverflow.com/questions/42935004/how-to-prevent-run-skaction-on-paused-scene-after-unpaused-texture-of-node-no