gpt4 book ai didi

swift - 如何停止屏幕上的节点被触摸? SpriteKit

转载 作者:行者123 更新时间:2023-11-30 12:39:32 24 4
gpt4 key购买 nike

我正在创建一个游戏,屏幕上有一个节点,当玩家触摸屏幕时,该节点就可以工作,但我只想当玩家点击该节点时,然后 Action 就会发生。

Here is the code of what is going on.

var ballNode = SKSpriteNode(imageNamed: "balls.png")

var touchLocation = CGPoint()

override func didMove(to view: SKView) {

ballNode.size = CGSize(width: 100, height: 100)
ballNode.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.9)

self.addChild(ballNode)

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

let touch: UITouch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)

touchLocation = touch.location(in: self)

let moveDown = SKAction.moveTo(y: self.size.height*0.1, duration: 0.3)
let moveUp = SKAction.moveTo(y: self.size.height*0.9, duration: 0.3)
let moveSequence = SKAction.sequence([moveDown, moveUp])
ballNode.run(moveSequence)
let pointOfTouch = touch.location(in: self)
ballNode.contains(pointOfTouch)

最佳答案

可以这样完成。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if let touch = touches.first as UITouch! {

let touchLocation = touch.location(in: self)
if ballNode.frame.contains(touchLocation) {

let moveDown = SKAction.moveTo(y: self.size.height*0.1, duration: 0.3)
//let spin = SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1))
//let spin = SKAction.repeat(SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1), count: 3)
let spin = SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1)
let moveUp = SKAction.moveTo(y: self.size.height*0.9, duration: 0.3)
let moveSequence = SKAction.sequence([moveDown, spin , moveUp])
ballNode.run(moveSequence)
}
}
}

我添加了一行用于旋转 Action ,有 2 行注释行用于更多旋转。只需选择最适合您的即可

关于swift - 如何停止屏幕上的节点被触摸? SpriteKit ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42397018/

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