简体   繁体   English

swift 2.0分配了哪个图像

[英]swift 2.0 which image was assigned

I would like to know how to check which image was assigned to a SKSpriteNode. 我想知道如何检查将哪个图像分配给SKSpriteNode。

Here is my code: updated feb11 这是我的代码:更新了feb11

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

I have a solution for you but it might not be what you were looking for. 我为您提供了解决方案,但它可能不是您想要的。 Since your creating an image by passing a randomized string into SKSpriteNode , you can instead store the string in a variable and set the SKSpriteNotes .name property to the same exact thing and then check the value of name. 由于是通过将随机字符串传递到SKSpriteNode创建图像的,因此您可以将字符串存储在变量中,并将SKSpriteNotes .name属性设置为相同的值,然后检查name的值。 This saves you the trouble of creating instances to check against: 这省去了创建实例进行检查的麻烦:

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    

original post: 原始帖子:

Your conditional logic is mixed up, you're missing a curly brace: 您的条件逻辑混乱不清,缺少大括号:

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")
}

Although I can't be sure what your intentions are but I'm thinking you want your conditional statement inside your for in loop correct? 尽管我不确定您的意图是什么,但我认为您希望for循环中的条件语句正确吗?

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) 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM