ARKIT

SpriteKit(3) - 节点

2017-07-27  本文已影响88人  li_礼光

节点的基础设置

  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    //方向取负数
        }
    }
}

小总结 :

上一篇下一篇

猜你喜欢

热点阅读