zoukankan      html  css  js  c++  java
  • Swift游戏实战-跑酷熊猫 09 移除场景之外的平台

    上一节,我们写出了一个疯狂产生平台的东西。所谓上帝欲使其灭亡,必先使其疯狂。所以太疯狂都不是什么好事,所以我们要采取一些措施,例如移除场景之外的平台。btw如果哪天你觉得自己的老板行为乖张,难以理喻。例如明明没什么事做还要没事找事让你疯狂加班,这时候就要小心,小心……哈哈,扯远了。

    要点:

    如何判断平台移除场景:

    由于我们的平台是一个接一个的有顺序的产生,所以每次我们只要判断数组第一个平台也就是下标为0的元素是否移除场景就够了。怎么判断移除场景呢?由于我们的平台的锚点是在最左边,所以只要判断平台的坐标是否小于平台的宽度的负值即可。这一切都在move方法中进行。

    if platforms[0].position.x < -platforms[0].width {
            platforms[0].removeFromParent()
            platforms.removeAtIndex(0)
    }

    这时候我们的平台工厂类的完整代码应该是这样,重要代码已加粗加大

    import SPriteKit
    
    class PlatformFactory:SKNode{
        let textureLeft = SKTexture(imageNamed: "platform_l")
        let textureMid = SKTexture(imageNamed: "platform_m")
        let textureRight = SKTexture(imageNamed: "platform_r")
    
        var platforms = [Platform]()
        var sceneWidth : CGFloat = 0
        var delegate:ProtocolMainScene?
    
        func createPlatformRandom(){
            let midNum:UInt32 = arc4random()%4 + 1
            let gap:CGFloat = CGFloat(arc4random()%8 + 1)
            let x:CGFloat = self.sceneWidth + CGFloat(midNum*50) + gap + 100
            let y:CGFloat = CGFloat(arc4random()%200+200)
            createPlatform(midNum, x: x, y: y)
        }
    
        func createPlatform(midNum:UInt32,x:CGFloat,y:CGFloat){
            let platform = Platform()
            platform.position = CGPointMake(x, y)
    
            let platform_left = SKSpriteNode(texture: textureLeft)
            platform_left.anchorPoint = CGPointMake(0, 0.9)
           
    
            let platform_right = SKSpriteNode(texture: textureRight)
            platform_right.anchorPoint = CGPointMake(0, 0.9)
           
    
            var arrPlatform = [SKSpriteNode]()
    
            arrPlatform.append(platform_left)
           
           
            for i in 1...midNum {
                let platform_mid = SKSpriteNode(texture: textureMid)
                platform_mid.anchorPoint = CGPointMake(0, 0.9)
                arrPlatform.append(platform_mid)
            }
    
            arrPlatform.append(platform_right)
           
            platform.onCreate(arrPlatform)
           
            self.addChild(platform)
           
            platforms.append(platform)
            //通用公式:生成的平台的长度 + 平台的x坐标 - 主场景的宽度
            delegate?.onGetData(platform.width + x - sceneWidth)
           
        }
    
        func move(speed:CGFloat){
            for p in platforms {
                p.position.x -= speed
            }
            if platforms[0].position.x < -platforms[0].width {
                platforms[0].removeFromParent()
                platforms.removeAtIndex(0)
            }
        }
    }

    项目文件地址

    http://yun.baidu.com/share/link?shareid=3824235955&uk=541995622

    Swift游戏实战-跑酷熊猫系列

    00 游戏预览

    01 创建工程导入素材

    02 创建熊猫类

    03 熊猫跑动动画

    04 熊猫的跳和滚的动作

    05 踩踏平台是怎么炼成的

    06 创建平台类以及平台工厂类

    07 平台的移动

    08 产生源源不断的移动平台

  • 相关阅读:
    BIO与NIO、AIO的区别
    Java虚拟机
    PV模型
    HashMap、HashSet源代码分析其 Hash 存储机制
    单节点到分布式集群
    Oracle表分区
    ZooKeeper原理
    oracle中的 exists 和 in 的效率问题
    OQL对象查询语言
    keepalived openssl 报错
  • 原文地址:https://www.cnblogs.com/sandal1980/p/3872422.html
Copyright © 2011-2022 走看看