根据旋转施加冲动 - 斯威夫特

时间:2016-09-13 17:16:13

标签: swift sprite-kit rotation

我正在创建一个2D游戏,其中我有一个大炮停留在屏幕的底部,将射弹射向屏幕上方的某些目标。用户可以旋转加农炮以瞄准目标。我希望将射弹射向正面所在的位置。我不知道该怎么做,我需要你的帮助...继承我的代码 - >

import SpriteKit

class GameScene: SKScene {

    var canon = SKSpriteNode()
    var projectile = SKSpriteNode()


    var touching = false
    var locked = false
    var location = CGPoint()


    override func didMoveToView(view: SKView) {

        /* Setup your scene here */

        projectile.position = canon.position
        projectile.color = UIColor.whiteColor()
        projectile.size = CGSize(width: 50, height: 50)
        projectile.physicsBody?.affectedByGravity = false
        projectile.physicsBody?.linearDamping = 1
        projectile.physicsBody?.dynamic = true
        addChild(projectile)


        canon.zPosition = 2
        canon = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 200))
        canon.position = CGPoint(x: self.size.width/2.0, y: 10)

        addChild(canon)

            }

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

       /* Called when a touch begins */
        touching = true

        let touch = touches.first as UITouch!
        location = touch.locationInView(self.view)

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        rotateCanon(location.x)
        projectile.zRotation = canon.zRotation


    }

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

        touching = false

    }

    func rotateCanon(locationx:CGFloat) {
        if locationx <= frame.width/5 && touching {
            print(1)

            canon.runAction(SKAction.rotateByAngle(0.01, duration: 0.1))

        }
        if locationx > frame.width/5 && touching {
            print(2)

            canon.runAction(SKAction.rotateByAngle(-0.01, duration: 0.1))

        }
    }


}`

非常感谢,如果你能提供帮助那就太棒了:D

0 个答案:

没有答案