swift 2.0分配了哪个图像
我想知道如何检查分配给SKSpriteNode的图像 这是我的密码: 更新日期:2月11日swift 2.0分配了哪个图像,swift,image,random,Swift,Image,Random,我想知道如何检查分配给SKSpriteNode的图像 这是我的密码: 更新日期:2月11日 import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { override func didMoveToView(view: SKView) { /* Setup your scene here */ self.physicsWorld.contactDelegate = self
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
导入SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
self.physicsWorld.contactDelegate=self
}//enddidMoveToView
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
打印(“你好”)
var ball=SKSpriteNode()
接触{
let location=touch.locationInNode(自)
//使用随机颜色调用球
ball=SKSpriteNode(图像名为:“ball\(arc4random_uniform(3)))
ball.xScale=0.1
ball.yScale=0.1
ball.physicsBody=SKPhysicsBody(圆形半径:ball.size.height/2.5)
ball.position=位置
如果ball==SKSpriteNode(图像名为:“ball0”){
打印(“红色球已分配”)
}如果ball==SKSpriteNode(图像名为:“ball1”){
打印(“绿色球已分配”)
}如果ball==SKSpriteNode(图像名为:“ball2”){
打印(“蓝球已分配”)
}
self.addChild(ball)
}//结束接触
}//结束接触开始了
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
}
}//结束游戏场景
我为您提供了一个解决方案,但它可能不是您想要的。由于通过将随机字符串传递到SKSpriteNode
中来创建图像,因此可以将该字符串存储在变量中,并将SKSpriteNotes
.name
属性设置为相同的值,然后检查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
虽然我不能确定你的意图是什么,但我想你希望你的条件语句在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)
}
当然可以。第一次使用这项服务,我印象深刻,很高兴在这个世界上有像你这样的人:)我一定是弄错了。我认为读你的代码有道理,但我没有得到任何指纹。这个@dan Beaulieu有什么原因吗?在“touchesEnded”函数的顶部添加一条打印语句以确保它启动。更新后,看看我的新解决方案是否能让您在任何地方都完美无瑕,这正是我想要的谢谢
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)
}