【cocos 2d微信小游戏开发教程】基础使用笔记分享(二)

简介: 【cocos 2d微信小游戏开发教程】基础使用笔记分享(二)
  1. 音频播放新建AudioSource组件播放,勾选循环自动播放即可 通过代码播放,新建PlayMusic脚本
  • 第一种方式
start () {
    //找到音频组件
    let player:cc.AudioSource = this.getComponent(cc.AudioSource);
    //加载音频
    cc.resources.load("start", cc.AudioClip, function(err, sp:cc.AudioClip) {
        //赋值音频
        player.clip = sp;
        //播放
        player.play();
        //是否正在播放
        player.isPlaying
        //暂停
        player.pause();
        //恢复
        player.pause();
        //停止
        player.stop();
        //是否循环播放
        player.loop = true;
        //修改声音大小 1最大声
        player.volume = 1;
    });
}
  • 第二种方式
//加载音频
cc.resources.load("start", cc.AudioClip, function(err, sp:cc.AudioClip) {
    //播放背景音乐
    let audioId: number = cc.audioEngine.playMusic(sp, true);
    //是否正在播放
    cc.audioEngine.isMusicPlaying();
    let id = 1;//获取的音效id
    //暂停
    cc.audioEngine.pause(id);//暂停某个音效
    cc.audioEngine.pauseMusic();//暂停背景音乐
    cc.audioEngine.pauseAll();//暂停所有声音
    //恢复某个音效
    cc.audioEngine.resume(id);
    //停止
    cc.audioEngine.stop(id);
    //循环播放
    cc.audioEngine.setLoop(id, true);
    //声音大小
    cc.audioEngine.setVolume(id, 1);
})
  1. 物理系统
    新增物理系统刚体组件(Rigid Body)

    编写代码开启
const {ccclass, property} = cc._decorator;
@ccclass
export default class Wuli extends cc.Component {
    onLoad () {
        //开启物理系统,注意只能在onLoad开启,不可以在start
        cc.director.getPhysicsManager().enabled = true;
    }
    start () {
       //获取刚体
        let rbody = this.getComponent(cc.RigidBody);
        //给物体一个向x为1000,y为0的力,作用点在0,0
        rbody.applyForce(cc.v2(1000,0), cc.v2(0,0), true);
        //给物体中心点一个力
        rbody.applyForceToCenter(cc.v2(5000,0), true);
        //给物体一个速度,每秒多少像素,速度是匀速的,力会导致速度越来越快
        rbody.linearVelocity = cc.v2(50, 0);
    }
    // update (dt) {}
}

Enabled Contact Listener 是否开启碰撞检测
type值的解释

static 静态,不受到重力也不受到速度,比如房子地面
kinematic 不受重力,受到速度影响
dynamic 可以受到重力和速度的影响
animated 参与动画时使用

  1. 物理碰撞
    给物体添加合适的碰撞体,注意是在物理组件里加

    检测碰撞,记得先去Rigid Body开启Enabled Contact Listener碰撞检测
onLoad () {
    //开启物理系统,注意只能在onLoad开启,不可以在start
    cc.director.getPhysicsManager().enabled = true;
}
start () {
    //获取刚体
    let rbody = this.getComponent(cc.RigidBody);
    //给物体一个速度,每秒多少像素,速度是匀速的,力会导致速度越来越快
    rbody.linearVelocity = cc.v2(50, 0);
}
//开始碰撞
onBeginContact(contact, self, other){       
    //碰撞点,用于生成火花爆炸效果等
    let points = contact.getWorldManifold().points;
    console.debug('发生碰撞'+points);
    //法线 两个物体碰撞,垂直于被碰撞体的一条线 事例:如果发生斜着碰撞,火花仍然垂直产生
    let normal = contact.getWorldManifold().normal;
}
//结束碰撞
onEndContact(contact, self, other){
}
// update (dt) {}
  1. 射线
