Cocos Creator--1.介绍
目录
前言
这里我主要是写代码,以及小游戏的制作,以及一个代码库的收集。
一、简介
看官网!!!
Cocos Creator API
二、第一阶段问题
1.加载和切换场景(场景加载回调)
onbtn(){
// cc.director.loadScene("Main")//跳转到游戏场景
cc.director.loadScene("Main", this.onSceneLaunched);
}
onSceneLaunched(){
console.log("这是回调函数")
}
2.音频播放(三种)
2.1.使用 AudioSource 组件播放
代码如下(示例):
2.2.通过脚本控制 AudioSource 组件
tanren:cc.AudioSource=null;//背景音乐
this.tanren= this.getComponent(cc.AudioSource);
this.tanren.play();//播放
this.tanren.pause();//结束
2.3.通过脚本控制 AudioSource 组件(官方推荐的)
@property(cc.AudioClip)
tanren1:cc.AudioClip=null;//背景音乐
current: number;
this.current=cc.audioEngine.play(this.tanren1,false,1);//播放
cc.audioEngine.pause(this.current);//结束
cc.audioEngine.stop(this.current);//结束
3.访问节点和组件
var node = this.node;//当前所在节点
var label = this.getComponent(cc.Label);//当前所在节点的其他组件
//如果在节点上找不到你要的组件,getComponent 将返回 null,
var label = this.getComponent(cc.Label);
if (label) {
label.string = "Hello";
}
else {
cc.error("Something wrong?");
}//当一个预防措施
var rotate = this.getComponent("SinRotate");//获取其他脚本
this.backNode = cc.find("Canvas/Menu/Back");//全局查找
4.生命周期
5.数据储存cc.sys.localStorage
//cc.sys.localStorage API
cc.sys.localStorage.setItem(key,value); //key 为字符串类型,value为数值或字符串
cc.sys.localStorage.getItem(key); //返回值为数值或者字符串。若数据不存在,则返回null
cc.sys.localStorage.removeItem(key); //根据键值删值
cc.sys.localStorage.clear(); //清空所有通过cc.sys.localStorage保存的数据
6.图片的轮换
bg:cc.Node;
bg1:cc.Node;
this.bg= this.node.getChildByName("bg");
this.bg1= this.node.getChildByName("bg1");
this.bg.y-=5;
this.bg1.y-=5;
if(this.bg1.y<=-1080)
{
this.bg1.setPosition(cc.v2(0,1080));
}
if(this.bg.y<=-1080)
{
this.bg.setPosition(cc.v2(0,1080));
}
//还有设置bg的y=1080;
总结
就到着了,就是主要看官方文档,然后不断的多做几个游戏,功夫就自然水到渠成。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)