读一读

创建场景资源后,可以选中场景,然后在右边的属性检查器中勾选自动释放资源,然后保存就可以在加载其他场景的时候自动把资源释放掉了。

release-resources.png


const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

   onclick()
   {
        cc.director.loadScene("123",this.onLoadFinish)
   }

   onLoadFinish()
   {
        console.log("加载123场景完成")
   }
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class SceneManager extends cc.Component {

    start () {
        //使一个根节点常驻内存
        cc.game.addPersistRootNode(this.node) 
    }

    onDestroy()
    {
        cc.game.removePersistRootNode(this.node) //取消节点常驻内存
    }
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.SpriteFrame)
    sprite:cc.SpriteFrame = null

    @property(cc.Prefab)
    prefab:cc.Prefab = null

   start(){
        let node = new cc.Node("Sprite") //创建节点
        let sp = node.addComponent(cc.Sprite) //添加组件
        sp.spriteFrame = this.sprite
        node.parent = this.node

        let node_clone = cc.instantiate(node) //克隆节点
        node_clone.parent = this.node
        node_clone.x = 100

        let pre = cc.instantiate(this.prefab) //实例化预设体
        pre.parent = this.node
        pre.y = 200

        node_clone.destroy() //节点销毁
   }
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    onLoad(){} //在节点首次激活时调用 一般用来获取其他组件
    start(){} //在第一次执行update前调用 一般用于初始化中间数据
    update(dt){} //在组件enable状态下每帧执行
    lateUpdate(){} //每帧在update执行完后执行
    onEnable(){} //组件被激活时调用
    onDisable(){} //组件被禁用时调用
    onDestroy(){} //组件被销毁时调用
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Node)
    nodeVar:cc.Node = null

    start () {
        this.node.active = false //设置激活状态
        this.node.parent = this.nodeVar //设置父节点 等价于下面两行
        this.node.removeFromParent(false)
        this.nodeVar.addChild(this.node)

        //更改位置
        this.node.x = 100
        this.node.y = 100
        this.node.setPosition(100,100)
        this.node.setPosition(cc.v2(100,100))
        this.node.position = cc.v2(100,100)

        //更改旋转
        this.node.rotation = 90
        this.node.setRotation(90)

        //更改缩放
        this.node.scaleX = 2
        this.node.scaleY = 2
        this.node.setScale(2,2)

        //更改节点尺寸
        this.node.setContentSize(100,100)
        this.node.setContentSize(cc.size(100,100))
        this.node.width = 100
        this.node.height = 100

        //更改节点锚点位置
        this.node.anchorX = 1
        this.node.anchorY = 0
        this.node.setAnchorPoint(1,0)
    }
}

cc.Class({
    extends: cc.Component,

    properties: {
        label:{type:cc.Label,default:null},//可以找属性检查器中赋值 组件和节点都可以
        text:'hello'
    },

    start () {
        this.label.string = this.text
        var a = this.node //获取到当前节点
        a.x = 100

        var can = this.getComponent(cc.Canvas)
        can = this.getComponent("Canvas")
        can = this.node.getComponent(cc.Canvas)

        var nodes = this.node.children //获取节点下的所有子节点
        console.log("数量:" + nodes.length)
        var node = this.node.getChildByName("NodeName")//根据名字查找子节点
        var node2 = cc.find("NodeName/Xixi",this.node) //在指定节点下查找子节点
        var node3 = cc.find("Canvas/NodeName") //从根节点开始全局查找
    },
});



import Helloworld from './HelloWorld'  //引入其他脚本
const LEVEL = cc.Enum({EAST:1,HARD:2}) //定义枚举
cc.Class({
    extends: cc.Component,

    properties: {

        intVar:0,//整形
        floatVar:0,//浮点数
        boolVar:false,//布尔值
        nodeVar:{
            default:null,
            type:cc.Node //节点
        },
        nodeArrVar:{
            default:[],
            type:[cc.Node]//节点数组
        },
        labelVar:{
            default:null,
            type:cc.Label//Label
        },
        prefabVar:cc.Prefab,//预设体
        ver2Var:new cc.Vec2(),
        hVar:{
            default:null,
            type:Helloworld//自定义脚本组件
        },
        enumVar:{
            default:LEVEL.EAST,
            type:LEVEL//自定义枚举
        }
    },
});



image.png