swift 快速奔跑的兔几 本节的内容是:SpriteKit第二讲

前端之家收集整理的这篇文章主要介绍了swift 快速奔跑的兔几 本节的内容是:SpriteKit第二讲前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

补充一下,Sprite是精灵的意思喔~是不是感觉萌萌哒~~~~~
SpriteKit节点:
不同的节点做不同的事情:
SKSpriteNode:显示一个图像或者一个彩色矩形。
SKLabelNode:显示文本
SKShapeNode:显示任意UIBezierPath
SKEffectNode:向其所有子节点应用图像特效,比如模糊或者色移。
空的SKNode对象,可以将多个节点划分为一组。

下面是使用SKSpriteNode显示一个小黄人图片的栗子:

import UIKit
import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        // 当屏幕发生变化时的效果
        self.scaleMode = .AspectFill

        let label = SKLabelNode(text: "I am littleYellowMan")
        label.position = CGPoint(x: size.width/2.0,y: size.height/2.0)

        let spriteNode1 = SKSpriteNode(imageNamed: "littleYellowMan.jpg")
        spriteNode1.position = CGPoint(x: size.width/2.0,y: size.height/3.5)
        spriteNode1.size = CGSize(width: size.width,height: size.height/3)

        self.addChild(spriteNode1)
        self.addChild(label)
    }
}
import UIKit
import SpriteKit

class spriteKitViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = GameScene(size:self.view.bounds.size)
        scene.backgroundColor = UIColor.grayColor()
        scene.scaleMode = .AspectFill
        let skView = self.view as! SKView
        //let transitionOne = SKTransition.moveInWithDirection(SKTransitionDirection.Right,duration: 0.5)
        skView.presentScene(scene)
    }
}

ok,下面要来试一试接受点击事件:并且做场景切换,这一步需要3个文件咯,那个func touchesBegan负责处理点击事件咯~

import UIKit
import SpriteKit

class MenuScene: SKScene {
    override func didMoveToView(view: SKView) {
        let lableOfMenu = SKLabelNode(text: "welcome to YellowMoon!")
        lableOfMenu.fontSize = 20
        lableOfMenu.position = CGPoint(x: size.width/2.0,y: size.height/2.0)

        self.addChild(lableOfMenu)
    }

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        let showSprite = GameScene(size:self.size)

        let transitionFromHere = SKTransition.moveInWithDirection(SKTransitionDirection.Right,duration: 0.5)

        self.view?.presentScene(showSprite,transition: transitionFromHere)
    }
}
import UIKit
import SpriteKit

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        // 当屏幕发生变化时的效果
        self.scaleMode = .AspectFill

        let label = SKLabelNode(text: "I am littleYellowMan")
        label.position = CGPoint(x: size.width/2.0,height: size.height/3)

        self.addChild(spriteNode1)
        self.addChild(label)
    }
}
import UIKit
import SpriteKit

class spriteKitViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = MenuScene(size:self.view.bounds.size)
        scene.backgroundColor = UIColor.grayColor()
        scene.scaleMode = .AspectFill
        let skView = self.view as! SKView
        //let transitionOne = SKTransition.moveInWithDirection(SKTransitionDirection.Right,duration: 0.5)
        skView.presentScene(scene)
    }
}

猜你在找的Swift相关文章