gpt4 book ai didi

swift - 如何根据分数添加 Sprite ?

转载 作者:行者123 更新时间:2023-11-30 13:09:33 25 4
gpt4 key购买 nike

到目前为止,我几乎完成了我的游戏。我有一个因重力而下落的球,人们轻敲它使其弹起。每次点击 Sprite 时,得分就会增加 +1。 View 加载时总共会显示 3 个球。我希望每个球都显示在某个点标记处(当玩家达到 10 点时显示一个球,当玩家达到 20 点时显示另一个球)。

我尝试将其放入 update(currentTime: CFTimeInterval) 中,但最终得到的是:

enter image description here

(这是 10 分中的第二个球。)

似乎有无数个球,直到该人达到 21 分为止,从而停止永无休止的级联。如果你点击级联,它会拾起一个球并让它跳出来,这正是我想要的。

这是GameScene.swift(不包括update(CFTimeInterval)函数)

import SpriteKit

class GameScene: SKScene {
var ball: Ball!
var secondball: Ball!


override func didMoveToView(view: SKView) {
//=======Ball 1=======//
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


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



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

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

class Ball: SKSpriteNode {



init() {
let texture = SKTexture(imageNamed: "Ball")
super.init(texture: texture, color: .clearColor(), size: texture.size())
userInteractionEnabled = true

}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let scene = self.scene as! GameScene
scene.score += 1

physicsBody?.velocity = CGVectorMake(0, 100)
physicsBody?.applyImpulse(CGVectorMake(0, 900))



}

}

override func update(currentTime: CFTimeInterval) {


if (score >= 10){
self.backgroundNode.texture = SKTexture(imageNamed: "orangebackground")
}


if (score >= 20){
self.backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
}


if (score >= 30) {
self.backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
}
if (score >= 40){
self.backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
}
if (score >= 50){
self.backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")
}
if (score >= 60){
self.backgroundNode.texture = SKTexture(imageNamed: "purplebackground")
}
if (score >= 70){
self.backgroundNode.texture = SKTexture(imageNamed: "brownbackground")
}
if (score >= 80) {
self.backgroundNode.texture = SKTexture(imageNamed: "maroonbackground")
}
if (score >= 90){
self.backgroundNode.texture = SKTexture(imageNamed: "tanbackground")
}
if (score >= 100){
self.backgroundNode.texture = SKTexture(imageNamed: "pinkbackground")
}
if (score >= 125) {
self.backgroundNode.texture = SKTexture(imageNamed: "bronzebackground")
}
if (score >= 150) {
self.backgroundNode.texture = SKTexture(imageNamed: "silverbackground")
}
if (score >= 175) {
self.backgroundNode.texture = SKTexture(imageNamed: "goldbackground")
}
if (score >= 200) {
self.backgroundNode.texture = SKTexture(imageNamed: "elitebackground")

}




}


}

最佳答案

更新方法的调用速度比增加的分数更快。因此,在每次调用更新方法时都会添加一个新球。

尝试这样的事情:

将这一行代码移到更新方法之外,以创建一个具有一个球实例的全局变量:

let secondball = Ball() 

更新内部:

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

关于swift - 如何根据分数添加 Sprite ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882693/

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