作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的游戏。您从一个球开始,然后弹跳直到获得 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/
我正在尝试开发右边框/Angular 具有特定 Angular (30°) 的表格。我见过一些类似的解决方案,但它们都无法在一定程度上发挥作用。如果我想从 30° 改变到 20°,我不想花太多力气。
我是一名优秀的程序员,十分优秀!