精灵套件旋转重复

时间:2015-07-26 16:07:23

标签: sprite-kit swift2

你怎么像船一样来回制作精灵摇滚?

继承我写的代码:

import UIKit
import SpriteKit

let boat = SKSpriteNode(imageNamed: "boat_floor")
var rotationVal = CGFloat(-1)
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
    backgroundColor = UIColor.whiteColor()
    boat.size = CGSize(width: self.frame.size.width, height: self.frame.size.width)
    boat.position = CGPoint(x: self.frame.size.width/2, y: 0)
    boat.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "boat_floor"), size: boat.size)
    boat.physicsBody?.dynamic = false
    boat.alpha = 1
    self.addChild(boat)
    ChangeAngle()
    NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("ChangeAngle"), userInfo: nil, repeats: true)
}
func ChangeAngle(){
    let action = SKAction.rotateByAngle(rotationVal, duration:30)
    if rotationVal < 0 {
        rotationVal = CGFloat(M_PI)
        boat.runAction(action)
    }
    else if rotationVal > 0 {
        rotationVal = -CGFloat(M_PI)
        boat.runAction(action)
    }
    print(rotationVal)


}

}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以这样做(只需复制并粘贴代码以查看其工作原理):

import UIKit
import SpriteKit

let boat = SKSpriteNode(color: SKColor.greenColor(), size:CGSize(width: 200, height: 80))


class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

        backgroundColor = UIColor.whiteColor()

        boat.position = CGPoint(x: self.frame.size.width/2, y: 100)

        boat.alpha = 1
        self.addChild(boat)


        let rotate = SKAction.rotateByAngle(-0.6, duration:4)

        let sequence = SKAction.repeatActionForever(SKAction.sequence([rotate, rotate.reversedAction()]))

        let complete = SKAction.sequence([SKAction.rotateByAngle(0.3, duration:2), sequence])

        boat.runAction(complete, withKey:"someKey")

    }

}

在SpriteKit中使用NSTimer通常是个坏主意,因为它不尊重节点,场景或视图的暂停状态。