gpt4 book ai didi

swift 2.0 分配了哪个图像

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

我想知道如何检查哪个图像分配给了 SKSpriteNode。

这是我的代码:2月11日更新

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.physicsWorld.contactDelegate = self

}//enddidMoveToView

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
print("hello")
var ball = SKSpriteNode()
for touch in touches {

let location = touch.locationInNode(self)
//calls a ball with a randomImgColor
ball = SKSpriteNode(imageNamed:"ball\(arc4random_uniform(3))")
ball.xScale = 0.1
ball.yScale = 0.1
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2.5)
ball.position = location

if ball == SKSpriteNode(imageNamed:"ball0") {
print("the red ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball1") {
print("the green ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball2") {
print("the blue ball was assigned")
}

self.addChild(ball)
}//ends touch
}//endtouchesBegan


override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}


}//end GameScene

最佳答案

我为您提供了一个解决方案,但它可能不是您正在寻找的。由于您通过将随机字符串传递到 SKSpriteNode 来创建图像,因此您可以将该字符串存储在变量中并设置 SKSpriteNotes .name 属性到完全相同的事物,然后检查名称的值。这节省了您创建实例进行检查的麻烦:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
var ball = SKSpriteNode() // in your for loop!
let location = touch.locationInNode(self)
let random = "ball\(arc4random_uniform(3))" // <-- notice
ball = SKSpriteNode(imageNamed:random)
ball.xScale = 0.1
ball.yScale = 0.1
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2.5)
ball.name = random // <-- set name == to your ball type
ball.position = location

// safely unwrap ball name
guard let ballName = ball.name else {
return
}
print(ballName)
// check ball name
if ballName == "ball0" {
print("the red ball was assigned")
} else if ballName == "ball1" {
print("the green ball was assigned")
} else if ballName == "ball2" {
print("the blue ball was assigned")
}

self.addChild(ball)
}
}//ends touch
<小时/>

原帖:

你的条件逻辑混淆了,你缺少一个大括号:

if ball == SKSpriteNode(imageNamed:"ball0") {
print("the red ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball1") {
print("the green ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball2") {
print("the blue ball was assigned")
}

虽然我不确定你的意图是什么,但我认为你希望 for in 循环中的条件语句正确吗?

for touch in touches {
let location = touch.locationInNode(self)

//calls a ball with a randomImgColor
ball = SKSpriteNode(imageNamed:"ball\(arc4random_uniform(3))")
ball.position = location

if ball == SKSpriteNode(imageNamed:"ball0") {
print("the red ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball1") {
print("the green ball was assigned")
} else if ball == SKSpriteNode(imageNamed:"ball2") {
print("the blue ball was assigned")
}

self.addChild(ball)
}

关于swift 2.0 分配了哪个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330511/

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