cocos creator全局变量与常住节点

1, 使用 通过模块访问

创建一个helloworld项目。

创建一个脚本,来保存变量,Common.js

module.exports = {

data : null

};

在helloworld场景所绑定的脚本文件开头引用common脚本

var com = require('Common');

在脚本的onload函数里面访问Common脚本变量。并赋值

    var com = require('Common');
    cc.Class({
        extends: cc.Component,
     
        properties: {
            label: {
                default: null,
                type: cc.Label
            },
            text: 'Hello, World!'
        },
     
        // use this for initialization
        onLoad: function () {
            this.label.string = this.text;
            com.data = 50;
        },
        
        //跳转场景
        setscene : function(){
          cc.director.loadScene('demo');  
        },
     
        // called every frame
        update: function (dt) {
     
        },
    });


新建一个场景,demo,并新建一个脚本文件。demo.js。并绑定脚本给场景

同理,在demoScene脚本中引用Common脚本。并且打印在上一个场景所传入的值

    var com = require('Common');
    cc.Class({
        extends: cc.Component,
     
        properties: {
            // foo: {
            //    default: null,
            //    url: cc.Texture2D,  // optional, default is typeof default
            //    serializable: true, // optional, default is true
            //    visible: true,      // optional, default is true
            //    displayName: 'Foo', // optional
            //    readonly: false,    // optional, default is false
            // },
            // ...
        },
     
        // use this for initialization
        onLoad: function () {
            cc.log('data的值是'+ com.data)
        },
     
        // called every frame, uncomment this function to activate update callback
        // update: function (dt) {
     
        // },
    });

这是全局变量的一个使用方法

还有一种是设置常驻节点

在helloworld场景的层级管理中,新建节点,我是创建一个根节点


并且给该节点绑定一个脚本,

这个脚本的内容:

    cc.Class({
        extends: cc.Component,
     
        properties: {
     
        },
     
        // use this for initialization
        onLoad: function () {
            cc.game.addPersistRootNode(this.node);
        },
        //自定义的两个函数。将值保存在this变量里
        setdata : function(json){
            this.data = json;  
        },
        getdata : function(){
            return this.data;  
        },
    });

 

helloworld.js

    var com = require('Common');
    cc.Class({
        extends: cc.Component,
     
        properties: {
            label: {
                default: null,
                type: cc.Label
            },
            text: 'Hello, World!'
        },
     
        // use this for initialization
        onLoad: function () {
            this.label.string = this.text;
            com.data = 50;
            //获取常驻节点所绑定的脚本
            var node = cc.find('New Node').getComponent('node');
            //调用该脚本的函数并传值
            node.setdata('这是常驻节点');
        },
        
        
        //跳转场景
        setscene : function(){
          cc.director.loadScene('demo');  
        },
     
        // called every frame
        update: function (dt) {
     
        },
    });

第二个场景,demoScene.js

    <pre name="code" class="javascript">var com = require('Common');
    cc.Class({
        extends: cc.Component,
     
        properties: {
     
        },
     
        // use this for initialization
        onLoad: function () {
            cc.log('data的值是'+ com.data);
            
            //获取常驻节点
            var node = cc.director.getScene().getChildByName('New Node');
            //获取节点的node脚本组件,并调用脚本里面的函数
            var data = node.getComponent('node').getdata();
            cc.log('常驻节点的data值为'+data);
        },
     
    });


运行结果,场景条转后,值还是存在的。


---------------------
作者:阿启
来源:CSDN
原文:https://blog.csdn.net/u010971754/article/details/51815325
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值