Code Monkey home page Code Monkey logo

cameraendlesswithspritekittutorial's Introduction

A Tutorial For How To Use SpriteKit Camera Making Endless Background

效果

Player运用Camera节点向前移动的效果

向前舞动

命为SpriteNode为player

player的Custom Class 为自定义Node

拖动Camera 进场景中,命名为mainCamera

相机Camera的Position(0,0) ,Zposition为1

设置Scene的Camera为mainCamera

camera的节点移动到2048(self.size.width)的时候,把红色框内的节点移动到最右边( node.position.x += self.size.width * SCENE_NUMBERS)
 /// 查找所有命名为ground的精灵节点
        enumerateChildNodes(withName: "//ground") { (node, _ ) in
            /// 如果当前的节点 + scene.size.with <  则移动节点
            if node.position.x + self.size.width < camera.position.x {
                node.position.x += self.size.width * SCENE_NUMBERS /// 更新节点的位置
            }
        }

完整的代码如下:

//  GameScene.swift
//  CameraEndless
//
//  Created by www.iFIERO.com on 2018/6/24.
//  Copyright © 2018 iFiero. All rights reserved.
//

import SpriteKit
import GameplayKit

public let CAMERA_MOVE_XPOS:CGFloat   = 12  /// 相机X-Axis移动的尺寸;
public let SCENE_NUMBERS:CGFloat = 2.0      /// 有几个场景scene

class GameScene: SKScene {
    
    private var player = PlayerClass()  /// 初始化自定义节点
    private var mainCamera:SKCameraNode!
    private var ground:SKSpriteNode! /// 地板
    private var bg:SKSpriteNode!    /// 背景
    private var cloud:SKSpriteNode! /// 云 cloud
    
    override func didMove(to view: SKView) {
        
        initUI()     /// 初始化Scene里的各个精灵节点
        initCamera() /// 初始化相机节点;
    }
    
    func initUI(){
        /// player名称为GameScene.sks 上自行命名的名称
        player = childNode(withName: "player") as! PlayerClass
        player.initPlayer() /// 开始Wobbing
        ground = childNode(withName: "ground") as! SKSpriteNode
        bg     = childNode(withName: "bg") as! SKSpriteNode
        cloud  = childNode(withName: "cloud") as! SKSpriteNode
    }
    
    func initCamera(){
        mainCamera = childNode(withName: "mainCamera") as! SKCameraNode
    }
    
    /// 移动节点
    func moveSprites(camera:SKCameraNode){
        /// 查找所有命名为ground的精灵节点
        enumerateChildNodes(withName: "//ground") { (node, _ ) in
            /// 如果当前的节点 + scene.size.with <  则移动节点
            if node.position.x + self.size.width < camera.position.x {
                node.position.x += self.size.width * SCENE_NUMBERS /// 更新节点的位置
            }
        }
        /// 查找bg
        enumerateChildNodes(withName: "//bg") { (node, _) in
            if node.position.x + self.size.width < camera.position.x {
                node.position.x += self.size.width * SCENE_NUMBERS
            }
        }
        ///查找所有云
        enumerateChildNodes(withName: "cloud") { (node, _) in
            if node.position.x + self.size.width < camera.position.x {
                node.position.x += self.size.width * SCENE_NUMBERS 
            }
        }
    }
    
    override func update(_ currentTime: TimeInterval) {
        mainCamera.position.x += CAMERA_MOVE_XPOS /// 向前移动;
        player.position.x += CAMERA_MOVE_XPOS     /// player向右移动的速度和camera的速度一致
        moveSprites(camera: mainCamera)           /// 传入相机节点
    }
}

更多教程:http://www.iFIERO.com

cameraendlesswithspritekittutorial's People

Contributors

apiapia avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

yunzixun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.