如何在与另一个对象接触时使Sprite移动

时间:2016-05-03 21:22:39

标签: swift sprite

这是我第一次发布堆栈溢出,如果我做错了,请告诉我。我的任务很简单,我想发送精灵在与网球架/物体接触时随机移动。我需要帮助完成任务的第二部分。谁能帮帮我吗?我对Swift比较陌生,所以请记住这一点。

类ViewController:UIViewController {

@IBOutlet weak var playButton: UIButton!

@IBOutlet weak var tennisRacket: UIImageView!

@IBOutlet weak var moveRacket: UIButton!

@IBOutlet weak var scoreLabel: UILabel!

@IBOutlet weak var scoreText: UILabel!


var tennisScene = SKScene()
var tennisBallSprite = SKSpriteNode(imageNamed: "tennisball")



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    // hide or show elements

    playButton.hidden = false
    tennisRacket.hidden = true
    moveRacket.hidden = true
    scoreLabel.hidden = true
    scoreText.hidden = true
    tennisBallSprite.hidden = true

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}


@IBAction func playPressed(sender: UIButton) {


    playButton.hidden = true
    tennisRacket.hidden = false
    moveRacket.hidden = false
    tennisBallSprite.hidden = false


    tennisBallSprite.position = CGPointZero
    tennisBallSprite.size = CGSize(width: 20, height: 20)
    tennisBallSprite.physicsBody?.affectedByGravity = true
    tennisBallSprite.physicsBody?.allowsRotation = true
    tennisScene.addChild(tennisBallSprite)

}



@IBAction func hitBall(sender: UIButton) {





    self.tennisRacket.transform = CGAffineTransformMakeRotation((-15 * CGFloat(M_PI)) / 180.0)



    delay(1/5) {

        self.tennisRacket.transform = CGAffineTransformIdentity
    }



}

}

0 个答案:

没有答案