Creator动态获取,数据文,JSON并使用,枚举Enum,cc.sys.localStorage获取音效的判断 ,冒泡排序做排行榜 ,动态获取提示(cc.loader.loadRes),制作签到

本文介绍了如何在Cocos Creator中使用冒泡排序实现排行榜,动态加载和使用JSON数据,以及利用枚举和localStorage进行游戏数据管理。示例包括加载提示、动态获取JSON对象、创建签到系统等。
摘要由CSDN通过智能技术生成

冒泡排序做排行榜

cc.Class({
extends: cc.Component,

properties: {
    nameArray: {
        default: [],
        type: cc.Label
    },
    scoreArray: {
        default: [],
        type: cc.Label
    },

    scoreArrayNum: {
        default: [],
        type: cc.Integer
    },
},

// LIFE-CYCLE CALLBACKS:

// onLoad () {},

start() {
    for (let i = 0; i < 6; i++) {
        this.scoreArrayNum[i] = i * 3;
    }

  //  let arr = this.bubbleSort(this.scoreArrayNum, this.nameArray);

    for (let i = 0; i < 6; i++) {
        this.nameArray[i].string =""+ GlobalDate.date.rankingName[i];
        this.scoreArray[i].string = ""+ GlobalDate.date.rankingScoreNum[i];
      //  this.scoreArray[i].string = arr[i];
    }

 //   this.scoreArray[i].string = arr[i] = ""+ GlobalDate.date.rankingScoreNum[i];
},

// update (dt) {},

//冒泡排序
bubbleSort(arr, arrName) {
    let len = arr.length;
    for (let i = 0; i < len - 1; i++) {
        for (let j = 0; j < len - 1 - i; j++) {
            if (arr[j] < arr[j + 1]) {        //相邻元素两两对比
                let temp = arr[j + 1];        //元素交换
                arr[j + 1] = arr[j];
                arr[j] = temp;

                let _name = arrName[j + 1];
                arrName[j + 1] = arrName[j];
                arrName[j] = _name;
            }
        }
    }
    return arr;
},

btnCallBack(){
    this.node.active = false;
},

});

动态获取提示(Load实例化预制体)

showToast(text){
console.log("[sdk]showToast", text)
if(this.v_vivo != this.NONE){
window[“qg”] && qg.showToast({ message: text })
}
else{
cc.loader.loadRes(“prefab/toast”, (err, prefab) => {
if (!err) {
let node = cc.instantiate(prefab);
node.getComponent(cc.Component).setMessage(text);
cc.director.getScene().children[0].addChild(node);
}
});
}
},
this.showToast(“暂无视频!!!”);
showToast(text){
cc.loader.loadRes(“prefabs/toast”, (err, prefab) => {
if (!err) {
let node = cc.instantiate(prefab);
node.getComponent(“setMessage”).setMessage(text);
cc.director.getScene().children[0].parent.addChild(node);
// console.log(“父物体”,cc.director.getScene().children[0].parent.name);
// console.log(“子物体”,cc.director.getScene().children[0].name);

            }
        });
}

在预制体脚本里面写上
TextLabel: {
default: null,
type: cc.Label,
},
setMessage(text){
this.TextLabel.string = text;
console.log("挑战 this.TextLabel.string = ",this.TextLabel.string);
this.scheduleOnce(()=>{
this.node.removeFromParent();
this.node.destroy();
},1);

}

.json可以理解为cc.Enum枚举类型的对象升级模式

// ******** 动态加载并使用 .json文件中的对象 **********************//

游戏 《口袋之旅程》
在这里插入图片描述
cc.loader.loadRes(‘mapNpc’, function (err, object) {
if (err) {
console.log(“mapNpc…err:”, err);
return;
}
else {
let fruit = object.json.npc1;
global.jesonNpc = fruit;
}
});
cc.loader.loadRes(‘mapLevel’, function (err, object) {
if (err) {
console.log(“mapLevel…err”, err);
return;
}
else {
let fruit = object.json.npc1;
global.jesonLevel = fruit;

        }
    });

if (this.mapState == 0)
npcJeson = global.jesonNpc;
else
npcJeson = global.jesonNpc_2;

    for (let i = 0; i < npcJeson.length; i++)//生成NPC
    {
        let type = npcJeson[i].type;
        let x = npcJeson[i].x;
        let y = npcJeson[i].y;
        let scale = npcJeson[i].scale;
        let moveLength = npcJeson[i].moveLength;
        let v = npcJeson[i].v;
        // if (y > createTimes && y < createTimes + 20)
        if (this.repeatTimes > 0) {
            if (y + this.repeatTimes * 2000 == createTimes)
                this.createNpc(type, x, y + this.repeatTimes * 2000, scale, moveLength, v);
        }
        else {
            if (y == createTimes)
                this.createNpc(type, x, y + this.repeatTimes * 2000, scale, moveLength, v);
        }

    }

在这里插入图片描述

//*************************** TypeScript枚举 ********************************//

export enum LayerOrder {
Normal = 1,
Model = 2,
Tip = 4,
Guide = 8,
Debug = 16,
System = 32,
Scene = 64,
};
// Tip设置为1是与编辑器中的group 1对应的。
export enum LayerGroupType {
Default = 0,
Scene = 0,
Normal = 0,
Model = 0,
Tip = 1,
Guide = 0,
Debug = 0,
System = 0,
}
export enum ScrollViewEvent {
DEFAULT = 0,
TO_TOP_LEFT,
TO_BOTTOM_RIGHT,
SCROLLING,
SCROLL_START,
SCROLL_END,
BOUNCE_TOP_LEFT,
BOUNCE_BOTTOM_RIGHT,
}

//*************************** 模块化javaScript枚举 ********************************//

module.exports = {

TouchType : cc.Enum({
    DEFAULT: 0,
    FOLLOW: 1,
}),

DirectionType : cc.Enum({
    FOUR: 4,
    EIGHT: 8,
    ALL: 0,
}),

};

//**********************制作签到 **********************************//

在另一个 cocosCreator的常用API与方法 文章中

二维动画
DragonBones 简称DB 优化好,效率高 DragonBones 龙形骨骼

Spine要好, 功能也跟强大一些 Spine Skeleton 脊椎骨骼
getAttachment (获取依赖项 )通过 (slot插槽位置 )和 (attachment附件依赖项) 的名称获取 attachment

// ********************* cc.sys.localStorage **********************//

取值的时候 加入 if(userdate != “” &&userdate != null && userdate != undefined)的判断很重要

jiazaiDate() {
    if (cc.sys.localStorage.getItem("havedate") == null) {
        cc.sys.localStorage.setItem('havedate', 1);
        var _date = JSON.stringify(cc.GlobalDate.date);
        cc.sys.localStorage.setItem('date', _date)
        console.log("userdate第一次",_date);
    } else {
        ~~var userdate = cc.sys.localStorage.getItem('date');~~ 
        console.log("userdate第n次"+userdate);    
        
        ~~if(userdate  != "" &&userdate != null && userdate != undefined){~~ 
        
            cc.GlobalDate.date = JSON.parse(userdate);
        }        
        
    }
    cc.director.loadScene("FirstScene");
},

//取出的时候用JSON.parse转换后再使用

var _date = JSON.stringify(cc.GlobalDate.date);
cc.sys.localStorage.setItem(“date”, _date)
//存储的时候用JSON.stringify转换后再存储

一般先游戏中 可以用 if (cc.sys.localStorage.getItem(“havedate”) == null) {
cc.sys.localStorage.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值