如何检测用户何时在特定节点上向特定方向滑动?
How do I detect when a user swipes in a certain direction on a specific node?
我的屏幕顶部有 6 个不同的彩色圆圈落下。对于每个节点,您应该朝不同的方向滑动以得分(红色圆圈;向上滑动,蓝色圆圈;向下滑动等),但是如果用户向与分配给该特定节点的方向不同的方向滑动,它结束了游戏。
我对滑动手势非常不熟悉,所以我没有代码,但非常感谢帮助。
我如何生成我的节点:
func array() {
let colorCount = 5
let index=Int(arc4random_uniform(UInt32(colorCount)))
let colors = SKSpriteNode(imageNamed: "Color\(index+1)")
colors.size = CGSizeMake(130, 130)
colors.position = CGPointMake(size.width / 2, 650)
addChild(colors)
colors.runAction(
SKAction.moveByX(0, y: -1600,
duration: NSTimeInterval(8.5)))
}
只要落到场景底部以下,它们就会从场景中移除。
您可以像这样添加手势识别器:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender:UISwipeGestureRecognizer){
print("swiped right")
}
}
其他方向也一样。因此,当您生成一个节点时,您可以设置一个变量来存储当前节点的类型(或颜色),并基于该变量检查用户是否向正确的方向滑动。
提示:
请记住,如果每次加载场景时都添加手势识别器,则可能 运行 出现内存问题。这可能会发生,因为识别器被添加到视图而不是场景,所以无论场景是否被释放,识别器都会保持活动状态。阅读更多 .
编辑:
这是一个可行的示例,它与您当前的实现略有不同,但总的来说,它做的事情是一样的。您可以复制并粘贴以查看其工作原理。它应该让您了解可以在哪些方向上解决问题,例如:
- 如何跟踪当前节点的颜色
- 如何创建SKSpriteNode的子类
- 如何在节点结束时移除屏幕
可能还有更多(比如如何在分数增加时自动更新标签)
enum Color:UInt32 {
case Red // 0 - texture should be Color0@2x.png
case Blue // 1 - texture should be Color1@2x.png
//case Green // 2 - texture should be Color2@2x.png
//case Yellow // 3 - texture should be Color3@2x.png
case NumberOfColors
}
class Circle:SKSpriteNode {
let textureColor:Color
//Initialize a circle with color and size
init(textureColor:Color, size:CGSize){
self.textureColor = textureColor
super.init(texture: SKTexture(imageNamed: "Color\(textureColor.rawValue)"), color:.clearColor(), size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene: SKScene {
let label = SKLabelNode(fontNamed: "ArialMT")
var score: Int = 0 {
didSet {
label.text = "Score: \(score)"
}
}
var currentCircle:Circle?
func spawnRandomCircle() {
//Randomize a circle. If you fail to randomize index correctly, default circle will be blue.
let index = arc4random_uniform(Color.NumberOfColors.rawValue)
let circle = Circle(textureColor: Color(rawValue: index) ?? Color.Blue, size: CGSize(width: 50, height: 50))
circle.position = CGPointMake(size.width / 2, 650)
addChild(circle)
//Move circle (and remove it, if end-up offscreen)
let move = SKAction.moveByX(0, y: -1600, duration: NSTimeInterval(8.5))
let moveAndRemove = SKAction.sequence([move, SKAction.removeFromParent()])
circle.runAction(moveAndRemove, withKey: "moving")
//Update currentCircle
currentCircle = circle
}
override func didMoveToView(view: SKView) {
//Setup score label
addChild(label)
label.position = CGPoint(x: frame.minX+50, y: frame.maxY-50) //Put a score label in top left corner
label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
label.text = String(score)
//Create gesture recognizers
let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
swipeUp.direction = .Up
view.addGestureRecognizer(swipeUp)
let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
swipeDown.direction = .Down
view.addGestureRecognizer(swipeDown)
//Spawn first circle
spawnRandomCircle()
}
//This method accepts the Color associated with a certian swipe direction
//eg. if Swipe Up is detected, Red color will be passed ...
func validateSwipe(color:Color){
if let circle = currentCircle {
circle.removeFromParent()
if circle.textureColor == color {++score} else {--score}
spawnRandomCircle()
}
}
func swipedUp(sender:UISwipeGestureRecognizer){
validateSwipe(Color.Red) //Red circle - Swipe Up
}
func swipedDown(sender:UISwipeGestureRecognizer){
validateSwipe(Color.Blue) //Blue circle - Swipe Down
}
}
这可以通过许多不同的方式完成,如果您有任何问题,请随时提出...
我的屏幕顶部有 6 个不同的彩色圆圈落下。对于每个节点,您应该朝不同的方向滑动以得分(红色圆圈;向上滑动,蓝色圆圈;向下滑动等),但是如果用户向与分配给该特定节点的方向不同的方向滑动,它结束了游戏。
我对滑动手势非常不熟悉,所以我没有代码,但非常感谢帮助。
我如何生成我的节点:
func array() {
let colorCount = 5
let index=Int(arc4random_uniform(UInt32(colorCount)))
let colors = SKSpriteNode(imageNamed: "Color\(index+1)")
colors.size = CGSizeMake(130, 130)
colors.position = CGPointMake(size.width / 2, 650)
addChild(colors)
colors.runAction(
SKAction.moveByX(0, y: -1600,
duration: NSTimeInterval(8.5)))
}
只要落到场景底部以下,它们就会从场景中移除。
您可以像这样添加手势识别器:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
}
func swipedRight(sender:UISwipeGestureRecognizer){
print("swiped right")
}
}
其他方向也一样。因此,当您生成一个节点时,您可以设置一个变量来存储当前节点的类型(或颜色),并基于该变量检查用户是否向正确的方向滑动。
提示:
请记住,如果每次加载场景时都添加手势识别器,则可能 运行 出现内存问题。这可能会发生,因为识别器被添加到视图而不是场景,所以无论场景是否被释放,识别器都会保持活动状态。阅读更多
编辑:
这是一个可行的示例,它与您当前的实现略有不同,但总的来说,它做的事情是一样的。您可以复制并粘贴以查看其工作原理。它应该让您了解可以在哪些方向上解决问题,例如:
- 如何跟踪当前节点的颜色
- 如何创建SKSpriteNode的子类
- 如何在节点结束时移除屏幕
可能还有更多(比如如何在分数增加时自动更新标签)
enum Color:UInt32 { case Red // 0 - texture should be Color0@2x.png case Blue // 1 - texture should be Color1@2x.png //case Green // 2 - texture should be Color2@2x.png //case Yellow // 3 - texture should be Color3@2x.png case NumberOfColors } class Circle:SKSpriteNode { let textureColor:Color //Initialize a circle with color and size init(textureColor:Color, size:CGSize){ self.textureColor = textureColor super.init(texture: SKTexture(imageNamed: "Color\(textureColor.rawValue)"), color:.clearColor(), size: size) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } class GameScene: SKScene { let label = SKLabelNode(fontNamed: "ArialMT") var score: Int = 0 { didSet { label.text = "Score: \(score)" } } var currentCircle:Circle? func spawnRandomCircle() { //Randomize a circle. If you fail to randomize index correctly, default circle will be blue. let index = arc4random_uniform(Color.NumberOfColors.rawValue) let circle = Circle(textureColor: Color(rawValue: index) ?? Color.Blue, size: CGSize(width: 50, height: 50)) circle.position = CGPointMake(size.width / 2, 650) addChild(circle) //Move circle (and remove it, if end-up offscreen) let move = SKAction.moveByX(0, y: -1600, duration: NSTimeInterval(8.5)) let moveAndRemove = SKAction.sequence([move, SKAction.removeFromParent()]) circle.runAction(moveAndRemove, withKey: "moving") //Update currentCircle currentCircle = circle } override func didMoveToView(view: SKView) { //Setup score label addChild(label) label.position = CGPoint(x: frame.minX+50, y: frame.maxY-50) //Put a score label in top left corner label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left label.text = String(score) //Create gesture recognizers let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:")) swipeUp.direction = .Up view.addGestureRecognizer(swipeUp) let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:")) swipeDown.direction = .Down view.addGestureRecognizer(swipeDown) //Spawn first circle spawnRandomCircle() } //This method accepts the Color associated with a certian swipe direction //eg. if Swipe Up is detected, Red color will be passed ... func validateSwipe(color:Color){ if let circle = currentCircle { circle.removeFromParent() if circle.textureColor == color {++score} else {--score} spawnRandomCircle() } } func swipedUp(sender:UISwipeGestureRecognizer){ validateSwipe(Color.Red) //Red circle - Swipe Up } func swipedDown(sender:UISwipeGestureRecognizer){ validateSwipe(Color.Blue) //Blue circle - Swipe Down } }
这可以通过许多不同的方式完成,如果您有任何问题,请随时提出...