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;

总结

就到着了,就是主要看官方文档,然后不断的多做几个游戏,功夫就自然水到渠成。

posted @   cactus9  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示