cocos creator 编程基础

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013862108/article/details/82982885

官网文档永远是首先想到的第一手文档资料

脚本中怎么获取子节点?

使用this.node.getChildByName

this.node.getChildByName('tip');

怎么使精灵隐藏或显示?

this.node.getChildByName('tip').active = false;      //false 就是隐藏,true就是显示

怎么加载远程图片,并设置为精灵的sprite frame?

注意要做一下错误处理

            //加载logo 
            cc.loader.load({url: logo_url, type:"png"},function(err, texture){
                if(err){
                    console.log("logo图片下载失败", logo_url, err);
                    return;
                }
                gamedata.logoimgFrame =   new cc.SpriteFrame(texture);
                self.node.getChildByName('logo').getChildByName('img').getComponent(cc.Sprite).spriteFrame = gamedata.logoimgFrame;
            });

怎么为button按钮类型的精灵设置点击事件?

this.node.getChildByName('tip').getChildByName('kickBtn').on('click',this.kickBtnCallback,this);

怎么获取当前执行的是那个脚本? 除了使用this,或说在this不好使的情况下

cc.director.getScene().getChildByName('Canvas').getComponent('enroll').count = 5;  //Canvas是根结点名称,"enroll"是js脚本名称; count 是脚本中的一个变量

怎么设置一个精灵的zorder值?

this.node.getChildByName('outTip').setLocalZOrder(999);

怎么获取屏幕的宽高? 怎么获取屏幕的尺寸?

var winSize = cc.director.getWinSize();

var jiaodu = Math.atan(winSize.height/winSize.width) * 180 /Math.PI;

怎么设置精灵的旋转角度?

this.flyer.setRotation(90-jiaodu);

怎么设置播放音乐的音量大小?

cc.audioEngine.play(this.flyAudio, false, 49);         //第三个参数为音量倍数 49倍

怎么实时改变动画的频率 或动画的播放速度?

var anim = gamedata.ingScene[i].getComponent(cc.Animation);

var animState = anim.getAnimationState(anim_name);

//计算速度系数 todo

var speed = this.getSpeed(i);

animState.speed = speed;

怎么根据对象的某个属性进行排序?

sortedData.sort(this.compare("score",'desc'));

    //定义一个比较器
    compare : function (propertyName,order = 'asc') {  //降序desc
        return function(object1, object2) {
            var value1 = object1[propertyName];
            var value2 = object2[propertyName];
            if (value2 < value1) {
                if(order == 'asc'){
                    return 1;
                }else{
                    return -1;
                }
            } else if (value2 > value1) {
                if(order == 'asc'){
                    return -1;
                }else{
                    return 1;
                }
            } else {
                return 0;
            }
        }
    },

怎么加载场景?

cc.director.loadScene("enroll");

点击一个按钮跳转到一个html页面怎么实现?

核心 还是 window.location.href

   var prize_btn = cc.instantiate(rankNode.hasprizePrefab);
            prize_btn.on('click', function(){
                window.location.href = window.couponUrl + "?uniqueBid="+gamedata.playerInfo.uniqueBid+"&no";

            }, this);
            rankNode.node.addChild(prize_btn);

猜你喜欢

转载自blog.csdn.net/u013862108/article/details/82982885