【问题标题】:touch moking sprite by tap not swipe swift通过点击触摸 moking sprite 不快速滑动
【发布时间】:2016-01-27 20:42:45
【问题描述】:

我的代码中有一个块,其中只有在您点击屏幕时精灵才会移动。当我向上或向下滑动时,我希望有这种行为。这是我的代码

import SpriteKit
import UIKit

class GameScene: SKScene {
    var porker:Porker!
    var touchLocation = CGFloat()
    var gameOver = false


    override func didMoveToView(view: SKView) {
        addBG()
        addPig()
    }

    func addBG() {
        let bg = SKSpriteNode(imageNamed: "bg");
        addChild(bg)
    }

    func addPig() {
        let Pig = SKSpriteNode(imageNamed: "pig")
        porker = Porker(guy:Pig)
        addChild(Pig)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


        for touch: AnyObject in touches{
            if !gameOver {
                touchLocation = (touch.locationInView(self.view!).y * -1) + (self.size.height/2)
            }

        }
        let moveAction = SKAction.moveToY(touchLocation, duration: 0.5)
        moveAction.timingMode = SKActionTimingMode.EaseOut
        porker.guy.runAction(moveAction)
    }
}


    func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

【问题讨论】:

    标签: ios swift sprite-kit touch


    【解决方案1】:

    用以下代码替换 touchesBegan

    // Store the start touch position
    let yTouchCurrentPosition = 0.0
    let yTouchDistance = 0.0
    let yTouchStartPosition = 0.0
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            yTouchStartPosition = touch.locationInNode(self).y
        }
    }
    
    // Calculate the distance of the touch movement
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            yTouchCurrentPosition = touch.locationInNode(self).y
            yTouchDistance = yTouchStartPosition - yTouchCurrentPosition
        }
    }
    
    // Reset all movement states and move sprite
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        yTouchCurrentPosition = 0.0
        yTouchDistance = 0.0
        yTouchStartPosition = 0.0
    
        let moveAction = SKAction.moveToY(CGPoint(porker.guy.location.x, porker.guy.location.y + yTouchDistance), duration: 0.5)
        moveAction.timingMode = SKActionTimingMode.EaseOut
        porker.guy.runAction(moveAction)
    }
    

    【讨论】:

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