【问题标题】:TouchedEnded over TouchesBegan - Swift iOS SpriteKitTouchedEnded over TouchesBegan - Swift iOS SpriteKit
【发布时间】:2015-02-10 00:34:39
【问题描述】:

我会开始触摸(将您带到一个单独的场景),但我也有一个 SKSpriteNode 设置,当您触摸它时,您会进入另一个场景。这是我用于查找settingsPage 节点是否已被触摸的代码。

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)

    if (settingsPage .containsPoint(touchLocation))
    {
        println("Going to Settings")
        settingsScene()
    }   
}

由于某种原因,开始的触摸优先于结束的触摸。

他们是解决这个问题的方法吗?

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */


    let touch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)

    if (settingsPage .containsPoint(touchLocation))
    {
        println("Going to Settings")
        settingsScene()
   }






    game1 = 1

    if (Menu == 0) {
        movingObjects.speed = 1
        birdPhysics()
        let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:0.8)
        bird.runAction(SKAction.repeatActionForever(action))



        taptoflap.removeFromParent()

        started = 1

        if (powerupStatus == 1) {
            PUten = NSTimer.scheduledTimerWithTimeInterval(8, target: self, selector: Selector("plustenSpawn"), userInfo: nil, repeats: true)
        }

    }

    if (gameOver == 0) {




        bird.physicsBody?.velocity = CGVectorMake(0, 0)
        bird.physicsBody?.applyImpulse(CGVectorMake(0, impulse))



        // Add 1 to the currentTouches
        currentTouches++

        Menu = 1

    } else {
        scoreLabel.text = "0"
        bird.physicsBody?.velocity = CGVectorMake(0,0)
        settingsPage.removeFromParent()
        gameOver = 0                                                                                
        movingObjects.speed = 1
        // Animate Bird
        var animation = SKAction.animateWithTextures([birdTexture, birdTexture2, birdTexture3, birdTexture4], timePerFrame: 0.08)
        var makeBirdFlap = SKAction.repeatActionForever(animation)
        bird.runAction(makeBirdFlap)
        bird.texture = birdDeadTexture
        normButton.removeFromSuperview()
        highscoreClassic.removeFromParent()

        let height = self.frame.size.height
        let width = self.frame.size.width

        var speedGameScene: SpeedGameScene = SpeedGameScene(size: CGSizeMake(width, height))
        var spriteView: SKView = self.view as SKView!
        var trans:SKTransition = SKTransition.doorsCloseHorizontalWithDuration(0.7)
        spriteView.presentScene(speedGameScene, transition: trans)

    }
}

【问题讨论】:

  • touchesBegan 将始终在touchesEnded 之前执行。我不明白你的问题?
  • 好吧,我的场景中有一个节点,当我触摸该节点时,我想导航到另一个场景,但问题是该节点仅在游戏结束时显示,但 touchesBegan 将运行再次游戏(如果点击屏幕)?这样更有意义吗?
  • 你也可以显示touchesBegan 代码吗?

标签: swift touch-event


【解决方案1】:

touchesBegan 中的代码之后放置一个return 以不执行其余代码。

if (settingsPage.containsPoint(touchLocation))
{
    println("Going to Settings")
    settingsScene()
    return
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多