gpt4 book ai didi

swift - 如果达到一定分数则添加 Sprite

转载 作者:行者123 更新时间:2023-11-28 06:36:02 25 4
gpt4 key购买 nike

我有一个简单的游戏。您从一个球开始,然后弹跳直到获得 10 分。当你达到 10 分时,另一个球会出现(这样可以更快地到达下一个地标)。当达到 20 分时,出现第三个球。我已经设置好了,但我需要知道如何使用 case 执行 if 语句。

这是我要实现的语句。

   if (score == 10) && secondball.parent == nil {

这是我目前所拥有的:

import SpriteKit

class GameScene: SKScene {

override func didMoveToView(view: SKView) {

let ball = Ball()

ball.position = CGPoint(x:self.frame.midX, y:440)

addChild(ball)



ball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false

ball.physicsBody?.friction = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody!.categoryBitMask = 0




override func update(currentTime: CFTimeInterval) {


if shouldUpdateTexture {
shouldUpdateTexture = false
switch score {
case 10...19: backgroundNode.texture = SKTexture(imageNamed: "orangebackground")

let secondball = Ball()
secondball.position = CGPoint(x:self.frame.midX * 1.65, y: 440)
addChild(secondball)



secondball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
secondball.physicsBody?.dynamic = true
secondball.physicsBody?.allowsRotation = false

secondball.physicsBody?.friction = 0
secondball.physicsBody?.angularDamping = 0
secondball.physicsBody?.linearDamping = 0
secondball.physicsBody?.usesPreciseCollisionDetection = true
secondball.physicsBody!.categoryBitMask = 0



case 20...29: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")

let thirdball = Ball()
thirdball.position = CGPoint(x:self.frame.midX * 0.35, y:440)
addChild(thirdball)



thirdball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
thirdball.physicsBody?.dynamic = true
thirdball.physicsBody?.allowsRotation = false

thirdball.physicsBody?.friction = 0
thirdball.physicsBody?.angularDamping = 0
thirdball.physicsBody?.linearDamping = 0
thirdball.physicsBody?.usesPreciseCollisionDetection = true
thirdball.physicsBody!.categoryBitMask = 0

case 30...39: backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
case 40...49: backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
case 50...59: backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")

if ball.parent != nil {
let ball = Ball()

ball.position = CGPoint(x:self.frame.midX, y:440)

addChild(ball)



ball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false

ball.physicsBody?.friction = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody!.categoryBitMask = 0


}

case:10...19 有效,case:20...29 也有效,但对于 case 50...59 如果前一个球不存在,我只希望另一个球出现。

如果您需要更多或更少的信息,请在下方评论。我会立即更新。

------更新----代码运行良好,除非分数达到 50 以检查球是否存在。

谢谢,乔丹。

最佳答案

case 50...59: 中,只需添加一个 if 语句来检查球是否已经存在。添加这样的内容:

if thirdBall.parent == nil {

addChild(thirdBall)
// Whatever other code you want to run only if the ball needs to be added to the scene

}

希望这对您有所帮助!

关于swift - 如果达到一定分数则添加 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089775/

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