我的屏幕顶部有不同的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)))
}
只需从场景底部下方将它们从场景中移除即可。
答案 0 :(得分:2)
您可以像这样添加手势识别器:
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")
}
}
对其他方向做同样的事情。因此,当您生成一个节点时,您可以设置一个存储当前节点类型(或颜色)的变量,并根据该变量检查用户是否向右滑动。
提示:
请记住,如果每次加载场景时添加手势识别器,都会遇到内存问题。这可能发生,因为识别器被添加到视图而不是场景中,因此无论场景是否被释放,识别器都将保持活跃状态。阅读更多here。
修改强>
这是一个可行的例子,与你当前的实现略有不同,但总的来说,它做同样的事情。您可以复制并粘贴以查看其工作原理。它应该让你知道你可以去哪个方向解决问题,如:
可能还有一些(比如如何在分数增加时自动更新标签)
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
}
}
这可以通过多种方式完成,如果您有任何问题,请随时提出......