Cocos Creator2D游戏开发(9)-飞机大战(7)-爆炸效果

这个爆炸效果我卡在这里好长时间,视频反复的看, 然后把代码反复的测试,修改,终于给弄出来
视频中这段,作者也是修改了好几次, 跟着做也走了不少弯路; 最后反正弄出来了;
有几个坑;
① 动画体创建位置是enemy_prefab
② enemy_prefab预制体下不用放动画就行;
③ 代码中引用Animation不会有提示,要手动添加

下面直接跟着做把, 争取一次能成吧

  1. 在enemy_prefab下创建俩动画体, 命名enemy_animation,explosion_animation
    enemy_animation下里就放一张enemy图片,但是节点选择enemy才能创建; 我们要的是文件;
    在这里插入图片描述
  2. 将动画体拖斤enemy_prefab的属性检查器中
    在这里插入图片描述
    最坑的一步来了

Enemy.ts文件中添加动画的引用(需要手动把Animation,放在import 里面)

   @property(Animation) ExploAnim: Animation;   // 添加属性 以便在代码中 播放动画

完整代码如下

import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, Animation } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Component {

    @property(Animation) ExploAnim: Animation;   // 添加属性 以便在代码中 播放动画

    private isExplo = false;
    private collider: Collider2D;

    start() {
        this.collider = this.getComponent(Collider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); // 碰撞函数注册
        }
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {

        if (this.isExplo === false) { // 碰撞一次就行了
            this.isExplo = true;
            this.collider.destroy();
            this.ExploAnim.play("explosion_animation");
            this.scheduleOnce(() => {   // 匿名函数  局部函数            
                this.node.destroy();
            }, 0.9);
        }
    }

    update(deltaTime: number) {
        const pos = this.node.getPosition();
        if (pos.y < -400) {
            this.node.destroy();
        } else {
            this.node.setPosition(pos.x, pos.y - 5);
        }
    }
}



最后效果有了;
在这里插入图片描述

留下了几个坑没有填, 这些节点的颜色不会改,没找到,隔行如隔山
第二个这个API文档我没看懂 https://docs.cocos.com/creator/3.8/api/zh/result?keyword=color
在这里插入图片描述

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值