我想让屏幕的右边使节点跳跃,左边的上半部分向左移动,下半部分向右移动。
一些不好的东西:
当我点击快速左、右、左、右多次时(它将停止并不会移动节点)
当我轻触屏幕的右侧部分并轻触左侧或右侧部分时,并非每次节点在下降时移动,有时第一个问题会重复出现
下面是一个具有相同机制的游戏示例:
https://itunes.apple.com/us/app/staying-together/id923670329?mt=8
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let tapper = UILongPressGestureRecognizer(target: self, action: "tappedScreen:");
tapper.minimumPressDuration = 0.1
view.addGestureRecognizer(tapper)
}
func jumpAction(){
if(!isJumping){
hero.physicsBody?.applyImpulse(CGVector(dx: 0, dy: ySpeed))
}
isJumping = true
}
func tappedScreen(recognizer: UITapGestureRecognizer){
if(recognizer.state == UIGestureRecognizerState.Began){
let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)
let touchY = self.convertPointFromView(recognizer.locationInView(self.view)).y
// only the bottom part of the screen, that way the UI button will be able to touch
if(touchY < self.frame.size.height/4){
if(touchX > 0){
println("up - longer");
} else if(touchX < -(self.frame.size.width/4)){
println("left - longer");
hero.runAction(SKAction.repeatActionForever(moveLeft), withKey: "longTap")
} else {
println("right - longer");
hero.runAction(SKAction.repeatActionForever(moveRight), withKey: "longTap")
}
}
} else {
if(touchX <= 0){
if (recognizer.state == UIGestureRecognizerState.Ended) {
println("ended, also stops the node from moving");
hero.removeActionForKey("longTap")
}
}
}
}
// I think this is need to mmove the node on single tap
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
let touch: UITouch = touches.first as! UITouch;
let location:CGPoint = touch.locationInNode(self);
let moveLeft = SKAction.moveByX(-15, y: 0, duration: 0.1)
let moveRight = SKAction.moveByX(15, y: 0, duration: 0.1)
// only the bottom part of the screen, that way the UI button will be able to touch
if(location.y < self.frame.size.height/4){
if(location.x > 0){
println("up");
self.jumpAction();
} else if(location.x < -(self.frame.size.width/4)){
hero.runAction(moveLeft);
println("left");
} else {
hero.runAction(moveRight);
println("right");
}
}
}
最佳答案
这是因为tappedScreen有一个引用uitappesturerecognizer的参数。必须将其设置为UILongPressGestureRecognizer。
func tappedScreen(识别器:UILongPressGestureRecognizer){
}
希望有帮助:)
关于swift - UILongPressGestureRecognizer无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31482752/