[Swift] 등속 직선 동작(SpriteKit 사용)

4211 단어 XcodeSwiftSpriteKit

환경은 Xcode6-Beta4입니다.
Game에서 새 항목을 만들고 Game Technology에서 SpriteKit를 선택합니다.
GameScene.swift에 다음 내용을 쓰면 완성됩니다.
GameScene.swift

import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {}

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        for touch in touches {

            let location = touch.locationInNode(self)

            // 赤い正方形を作成
            let square = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(60, 60))

            square.position = location
            square.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(60, 60))

            // 速度を設定
            square.physicsBody.velocity = CGVectorMake(0,200)

            // 重力を無視する
            square.physicsBody.affectedByGravity = false

            // 空気抵抗を無視する
            square.physicsBody.linearDamping = 0

            self.addChild(square)
        }
    }
    override func update(currentTime: CFTimeInterval) {}
}

좋은 웹페이지 즐겨찾기