onLoad () {
    //开启物理系统,注意只能在onLoad开启,不可以在start
    cc.director.getPhysicsManager().enabled = true;
    
    /**
     * 打出一条射线 从物体往天上打一条100的射线
     * 
     * cc.RayCastType.Any   穿过多个刚体时,随机返回碰撞的一个点
     * cc.RayCastType.Closest   返回碰撞第一个点
     * cc.RayCastType.All   返回多个点
     * cc.RayCastType.AllClosest   返回每个物体的碰撞的第一个点
     **/
    let results = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x, this.node.y+100), cc.RayCastType.Closest);
    for(let i = 0; i<results.length; i++){
        let res = results[i];
        //射线碰到的碰撞器
        // res.collider
        //碰到的点
        // res.point
        //碰到的法线
        // res.normal
    }
}

射线小练习,实现物体上下移动

const {ccclass, property} = cc._decorator;
@ccclass
export default class Wuli extends cc.Component {
    //方向
    dir: cc.Vec2 = cc.v2(0,1);
    onLoad () {
        //开启物理系统,注意只能在onLoad开启,不可以在start
        cc.director.getPhysicsManager().enabled = true;
    }
    update (dt) {
        //向下移动
        this.node.y -= this.dir.y * 100 * dt;
        //射线检测
        let res = cc.director.getPhysicsManager().rayCast(this.node.getPosition(), cc.v2(this.node.x, this.node.y- this.dir.y*100), cc.RayCastType.Closest);
        console.log(res.length);
        if(res.length > 0){
            this.dir.y *= -1;
        }
    }
}

  1. 动作系统
// 创建一个移动动作,2秒移动到100,100坐标位置
var action = cc.moveTo(2, 100, 100);
// 创建一个移动动作,2秒移动100,100个位置,To是绝对位置,By是相对位置
//var action = cc.moveBy(2, 100, 100);
//旋转
action = cc.rotateTo(2, 100);
//缩放
action = cc.scaleTo(2, 1.5,0.5);
//跳跃,2秒从当前位置向右跳200位置,跳的高度为100,跳1次
action = cc.jumpBy(2, 200, 0, 100, 1);
//闪烁 3秒闪烁5次
action = cc.blink(3, 5);
//淡出
action = cc.fadeOut(3);
//淡入
action = cc.fadeIn(3);
//渐变 3秒变成100透明度
action = cc.fadeTo(3, 100);
//颜色 3秒变成紫色
action = cc.tintTo(3, 100, 30, 100);
//显示
let action = cc.show();
//隐藏
action = cc.hide();
//切换显示隐藏  
action = cc.toggleVisibility();
//翻转
action = cc.flipX(true);
action = cc.flipY(true);
//停一秒再继续执行
action = cc.delayTime(1);
// 执行动作
node.runAction(action);
// 停止一个动作
node.stopAction(action);
// 停止所有动作
node.stopAllActions();
// 给 action 设置 tag
var ACTION_TAG = 1;
action.setTag(ACTION_TAG);
// 通过 tag 获取 action
node.getActionByTag(ACTION_TAG);
// 通过 tag 停止一个动作
node.stopActionByTag(ACTION_TAG);

顺序动作 cc.sequence 顺序动作可以让一系列子动作按顺序一个个执行。示例:

// 让节点左右来回移动
 var seq = cc.sequence(cc.moveBy(0.5, 200, 0), cc.moveBy(0.5, -200, 0));
 node.runAction(seq);

同步动作 cc.spawn 同步动作可以同步执行对一系列子动作,子动作的执行结果会叠加起来修改节点的属性。示例:

// 让节点在向上移动的同时缩放
 var spawn = cc.spawn(cc.moveBy(0.5, 0, 50), cc.scaleTo(0.5, 0.8, 1.4));
 node.runAction(spawn);

重复动作 cc.repeat 重复动作用来多次重复一个动作。示例:

// 让节点左右来回移动,并重复 5 次
 var seq = cc.repeat(
             cc.sequence(
                 cc.moveBy(2, 200, 0),
                 cc.moveBy(2, -200, 0)
             ), 5);
 node.runAction(seq);

永远重复动作 cc.repeatForever 顾名思义,这个动作容器可以让目标动作一直重复,直到手动停止。

// 让节点左右来回移动并一直重复
 var seq = cc.repeatForever(
             cc.sequence(
                 cc.moveBy(2, 200, 0),
                 cc.moveBy(2, -200, 0)
             ));

速度动作 cc.speed 速度动作可以改变目标动作的执行速率,让动作更快或者更慢完成。

// 让目标动作速度加快一倍,相当于原本 2 秒的动作在 1 秒内完成
 var action = cc.speed(
                 cc.spawn(
                     cc.moveBy(2, 0, 50),
                     cc.scaleTo(2, 0.8, 1.4)
                 ), 2);
 node.runAction(action);

动作回调

var finished = cc.callFunc(function(target, score) {
    this.score += score;
}, this, 100); //动作完成后会给玩家加 100 分
//在声明了回调动作 finished 后,您可以配合 cc.sequence 来执行一整串动作并触发回调:
var myAction = cc.sequence(cc.moveBy(1, cc.v2(0, 100)), cc.fadeOut(1), finished);
//在同一个 sequence 里也可以多次插入回调:
var myAction = cc.sequence(cc.moveTo(1, cc.v2(0, 0)), finished1, cc.fadeOut(1), finished2); // finished1、finished2 都是使用 cc.callFunc 定义的回调动作
  1. 动画系统


    绑定事件,挂载脚本,写个test()方法


    编辑一个跑步动画,全选图片拖进去即可,然后调节控制sample和speed值控制速度

脚本控制动画

```typescript
start () {
    //获取动画组件
    let ani = this.getComponent(cc.Animation);
    //播放动画
    ani.play("run");
    //暂停
    ani.pause();
    //继续
    ani.resume();
    //停止
    ani.stop();
}
```
持续更新中。。。。
目录
相关文章
|
9天前
|
开发工具
微信客服系统开发SDK使用教程- 拉取当前微信个人号列表请求(立即)
微信客服系统开发SDK使用教程- 拉取当前微信个人号列表请求(立即)
|
3天前
|
存储 JSON 测试技术
【cocos 2d微信小游戏开发教程】基础使用笔记分享(三)
【cocos 2d微信小游戏开发教程】基础使用笔记分享(三)
4 0
|
3天前
|
XML 小程序 前端开发
技术心得记录:微信小程序开发的基本流程
技术心得记录:微信小程序开发的基本流程
|
3天前
|
JSON 缓存 小程序
技术笔记:uniapp微信小程序支付
技术笔记:uniapp微信小程序支付
|
3天前
|
API 开发者
【cocos 2d微信小游戏开发教程】基础使用笔记分享(一)
【cocos 2d微信小游戏开发教程】基础使用笔记分享(一)
6 0
|
5天前
|
小程序 Java Maven
springboot开发微信小程序
springboot开发微信小程序
9 0
|
6天前
|
小程序 开发者 Windows
安装VantWeapp开发微信小程序
安装VantWeapp开发微信小程序
15 0
|
9天前
|
开发工具
云控微信开发SDK使用教程--手机微信朋友圈图片上传服务端
云控微信开发SDK使用教程--手机微信朋友圈图片上传服务端
|
2月前
|
小程序 API 数据安全/隐私保护
微信小程序开发中的一些常用标签
这些标签是微信小程序开发中的基础,开发者可以根据需要组合使用这些标签来构建小程序的界面。每个标签都有其属性和事件,可以通过属性来调整组件的样式和行为,通过事件来响应用户的操作。
61 5
|
25天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的智慧旅游平台开发微信小程序的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的智慧旅游平台开发微信小程序的详细设计和实现
39 8