您的位置:首页 > 游戏 > 手游 > Cocos Creator2D游戏开发(9)-飞机大战(7)-爆炸效果

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

2024/10/6 6:02:54 来源:https://blog.csdn.net/xy3233/article/details/140834110  浏览:    关键词: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
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com