Ios 如何防止在暂停的场景(取消暂停后)上运行SKAction,暂停/取消暂停场景后节点的纹理不变
我对暂停和取消暂停场景有两个问题。我有一个按钮:Ios 如何防止在暂停的场景(取消暂停后)上运行SKAction,暂停/取消暂停场景后节点的纹理不变,ios,swift,if-statement,sprite-kit,Ios,Swift,If Statement,Sprite Kit,我对暂停和取消暂停场景有两个问题。我有一个按钮: 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.zP
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)
我已经设置了暂停和取消暂停场景+更改按钮纹理的功能:
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
}
}
我已设置触摸按钮:
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")}
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
位置=触摸。位置(in:self)
节点=self.atPoint(位置)
如果node.name==“暂停”{
buttonpauseplayTouched()按钮
}
else{打印(“空白”)}
}
}
现在,当我按下“暂停”按钮时,我可以暂停和取消暂停场景,但纹理不会改变吗?怎么了?或者,如果要在暂停时将其他spritekitnode添加到场景中,则无法执行此操作
第二个问题是,我在现场还有一些其他的SKSpriteNodes,当我触摸它们时,我就开始行动了。若我在场景暂停时触摸它们,什么也不会发生,但当我取消暂停场景时,对象上的操作会运行。当场景暂停时,如何防止我无法对对象执行操作。我尝试:
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")}
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
位置=触摸。位置(in:self)
节点=self.atPoint(位置)
如果node.name==“暂停”{
buttonpauseplayTouched()按钮
}
如果场景?.view?.isPaused==true&&node.name==“对象”{print(“Nothing!!”}
}
else{打印(“空白”)}
}
}
没有成功。谢谢你给我小费
更新:
再次感谢你们两位!
分层解决方案更好,我尝试了一下,效果很好!移动背景有一个小问题:
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()
}
override func didMove(to view:SKView){
createBackground()
}
func createBackground(){
因为我在0…3{
background=SKSpriteNode(图像名为:“background1.png”)
background.name=“background”
背景设置刻度(0.694)
background.position=CGPoint(x:0,y:CGFloat(i)*background.size.height)
background.zPosition=1
gameLayer.addChild(背景)
}
}
func moveBackground(){
gameLayer.枚举子节点(名称为“background”),使用:({
(节点,错误)在
节点位置y-=7
如果node.position.y<-((自身背景大小高度)){
节点.位置.y+=(自身.背景.大小.高度)*3
}
}))
}
覆盖函数更新(uCurrentTime:TimeInterval){
moveBackground()
}
当我暂停游戏层时,背景仍在移动。当游戏玩家暂停时,如何编辑代码以停止移动地面?我希望只需对代码稍作修改就可以解决这个问题。谢谢 我将我的控件放在他们自己的图层(SKNode)controlsLayer.addChild(pauseButton)上,并将游戏对象放在他们自己的图层“gameLayer”上,然后当我想暂停游戏时,我只需暂停gameLayer(gameLayer.isPaused=true)。这会阻止游戏对象移动,给人暂停的感觉,但仍然允许我进行动作、添加对象等操作
override func update(_ currentTime: TimeInterval) {
if !gameLayer.isPaused {
moveBackground()
}
}
我个人是罗恩所说的话的粉丝,但我只想用几句话向你展示如何做到这一点:
func buttonpauseplayTouched() {
playPause.texture = SKTexture(imageNamed: isPaused ? "buttonPlay" : "buttonPause")
//pausing the scene rather than a view
self.isPaused = !self.isPaused
}
所以,基本上,根据场景的
isPaused
属性,选择纹理。然后,切换场景的暂停属性。在这种情况下,您不需要显示变量。谢谢!层的解决方案正是我想要的。我更新了我的问题,因为我在移动背景方面有问题。谢谢你的提示。我编辑了我的答案,告诉你如何暂停背景谢谢!我尝试了两种解决方案,效果很好!但像你说的,分层解决方案更好。一、 我更新了我的问题,因为移动了地面。谢谢你给我小费。