SpriteKit手势问题

时间:2015-03-27 20:55:22

标签: ios swift sprite-kit

因此,我一直在尝试将类似于megaman战网的磁贴系统实施到这个战术/益智游戏中。但是,我似乎无法通过滑动手势将精灵移动到所需的距离。

相反,手势最终将精灵移动到视图的左下角。在那个角落,手势似乎似乎没有按照配置运行。

为什么精灵会转移到左下角?

以下是代码:

class GameScene: SKScene {

var mageNodeOne: Mage?

//Swipe gesture handlers
func swipedRight(sender:UISwipeGestureRecognizer) {
    mageNodeOne!.position = CGPointMake(self.position.x + 115, self.position.y)
    println("swiped right")
}

func swipedLeft(sender:UISwipeGestureRecognizer) {
    mageNodeOne!.position = CGPointMake(self.position.x - 115, self.position.y)
    println("swiped left")
}

func swipedUp(sender:UISwipeGestureRecognizer) {
    mageNodeOne!.position = CGPointMake(self.position.x, self.position.y + 220)
    println("swiped up")
}

func swipedDown(sender:UISwipeGestureRecognizer) {
    mageNodeOne!.position = CGPointMake(self.position.x, self.position.y - 220)
    println("swiped down")
}

//Scene Setup
override func didMoveToView(view: SKView) {

    // Add gesture recognizers to view
    let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
    swipeRight.direction = .Right
    view.addGestureRecognizer(swipeRight)

    let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
    swipeLeft.direction = .Left
    view.addGestureRecognizer(swipeLeft)

    let swipeUp:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedUp:"))
    swipeUp.direction = .Up
    view.addGestureRecognizer(swipeUp)

    let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
    swipeDown.direction = .Down
    view.addGestureRecognizer(swipeDown)

    // Add tiles to view
    var tileSpacerOne = SKSpriteNode(imageNamed: "TileSpacer")
    tileSpacerOne.position = CGPoint(x: 508, y: 388)
    tileSpacerOne.zPosition = -1
    self.addChild(tileSpacerOne)

    var tileOne = SKSpriteNode(imageNamed: "TileExampleOne")
    tileOne.position = CGPoint(x: 350, y: 608)
    self.addChild(tileOne)

    var tileTwo = SKSpriteNode(imageNamed: "TileExampleOne")
    tileTwo.position = CGPoint(x: 350, y: 388)
    self.addChild(tileTwo)

    var tileThree = SKSpriteNode(imageNamed: "TileExampleOne")
    tileThree.position = CGPoint(x: 350, y: 168)
    self.addChild(tileThree)

    var tileFour = SKSpriteNode(imageNamed: "TileExampleOne")
    tileFour.position = CGPoint(x: 665, y: 608)
    self.addChild(tileFour)

    var tileFive = SKSpriteNode(imageNamed: "TileExampleOne")
    tileFive.position = CGPoint(x: 665, y: 388)
    self.addChild(tileFive)

    var tileSix = SKSpriteNode(imageNamed: "TileExampleOne")
    tileSix.position = CGPoint(x: 665, y: 168)
    self.addChild(tileSix)

    // Add mage to view
    mageNodeOne = Mage(imageNamed: "MageOne")
    mageNodeOne!.position = CGPointMake(667,475.5)
    addChild(mageNodeOne!)
}

1 个答案:

答案 0 :(得分:0)

为什么要将精灵移动到self + 115的位置,而不是mageNodeOne's当前位置+ 115。

示例:

 CGPointMake(self.position.x + 115, self.position.y)

应该是

 CGPointMake(mageNodeOne.position.x + 115, mageNodeOne.position.y)