【发布时间】:2016-12-23 04:20:31
【问题描述】:
我一直在研究,并且肯定尝试了一段时间,以找到一种在您点击精灵时激活功能的方法。例如:如果您点击蓝色精灵,它会激活blue()。点击红色精灵,它会激活red()。我一直在寻找一段时间,所有的答案要么是旧版本的,要么完全不适合我。有人有什么想法吗?
编辑: 这是我正在使用的一些代码。
class GameScene: SKScene {
var blue = SKSpriteNode()
var red = SKSpriteNode()
var green = SKSpriteNode()
var help = SKLabelNode()
var score = SKLabelNode()
var points = 0
var loseAmount = 1.0
var firstAdvance = 0
var loseTimer = Timer()
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
blue = self.childNode(withName: "blue") as! SKSpriteNode
blue.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
blue.name = "blue"
blue.isUserInteractionEnabled = true
red = self.childNode(withName: "red") as! SKSpriteNode
red.position = CGPoint(x: self.frame.width / 2 + 100, y: self.frame.height / 2)
red.name = "red"
red.isUserInteractionEnabled = true
green = self.childNode(withName: "green") as! SKSpriteNode
green.position = CGPoint(x: self.frame.width / 2 - 100, y: self.frame.height / 2)
green.name = "green"
green.isUserInteractionEnabled = true
help = self.childNode(withName: "help") as! SKLabelNode
help.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 4 * 3)
help.name = "help"
help.isUserInteractionEnabled = false
score = self.childNode(withName: "score") as! SKLabelNode
score.position = CGPoint(x: self.frame.width / 2, y: self.frame.height - 40)
score.name = "score"
score.text = "\(points)"
score.isUserInteractionEnabled = false
}
func randomMove() {
blue.position = CGPoint(x:Int(arc4random_uniform(244)+38),y:Int(arc4random_uniform(404)+38))
red.position = CGPoint(x:Int(arc4random_uniform(244)+38),y:Int(arc4random_uniform(404)+38))
green.position = CGPoint(x:Int(arc4random_uniform(244)+38),y:Int(arc4random_uniform(404)+38))
}
func advance() {
if (firstAdvance == 0) {
firstAdvance = 1
help.removeFromParent()
points += 1
playPointSound()
randomMove()
loseTimer = Timer.scheduledTimer(timeInterval: loseAmount, target: self, selector: #selector(GameScene.lose), userInfo: nil, repeats: true)
} else if (firstAdvance == 1) {
points += 1
loseAmount -= 0.01
playPointSound()
self.loseTimer.invalidate()
loseTimer = Timer.scheduledTimer(timeInterval: loseAmount, target: self, selector: #selector(GameScene.lose), userInfo: nil, repeats: true)
}
}
func lose() {
playLoseSound()
let endSceneTemp = EndScene(fileNamed: "EndScene")
self.scene?.view?.presentScene(endSceneTemp!, transition: SKTransition.crossFade(withDuration: 0.1))
}
func playPointSound() {
let path = Bundle.main.path(forResource: "", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
pointSound = sound
sound.play()
} catch {
// couldn't load file :(
}
}
func playLoseSound() {
let path = Bundle.main.path(forResource: "", ofType:nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
loseSound = sound
sound.play()
} catch {
// couldn't load file
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches) {
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
if name == "blue" {
advance()
}
if name == "red" || name == "green"{
lose()
}
}
}
}
override func update(_ currentTime: TimeInterval) {
}
}
【问题讨论】:
-
这在很大程度上取决于您如何创建精灵,如何将它添加到场景中,以及场景中的其他内容。你能提供这些信息吗?
-
@Confused 我的精灵是通过 .sks 文件创建的。场景中除了一个红色节点、一个蓝色节点和一个绿色节点外,没有太多东西。希望这会有所帮助。
标签: ios sprite-kit swift3