swift - Spritekit 中的 Pan Gesture Recognizer 移动对象

标签 swift sprite-kit draggable swift2 uipangesturerecognizer

我正在尝试在 spritkit 中制作游戏,但无法弄清楚如何像平移手势识别器一样在屏幕上左右拖动球。

import UIKit
import SpriteKit

class GameScene: SKScene,SKPhysicsContactDelegate {

  var ball = SKShapeNode()
  let slidePlayer = UIPanGestureRecognizer()

  override func didMoveToView(view: SKView) {   
    ball = SKShapeNode(circleOfRadius: 15)
    ball.position = CGPointMake(self.frame.size.width/2, 500)
    ball.fillColor = SKColor.purpleColor()
    self.addChild(ball)

    slidePlayer.addTarget(ball, action: "slide")
    self.view?.addGestureRecognizer(slidePlayer)
  }

  func slide(){
    print("slide")
  }
}

最佳答案

这是我在屏幕上左右拖动的 playButton 的示例代码。

import SpriteKit

class GameScene: SKScene {

    let playButton = SKSpriteNode(imageNamed: "play_unpresed")
    var fingureIsOnNinja = false
    let playCategoryName = "play"

    override func didMoveToView(view: SKView) {

        addPlayButton()
    }

    func addPlayButton(){

        playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        playButton.xScale = 0.2
        playButton.yScale = 0.2
        playButton.name = playCategoryName  //give it a category name
        self.addChild(playButton)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */
        for touch in (touches as! Set<UITouch>){
            let location = touch.locationInNode(self)

            if self.nodeAtPoint(location) == self.playButton {

                fingureIsOnNinja = true  //make this true so it will only move when you touch it.

            }
        }
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

        if fingureIsOnNinja {

            if let touch = touches.first as? UITouch {

                let touchLoc = touch.locationInNode(self)
                let prevTouchLoc = touch.previousLocationInNode(self)
                let ninja = self.childNodeWithName(playCategoryName) as! SKSpriteNode
                var newYPos = playButton.position.y + (touchLoc.y - prevTouchLoc.y)
                var newXPos = playButton.position.x + (touchLoc.x - prevTouchLoc.x)

                newYPos = max(newYPos, playButton.size.height / 2)
                newYPos = min(newYPos, self.size.height - playButton.size.height / 2)

                newXPos = max(newXPos, playButton.size.width / 2)
                newXPos = min(newXPos, self.size.width - playButton.size.width / 2)

                playButton.position = CGPointMake(newXPos, newYPos)  //set new X and Y for your sprite.
            }
        }
    }
}

希望对您有所帮助。

你可以关注THIS SpriteKit 中触摸的基本教程。

关于swift - Spritekit 中的 Pan Gesture Recognizer 移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31889842/

相关文章:

ios - ObserveSingleEvent 效果不佳

sprite-kit - CIFilter GaussianBlur 似乎在 iOS9.x 上被破坏了(与 SKEffectNode 一起使用)

Swift Spritekit 应用旋转脉冲

javascript - 拖到屏幕外时如何将div拖回第一个位置

jquery - Draggables Jquery UI,禁用可放置的单个 div

ios - Google Places iOS : How to provide context to API for relevant local results

ios - swift : '(NSObject, AnyObject)' does not have a member named 'subscript'

ios - Sprite 有时会消失 [Sprite Kit]

ios - SpriteKit 移动到 X :duration from Current Stat

jQuery UI 嵌套 droppables - 使draggable成为droppable