我的Swiperecognizer存在一个奇怪的问题。
我在屏幕上产生了对象。
let SpawnObject = SKAction.run({
() in
showobject()
})
let delay1 = SKAction.wait(forDuration: 0.9)
let SpawnDelay1 = SKAction.sequence([SpawnObject,delay1])
let SpawnDelayForever1 = SKAction.repeatForever(SpawnDelay1)
self.run(SpawnDelayForever1)
let distance = CGFloat(self.frame.height + 200)
let moveObject = SKAction.moveBy(x: -distance, y: 0, duration: TimeInterval(0.004 * distance))
let removeObject = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveObject,removeObject])
我有一些物理对象(Object2,B.png类似)。
func showObject(){
let texture = SKTexture(imageNamed: "A.png")
object = SKSpriteNode(texture: texture)
object.name = "A"
object.position = CGPoint(x: 0, y: self.frame.width)
object.setScale(0.7)
object.zPosition = 2
object.run(moveAndRemove)
object.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
object.physicsBody?.categoryBitMask = PhysicsCategory.Object
object.physicsBody?.collisionBitMask = PhysicsCategory.Object2
object.physicsBody?.contactTestBitMask = PhysicsCategory.Object2
object.physicsBody?.affectedByGravity = false
object.physicsBody?.isDynamic = true
addChild(object)
}
我在touchesBegan中设置了滑动识别器
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
node = self.atPoint(location)
if node.name == "A" {
//node.run(SKAction.moveBy(x: 1000, y: 0, duration: 1)). //Here I try set action only to touch
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(rightSlide(_:)))
rightSwipe.direction = .right
view!.addGestureRecognizer(rightSwipe)
}
else if node.name == "B" {
//node.run(SKAction.moveBy(x: -1000, y: 0, duration: 1)). //Here I try set action only to touch
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(leftSlide(_:)))
leftSwipe.direction = .left
view!.addGestureRecognizer(leftSwipe)
}else {print ("Blank space")}
}
}
func leftSlide(_ sender: UISwipeGestureRecognizer){
if node.name == "B" {
node.run(SKAction.moveBy(x: -1000, y: 0, duration: 1))
}else {print ("Bad swipe")}
}
func rightSlide(_ sender: UISwipeGestureRecognizer){
if node.name == "A" {
node.run(SKAction.moveBy(x: 1000, y: 0, duration: 1))
}else {print ("Bad swipe")}
}
现在问题。对象显示在屏幕上,当我在它们上面滑动时,它们会消失到一边。一切正常,直到我擦掉大约50个物体的东西,之后每个下一个滑动冻结应用程序微秒,对象怪异地向下跳几个像素(更多滑动,更多像素)并回到它们的原始位置,然后在这个奇怪的行为对象滑动之后。更多滑动=更大的冻结和更大的跳跃。
我尝试关闭物理,什么都没有。但是,当我直接将动作更改为触摸(无需滑动,只需触摸)时,应用程序将永远正常工作。所以必须有错误的滑动识别器。或不?我检查CPU和MEMORY,CPU有35%,内存大约50MB。屏幕上的节点最多20. FPS为60,但当应用程序冻结时,它会降至58(如闪烁)。
有什么建议吗?如何诊断问题在哪里?谢谢!
答案 0 :(得分:1)
您不应每次在touchesBegan方法中添加识别器,而应执行以下操作:
1)创建一个数组来存储所有识别器,以便以后轻松删除它们
2)仅添加一次识别器。请注意,识别器将添加到视图中,而不是添加到场景中
3)当场景即将从视图中删除时删除它们
import SpriteKit
class GameScene: SKScene,SKPhysicsContactDelegate {
var recognizers:[UIGestureRecognizer] = []
override func didMove(to view: SKView) {
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(GameScene.leftSlide(recognizer:)))
leftSwipe.direction = .left
leftSwipe.numberOfTouchesRequired = 1
self.view?.addGestureRecognizer(leftSwipe)
recognizers.append(leftSwipe)
//do the same for other recognizers
}
func leftSlide(recognizer:UISwipeGestureRecognizer){
print("Swipe")
}
//define other methods here to handle other recognizers...
override func willMove(from view: SKView) {
super.willMove(from: view)
for recognizer in recognizers {
self.view?.removeGestureRecognizer(recognizer)
}
recognizers.removeAll()
}
}