SpriteKit,SKSpriteNode缩放与弹跳

时间:2015-06-13 12:38:42

标签: ios swift animation sprite-kit

我需要使用弹跳来缩放SKSpriteNode(使用physicsBody)。

我不想创建3个(例如)SKAction来制作它。

是否有人知道考虑到phisicsBody的恢复属性的比例方法?

提前谢谢

我的变体

class OpenPlayerScene : SKScene {
private var openFromFrame: CGRect?

private var logoImage: UIImage!

let ballCategory: UInt32  = 1
let wallCategory: UInt32  = 2

convenience init(size: CGSize, openFromFrame: CGRect, imageForOpen:UIImage!) {
    self.init(size:size)
    self.backgroundColor = SKColor.clearColor()
    self.scaleMode = .AspectFit
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody?.categoryBitMask = wallCategory

    self.openFromFrame = openFromFrame
    self.logoImage = imageForOpen
}

override init(size: CGSize) {
    super.init(size: size)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func initLogoNode (logoImage: UIImage!) {
    let texture = SKTexture(image: logoImage!)
    let size = openFromFrame?.size
    let imageNode = SKSpriteNode(texture: texture, size: size!)
    imageNode.name = "logoNode"

    imageNode.physicsBody = SKPhysicsBody(rectangleOfSize: size!)
    imageNode.physicsBody?.dynamic = true
    imageNode.physicsBody?.restitution = 0.7
    imageNode.physicsBody?.affectedByGravity = false

    imageNode.physicsBody!.categoryBitMask = ballCategory

    let startPosition = CGPointMake(openFromFrame!.origin.x + openFromFrame!.size.width / 2, self.size.height - (openFromFrame!.origin.y + openFromFrame!.size.height / 2))

    imageNode.position  = startPosition
    self.addChild(imageNode)
}

override func didMoveToView(view: SKView) {
    self.runAnimation()
}

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

func runAnimation () {
    if let oldNote = self.childNodeWithName("logoNode") {
        oldNote.removeFromParent()
    }

    self.initLogoNode(self.logoImage)
    if let imageNode = self.childNodeWithName("logoNode") {

        let scaleValue = CGRectGetHeight(self.view!.frame) / CGRectGetHeight(imageNode.frame)

        let scale = SKAction.scaleTo(scaleValue * 0.95, duration: 0.5)

        let moveToX = SKAction.moveToX(275, duration: 0.5)
        let group = SKAction.group([scale, moveToX])

        let moveToX2 = SKAction.moveToX(225, duration: 1)
        let scale2 = SKAction.scaleTo(scaleValue * 0.8, duration: 1)
        let group2 = SKAction.group([scale2, moveToX2])

        let moveToX3 = SKAction.moveToX(230, duration: 0.5)
        let scale3 = SKAction.scaleTo(scaleValue * 0.82, duration: 0.5)
        let group3 = SKAction.group([scale3, moveToX3])

        let moveSequence = SKAction.sequence([group, group2, group3])

        imageNode.runAction(moveSequence, completion: { () -> Void in

        })
    }
}
}

0 个答案:

没有答案