当前位置: 首页 > news >正文

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
在这里插入图片描述

http://www.lryc.cn/news/415743.html

相关文章:

  • 终于有人把华为认证全部说清楚了
  • 【知识】pytorch中的pinned memory和pageable memory
  • 【系统架构设计】数据库系统(五)
  • 如何对人工智能系统进行测试|要点,方法及流程
  • CVE-2023-37569~文件上传【春秋云境靶场渗透】
  • MySQL简介 数据库管理与表管理
  • PHP 函数性能优化的技巧是什么?
  • 小程序支付(前端)
  • 开发一个自己的VSCode插件
  • Milvus 向量数据库进阶系列丨构建 RAG 多租户/多用户系统 (上)
  • 前缀和(更新中)
  • 记录一次单例模式乱用带来的危害。
  • 外卖项目day14(day11)---数据统计
  • 养猫科普!牙口不好的猫咪怎么选粮?好吃易消化主食罐推荐
  • 力扣刷题之3143.正方形中的最多点数
  • 【更新2022】省级经济高质量发展指标体系测度 含代码 2000-2022
  • 缓冲流练习
  • 自己履行很多的话语,依旧按照这个方式进行生活
  • 交通预测数据文件梳理:METR-LA
  • 按钮类控件
  • opencascade AIS_ViewController源码学习 视图控制、包含鼠标事件等
  • 拉削基础知识——拉床的类型及特点
  • docker-compose笔记
  • C# 自定义控件无法加载
  • avl树自实现(带图),探讨平衡因子与旋转
  • Elasticsearch 的DSL查询,聚合查询与多维度数据统计
  • 【如何高效处理前端常见问题:策略与实践】
  • 聊聊前端 JavaScript 的扩展运算符 “...“ 的使用场景
  • 华为续签了,但我准备离职了
  • RocketMQ 的认证与授权机制