真的需要关于UITouch的帮助(精灵套件)

时间:2015-07-29 12:04:05

标签: swift sprite-kit

我喜欢创造左侧和右侧,当我触摸它们时,一只鸟会飞向左侧或右侧!

  1. 我创造了一只鸟

    var birdtexture = SKTexture(imageNamed: "flappy1.png")            
    bird = SKSpriteNode(texture: birdtexture)
    bird.position
    bird.size
    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.width/2)
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = false
    self.addChild(bird)
    
  2. 然后我创建左节点

    var groundleft = SKSpriteNode()
    groundleft.position = CGPoint(x: 0, y: 50)
    groundleft.size = CGSize(width: 500, height: 100)
    groundleft.color = UIColor(red: 7, green: 5, blue: 7, alpha: 20)
    
    
    groundleft.physicsBody = SKPhysicsBody(rectangleOfSize:CGSizeMake(self.size.width, 100))
    groundleft.physicsBody?.dynamic = true
    groundleft.physicsBody?.affectedByGravity = false
    groundleft.name = "toleft"
    self.addChild(groundleft)
    
  3. 然后我去TouchBegan

    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)
    
    if(touchedNode.name == "toleft"){
    
        bird.physicsBody?.velocity = CGVectorMake(0, 0)
        bird.physicsBody?.applyImpulse(CGVectorMake(-10, 100))
    
    }
    
  4. 为什么它没有用。我的方法错了吗?有没有其他方法让我工作? 请帮忙,非常感谢

1 个答案:

答案 0 :(得分:0)

touchesBegan方法中试试这个:

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

    if let touch = touches.first as? UITouch {

        let touchLocation = touch.locationInNode(self)
        if nodeAtPoint(touchLocation) == groundleft {   //your sprite node
            //do something

        } 
    }
    super.touchesBegan(touches , withEvent:event)
}