LoginSignup
16
14

More than 5 years have passed since last update.

swiftでspritekit

Posted at

swiftのスプライトキットのサンプルになれば。
やってること
・イメージを取り込んで、物理特性を持たせる
・ボタンクリックでイメージをジャンプさせる

ほとんどobjective-cと変わらないですね。

import SpriteKit

class GameScene: SKScene {

    let Jump : CGFloat = 400.0
    let imageWidth : CGFloat = 30.0
    let FloarWidth : CGSize = CGSizeMake(500, 500)

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        let iorin = SKSpriteNode(imageNamed:"minase-iori0020")

        iorin.xScale = 0.1
        iorin.yScale = 0.1
        iorin.position = CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMidY(self.frame));
        iorin.name = "iori"

        let body = SKPhysicsBody(circleOfRadius:imageWidth)
        iorin.physicsBody = body

        self.addChild(iorin)

        let floor = SKSpriteNode(imageNamed:"minase-iori0020")
        floor.xScale = 0.3
        floor.yScale = 0.3
        floor.position = CGPoint(x:CGRectGetMidX(self.frame),y:CGRectGetMinY(self.frame));
        floor.name = "floor"

        let body2 = SKPhysicsBody(rectangleOfSize:FloarWidth)
        body2.affectedByGravity = false
        body2.dynamic = false
        body2.contactTestBitMask = 1
        floor.physicsBody = body2

        self.addChild(floor)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        self.childNodeWithName("iori").physicsBody.velocity.dy = Jump
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        self.childNodeWithName("floor").position =CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMinY(self.frame));
    }
}
16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14