SpriteKit与arkit结合炸弹小游戏

Spritekit介绍

http://www.jianshu.com/p/0427dc183ca8

SKNode & SKScene
属性:
Position: node 定位在坐标系中的位置
xScale & yScale 尺寸的缩放
zRotate: 旋转节点
Frame: 节点包含的矩形
Accumulated: 积累
Alpha: 透明度
isHidden: 是否可见
ignoresSiblingOrder:判別相同位置的两个 node 他们是否并存 默认是不并存的 false 如果改成 true 则都可以看到,那么假设说在 true 情況下,我们想要遮住他們的話,可以修改他们的 zPosition 数字比較大的会在上面


SKACtion:

runAction: 执行动作
runActionWithKey: 执行动作并且分配一个 key
runActionComlietion: 添加完成处理程序

集体的动作
Sequence: 子操作在另一個操作之后运行
Group: 子操作跟他同时进行
Repeat: 子操作运行行一定或是无线的循环操作
—————————-
hasAction: 当物件本身有操作的時候,我才进行子操作


PhysicsWorld:
将重力定义CGVector
SKPhyicsBody: 基于我们的物件所具有质量和提及的动态体


myTextureSpriteNode.physicsBody?.allowsRotation = true//是否改变重力冲击的方向
myTextureSpriteNode.physicsBody?.restitution = 1.0
// restitution 每次的碰撞中损失了多少动力,默认 0.2

游戏介绍

点击炸弹,炸弹消失,超过5s没有点击 ,发生爆炸

效果图

点击消失

超过5s爆炸

新建项目

新建项目选择game
这里写图片描述
选择spritekit
这里写图片描述

设置追踪镜头

  let configuration = AROrientationTrackingConfiguration()//追踪镜头的位置,只有三个自由度 速度比较快

编写核心Scene

代码很简单,看下就明白了
“`
//
// Scene.swift
// SpriteKitInAr
//
// Created by Liyanjun on 2017/10/10.
// Copyright © 2017年 liyanjun. All rights reserved.
//

import SpriteKit
import ARKit

class Scene: SKScene {

var playing = false//是否在play

//计时器
var timer = Timer()

//分数
var score = 0

override func didMove(to view: SKView) {
    // Setup your scene here
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}

func displayMenu(){
    /**
     SKLabelNode: 一种 label 來使用的 node
     name???
     */

    let logoLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
    logoLabel.fontSize = 50.0

    logoLabel.text = "Game Over!"
    logoLabel.verticalAlignmentMode = .center
    logoLabel.horizontalAlignmentMode = .center

    logoLabel.position = CGPoint(x: frame.midX, y: frame.midY + logoLabel.frame.size.height)

    logoLabel.name = "Menu"
    self.addChild(logoLabel)

    let infoLabel = SKLabelNode(fontNamed: "AvenirNext-Bold")
    infoLabel.fontSize = 50.0

    infoLabel.text = "你被炸飞了"
    infoLabel.verticalAlignmentMode = .center
    infoLabel.horizontalAlignmentMode = .center

    infoLabel.position = CGPoint(x: frame.midX, y: frame.midY - infoLabel.frame.size.height)

    infoLabel.name = "Menu"
    self.addChild(infoLabel)

    //      最高分
    let higthtScore = SKLabelNode(fontNamed: "AvenirNext-Bold")
    higthtScore.fontSize = 50.0

    higthtScore.text = "最高分:\(UserDefaults.standard.integer(forKey: "HighestScore"))"
    higthtScore.verticalAlignmentMode = .center
    higthtScore.horizontalAlignmentMode = .center

    higthtScore.position = CGPoint(x: frame.midX, y: infoLabel.frame.midY - higthtScore.frame.size.height * 2 )

    higthtScore.name = "Menu1" //??
    self.addChild(higthtScore)


    //点击屏幕从新开始
    let beginAgain = SKLabelNode(fontNamed: "AvenirNext-Bold")
    beginAgain.fontSize = 30.0
    beginAgain.text = "点击屏幕从新开始"
    beginAgain.verticalAlignmentMode = .center
    beginAgain.horizontalAlignmentMode = .center

    beginAgain.position = CGPoint(x: frame.midX, y: higthtScore.frame.midY - beginAgain.frame.size.height*2)

    beginAgain.name = "Menu1"
    self.addChild(beginAgain)
}

//添加炸弹

func addBomd()  {

    guard let sceneView = self.view as? ARSKView else {
                    return
    }

    //判断镜头的位置
    if let currentFrame = sceneView.session.currentFrame {

        let xOffset = Float(arc4random_uniform(UInt32(10)))/10 - 1.5

        let zOffset = Float(arc4random_uniform(UInt32(30)))/10 + 0.5

        var transFrame = matrix_identity_float4x4

        transFrame.columns.3.x = currentFrame.camera.transform.columns.3.x - xOffset
        transFrame.columns.3.z = currentFrame.camera.transform.columns.3.z - zOffset
        transFrame.columns.3.y = currentFrame.camera.transform.columns.3.y

        let archor = ARAnchor(transform: transFrame)


        sceneView.session.add(anchor: archor)

    }

    timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(explode), userInfo: nil, repeats: false)

}

@objc func explode(){

    //游戏结束

    timer.invalidate()//暂停计时器

    if UserDefaults.standard.integer(forKey: "HighestScore") < score {

        UserDefaults.standard.set(score, forKey: "HighestScore")
    }

    //数一下有是多少炸弹
    for node in children{
        if let node = node as? SKLabelNode, node.name == "Bomb"{
           node.text = "
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值