簡體   English   中英

落球動畫不起作用(Swift)

[英]Ball dropping animation not working (Swift)

該代碼應該將球從屏幕頂部放到底部。 並且,一旦觸摸到屏幕底部,它就應該回到屏幕頂部。 它不會移到頂部,並且會停止移動。 我希望它是一個連續的循環,每次觸到底部時都會重置ball.y位置。

import SpriteKit

class GameScene: SKScene {
    let ball = SKShapeNode(circleOfRadius: 20)
    let label = SKLabelNode(fontNamed: "Futura")
    let movingObjects = SKSpriteNode()

    override func didMoveToView(view: SKView) {
        let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = sceneBody

        //Ball Transition
        let ballTransition = SKAction.sequence([SKAction.fadeInWithDuration(1)])
        ball.runAction(ballTransition)

        //Ball function
        ball.fillColor = SKColor.redColor()

        ball.physicsBody = SKPhysicsBody(circleOfRadius: 25)
        ball.physicsBody?.affectedByGravity = false

        //Ball Movement
        ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))

        ballMovement()

        movingObjects.addChild(ball)

        self.addChild(label)
    }

    func ballMovement() {
        let moveBall = SKAction.moveToY(self.frame.size.height*0, duration: 3)
        let removeBall = SKAction.removeFromParent()
        let moveAndRemove = SKAction.sequence([moveBall, removeBall])
        ball.runAction(moveAndRemove)

        //Label Sprite
        label.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        label.fontColor = SKColor.redColor()
        label.fontSize = 30
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        label.text = "\(ball.position.y)"

        if ball.position.y < 26 {
            ball.position = CGPoint(x: self.frame.size.width/2, y: CGFloat(self.frame.size.height*1))
        }
    }
}

隨着程序變得越來越復雜,在動作中將球從其父對象中移除並仍然在其他位置對其進行操作將變得非常脆弱。 為什么不只用動作來做所有事情,而讓Sprite Kit擔心呢?

 let moveBall = SKAction.moveToY(0, duration: 3)
 let goBackUp = SKAction.moveToY(self.frame.size.height, duration:0)
 let keepFalling = SKAction.sequence([moveBall, goBackUp])
 ball.runAction(SKAction.repeatActionForever(keepFalling))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM