gpt4 book ai didi

swift - SpriteNode 不出现某些情况

转载 作者:行者123 更新时间:2023-11-28 08:35:00 26 4
gpt4 key购买 nike

我正在制作一个游戏,在该游戏中,在某些时候会出现一个新按钮和一个标签。问题是在某些情况下它们都不会出现。

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

/* Called when a touch begins */
circuloAmarillo.name = "button2"

let touch2 = touches.first
let positionInScene2 = touch2!.locationInNode(self)

let touchedNode2 = self.nodeAtPoint(positionInScene2)

if let name = touchedNode2.name {

print("name : \(name)")
if name == "button2" {
//do my stuff
circuloPrincipal.color = colorAmarillo
}
}

circuloAzul.name = "button3"

let touch3 = touches.first
let positionInScene3 = touch3!.locationInNode(self)

let touchedNode3 = self.nodeAtPoint(positionInScene3)

if let name = touchedNode3.name {
print("name : \(name)")
if name == "button3" {
//do my stuff
circuloPrincipal.color = colorAzul
}
}

circuloBlanco.name = "button4"

let touch4 = touches.first
let positionInScene4 = touch4!.locationInNode(self)
let touchedNode4 = self.nodeAtPoint(positionInScene4)

if let name = touchedNode4.name {

print("name : \(name)")
if name == "button4" {
//do my stuff
circuloPrincipal.color = colorBlanco
}
}

if gameStarted == false {

circuloBlanco.removeFromParent()

circuloPrincipal.color = colorAzul

enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true)

gameStarted = true

circuloPrincipal.runAction(SKAction.scaleTo(0.44, duration: 0.4))

score = 0
scoreLabel.text = "\(score)"
hits = 0
highscoreLabel.runAction(SKAction.fadeOutWithDuration(0.5))
}
}

func enemigos() {

circuloBlanco.size = CGSize(width: 60, height: 60)
circuloBlanco.position = CGPoint(x: frame.width / 2 - 105 , y: frame.height / 2 - 345)
circuloBlanco.color = colorBlanco
circuloBlanco.colorBlendFactor = 1.0
circuloBlanco.zPosition = 4.0

textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")

textLabel.text = "New color added"
textLabel.fontSize = 35
textLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 147)
textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
textLabel.colorBlendFactor = 1.0
textLabel.zPosition = 2.0

linea = SKSpriteNode(imageNamed: "linea")

linea.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 160)
linea.size = CGSize(width: frame.width, height: 50)
linea.alpha = 0.3
linea.zPosition = 1.0

let enemigo = SKSpriteNode(imageNamed: "enemigo")
enemigo.size = CGSize(width: 25, height: 25)
enemigo.zPosition = 3.0
enemigo.physicsBody = SKPhysicsBody(circleOfRadius: enemigo.size.height / 2)
enemigo.physicsBody?.categoryBitMask = physicsCategory.enemigo
enemigo.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal
enemigo.physicsBody?.collisionBitMask = physicsCategory.circuloPrincipal
enemigo.physicsBody?.dynamic = true

var colors = [colorAzul, colorAmarillo]

if score >= 22 { colors.append(colorBlanco) };let randomColorIndex = GKRandomDistribution(lowestValue: 0, highestValue: colors.count - 1).nextInt(); enemigo.color = colors[randomColorIndex]

if score == 20 {

let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)])

if linea.parent == nil {

linea.runAction(fadingAnimation)
self.addChild(linea)
}

if textLabel.parent == nil {

textLabel.runAction(fadingAnimation)
self.addChild(textLabel)
}

if circuloBlanco.parent == nil {
self.addChild(circuloBlanco)
}
}
}

最佳答案

我认为你丢失了这一行:

func enemigos() {
circuloBlanco = SKSpriteNode(imageNamed:"nameOfYourWhiteButton.png")
...
}

这是必要的,因为你removeFromParent这个元素,所以下次你调用enemigos函数时,我看到你改变了circuloBlanco参数。

关于swift - SpriteNode 不出现某些情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033048/

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