gpt4 book ai didi

ios - SpriteKit swift 3 中消失的按钮和标签

转载 作者:搜寻专家 更新时间:2023-11-01 07:16:29 27 4
gpt4 key购买 nike

我正在 SpriteKit 中使用 swift 3 制作简单的数学游戏,这是我在 SpriteKit 中的第二个项目,所以请耐心等待。我有一个奇怪的问题,因为有时我的应用程序可以正常工作,但有时我的标签和对象会感到绝望。在我的应用程序中,顶部的橙色圆圈随着标签中的随机数字下降,当用户单击红线下方的按钮之一时,标签中的数字除以按钮上的数字,当除以数字时等于 1 场比赛应该停止,它可能会弹出一个标签,上面写着“你赢了!”。(照片)

[ ]

如果落球在数字等于 1 之前触及红线,则游戏应该停止,并且它可能会弹出带有文本“游戏结束”的标签。(照片)

enter image description here

有时其中一个标签会消失(照片 1)和(照片 2)

enter image description here enter image description here

有时落球上的数字在划分后消失(照片)

disappearing label

或在游戏开始时。我不知道为什么会这样,但有时效果很好。

我的 GameScene 代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {


enum gametype {
case game
case wait
}

override func didMove(to view: SKView) {

print("test1")
koniec = self.childNode(withName: "gameover") as! SKLabelNode
ball = self.childNode(withName: "pilka") as! SKSpriteNode
ball.position.y = view.frame.height/2 - 20
numberinball = self.childNode(withName: "liczba") as! SKLabelNode
numberinball.position.x = ball.position.x
numberinball.position.y = (ball.position.y) - 14
random()
ball.alpha = 1
numberinball.alpha = 1
koniec.alpha = 0
button2 = self.childNode(withName: "2") as! SKSpriteNode
button3 = self.childNode(withName: "3") as! SKSpriteNode
button4 = self.childNode(withName: "4") as! SKSpriteNode
button5 = self.childNode(withName: "5") as! SKSpriteNode
button6 = self.childNode(withName: "6") as! SKSpriteNode
button7 = self.childNode(withName: "7") as! SKSpriteNode
button8 = self.childNode(withName: "8") as! SKSpriteNode
button9 = self.childNode(withName: "9") as! SKSpriteNode
button2.alpha = 1
button3.alpha = 1
button4.alpha = 1
button5.alpha = 1
button6.alpha = 1
button7.alpha = 1
button8.alpha = 1
button9.alpha = 1
gamedidstart1 = .game
}


//Variables

var button2 = SKSpriteNode()
var button3 = SKSpriteNode()
var button4 = SKSpriteNode()
var button5 = SKSpriteNode()
var button6 = SKSpriteNode()
var button7 = SKSpriteNode()
var button8 = SKSpriteNode()
var button9 = SKSpriteNode()

var ball = SKSpriteNode()

var numberinball = SKLabelNode()
var koniec = SKLabelNode()

var liczba = Int()
var numberingame = 0

var gamedidstart1:gametype = .game

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch: AnyObject in touches {
let location = touch.location(in: self)
print(location)

if gamedidstart1 == .game {
if button2.contains(location) && numberingame % 2 == 0 {
numberingame = numberingame/2
print("number has been divided by 2")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button3.contains(location) && numberingame % 3 == 0 {
numberingame = numberingame/3
print("number has been divided by 3")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button4.contains(location) && numberingame % 4 == 0 {
numberingame = numberingame/4
print("number has been divided by 4")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button5.contains(location) && numberingame % 5 == 0 {
numberingame = numberingame/5
print("number has been divided by 5")
numberinball.alpha = 1
numberinball.text = String(numberingame)
print(numberinball.alpha)
}else if button6.contains(location) && numberingame % 6 == 0 {
numberingame = numberingame/6
print("number has been divided by 6")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button7.contains(location) && numberingame % 7 == 0 {
numberingame = numberingame/7
print("number has been divided by 7")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button8.contains(location) && numberingame % 8 == 0 {
numberingame = numberingame/8
print("number has been divided by 8")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}else if button9.contains(location) && numberingame % 9 == 0 {
numberingame = numberingame/9
print("number has been divided by 9")
numberinball.alpha = 1
numberinball.text = String(numberingame)
}
print(numberingame)
print(numberinball.alpha)
}//numberinball.text = String(numberingame)
}
}


//Movng the ball
func ballpos() {
ball.run(SKAction.moveTo(y: (ball.position.y) - 30, duration: 1))
numberinball.run(SKAction.moveTo(y: (numberinball.position.y) - 30, duration: 1))
}

//Randomizing number

func random() {

var randomized = Int()

repeat{
randomized = Int(arc4random_uniform(100)+1)
print("randomized liczba to: \(randomized)")
}while sprawdz(Liczba: randomized) || randomized == 1
numberingame = randomized
numberinball.text = String(numberingame)
print("liczba w labelu zostaje ustawiona na: \(randomized)")
}



//Checking if number is divided by 2,3 or 5

func sprawdz(Liczba:Int) ->Bool {

var geted = Liczba
print("liczba przyjęta to:\(geted)")
while true {
if geted % 2 == 0 && geted != 1{
geted = geted/2
print("gate if geted % 2 == 0 && geted != 1 positive, liczba po podziale to: \(geted)")
}else if geted == 1 {
print("number is equal to one after while block for number two, number: \(geted)")
return false
}else if geted < 10 {
print("number is lower than ten so its returning")
return false
}else {
print("gate for loop while for number two negative")
break
}
}

while true {
if geted % 3 == 0 && geted != 1{
geted = geted/3
print("gate if geted % 3 == 0 && geted != 1 positive, number after dividing: \(geted)")
}else if geted == 1 {
print("number is equal to one after while block for number three, number: \(geted)")
return false
}else if geted < 10 {
print("number is lower than 10: \(geted) so it is returned")
return false
}else {
print("gate for loop while for number three negative")
break
}
}

while true {
if geted % 5 == 0 && geted != 1{
geted = geted/5
print("gate % 5 == 0 && geted != 1 positive, number after dividing: \(geted)")
}else if geted == 1 {
print("number is equal one after while block for number five: \(geted)")
return false
}else if geted < 10 {
print("number is lower than 10 \(geted) so it is returned")
return false
}else {
print("gate for loop while for number five negative")
break
}
}
return true
}



override func update(_ currentTime: TimeInterval) {


if gamedidstart1 == .game {
if ball.position.y <= koniec.position.y{
koniec.alpha = 1
koniec.text = "Game Over"
ball.removeAllActions()
ball.alpha = 0
numberinball.alpha = 1
numberinball.removeAllActions()
gamedidstart1 = .wait
//print(numberinball.alpha)
//print(numberinball.color!)
}else {
ballpos()
//print(numberinball.alpha)
//print(numberinball.color!)
}
if numberingame == 1 {
ball.removeAllActions()
ball.alpha = 1
numberinball.removeAllActions()
numberinball.alpha = 1
koniec.alpha = 1
koniec.text = "You Win!"
gamedidstart1 = .wait
//print(numberinball.alpha)
//print(numberinball.color!)
}
}
//numberinball.fontColor = UIColor.white


}

附言。我设置了按钮和标签的 alpha 以确保它们不会丢失,因为它们的 alpha = 0

最佳答案

spritekit 中节点的

zPosition 是一场噩梦。除非特别设置,否则具有相同 zPosition 的节点可以随机地在彼此前面或后面结束。

我建议确保需要出现在前面的节点的 zPosition 大于您希望在后台出现的节点。

关于ios - SpriteKit swift 3 中消失的按钮和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com