SpriteKit(3) - 节点

节点的基础设置

  func createNode() {
        let node = SKSpriteNode(color: UIColor.red, size: CGSize(width: 800, height: 800))
        node.name = "node"
        node.color = UIColor.red                                //填充颜色
        node.texture = SKTexture(imageNamed: "180x180.png")     //纹理图片
        node.colorBlendFactor = 0.1                             //渲染银子
        node.size = CGSize(width: 400, height: 400)             //节点尺寸
        node.setScale(2)                                        //节点缩放
        node.xScale = 2                                         //节点x缩放
        node.zRotation = CGFloat.pi * 2                         //节点旋转
        node.alpha = 1                                          //节点透明度
        node.isHidden = false                                   //节点隐藏
        node.normalTexture = node.texture?.generatingNormalMap()//法线纹理
        node.lightingBitMask = 1;                               //设置光源种类
        self.addChild(node)
        
        //创建光源
        let lightSprite = SKLightNode()
        lightSprite.position = CGPoint(x: 400, y: 400)
        lightSprite.name = "light"
        lightSprite.categoryBitMask = 1                         //光源类型
        lightSprite.lightColor = UIColor.white                  //光源颜色
        lightSprite.isEnabled = true                            //开启光源
        lightSprite.ambientColor = self.backgroundColor         //环境光颜色
        lightSprite.falloff = 1                                 //光照衰减速率
        self.addChild(lightSprite)
        
        //删除节点操作
//        node.removeFromParent()
//        removeAllChildren()
        
    }

节点移动

节点的移动根据主要通过update的方法来实现,刷新帧


import SpriteKit
import GameplayKit
class GameScene: SKScene {

    let sprite : SKSpriteNode = SKSpriteNode(imageNamed: "180x180.png")
    
    var lastUpdataTime : TimeInterval = 0           //记录上一次更新时间
    var dt : TimeInterval = 0                       //时间间距
    let spriteMovePointsPerSec : CGFloat = 480      //每秒钟移动的点(距离)
    var velocity  = CGPoint.zero                    //速度(由spriteMovePointsPerSec控制)
    
    
    
    override func didMove(to view: SKView) {
        self.backgroundColor = UIColor.gray
        //设置srpite
        sprite.position = CGPoint(x: 400, y: 400)
        sprite.setScale(0.8)
        self.addChild(sprite)
    }
    
    
    
    //传入一个节点和一个坐标点
    func move(sprite : SKSpriteNode , velocity : CGPoint)  {
        let amountToMove = CGPoint(x: velocity.x * CGFloat(dt), y: 0)       //偏移量
        sprite.position = CGPoint(x: sprite.position.x + amountToMove.x,    //设置精灵的位置
                                  y: sprite.position.y)
    }
    func moveToward(location : CGPoint) {
        let offset = CGPoint(x: location.x - sprite.position.x,y: 0)        //偏移量
        let length = sqrt(Double(offset.x * offset.x))                      //长度
        let direction = CGPoint(x: offset.x / CGFloat(length), y: 0)        //方向
        velocity = CGPoint(x: direction.x * spriteMovePointsPerSec, y: 0)   //速度
    }
    

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let myTouch = touches as NSSet
        let touch = myTouch.anyObject() as! UITouch
        let touchLocation = touch.location(in: self)    //获取触摸点(Scene本身也是一个node)
        
        moveToward(location: touchLocation)             //传入当前点击的点
    }

    override func update(_ currentTime: TimeInterval) {
        if lastUpdataTime > 0 {
            dt = currentTime - lastUpdataTime
        }else{
            dt = 0
        }
        lastUpdataTime = currentTime
        move(sprite: sprite, velocity: velocity)
        boundsCheck()
    }
    
    func boundsCheck()  {
        if sprite.position.x <= -(self.size.width * 0.5) {
            sprite.position.x = -(self.size.width * 0.5)
            velocity.x = -velocity.x    //方向取负数
        }
        if sprite.position.x >= (self.size.width * 0.5) {
            sprite.position.x = self.size.width * 0.5
            velocity.x = -velocity.x    //方向取负数
        }
    }
}

小总结 :

  • 要控制一个节点移动,通过update来刷新节点的位置.
  • 通过逻辑去操作节点的位置.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 12,823评论 6 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,967评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,951评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 16,467评论 4 61
  • 山人 1989.10.9 浅灰色的天幕里, 阳台上斜倚着一位女孩。 她那假装严肃的面孔, 不能掩饰眼睛...
    闲淡山人阅读 318评论 0 3

友情链接更多精彩内容