cocoscreator2.x炸弹爆炸物品飞出效果

  • 效果展示:
    请添加图片描述
  • 代码

const {ccclass, property} = cc._decorator;

@ccclass
export default class Main extends cc.Component {

    @property(cc.Node)
    private gameNode: cc.Node = null;

    @property(cc.Prefab)
    private boomPre: cc.Prefab = null;

    @property(cc.Prefab)
    private boxPre: cc.Prefab = null;

    private boomCircle: number = 400;//爆炸半径
    private boomAddition: number = 1000000;//效果加成
    private boxCollider: cc.Node[] = [];

    private boomPos: {x: number,y: number}[] = [{x: -100, y: 200}, {x: 50, y: -50}, {x: 300, y: 280}, {x: -240, y: -190}, {x: 180, y: 80}];
    onLoad () {
        cc.director.getPhysicsManager().enabled = true;
        this.createBox();
        this.createBoom();
    }

    createBoom() {
        let boom = cc.instantiate(this.boomPre);
        boom.setPosition(0,0,0);
        boom.parent = this.gameNode;

        cc.tween(boom)
            .to(0.1, {scale: 1.1, color: cc.Color.WHITE})
            .to(0.1, {scale: 1.0, color: cc.Color.RED})
            .union()
            .repeat(10)
            .call(()=>{
                boom.scale = 1.5;
                setTimeout(() => {
                    this.boomEnable(boom.getPosition());
                    boom.removeFromParent(false);
                },200);
            })
            .start()
    }
    
    createBox() {
        for(let i=0; i<this.boomPos.length; i++){
            let pos = this.boomPos[i];
            let node = cc.instantiate(this.boxPre);
            node.setPosition(pos.x, pos.y);
            this.boxCollider.push(node);
            node.parent = this.gameNode;
        }
    }

    boomEnable(pos: cc.Vec2) {
        this.boxCollider.forEach((node, index) => {
            //boxPos应为传入的pos节点的相对坐标。。(示例中2节点为兄弟节点,且传入pos为{0,0})😀
            let boxPos = node.getPosition();
            let vec = boxPos.sub(pos);//向量
            let distance = vec.mag();//模长
            if(distance < this.boomCircle){//判断是否在爆炸冲击范围内
                let addition = this.boomAddition / distance;//相距越近的box受到的冲量加成越大,受到的冲量大小
                let normalize = vec.normalize();//单位向量
                let rigidBody = node.getComponent(cc.RigidBody);
                node.color = cc.Color.RED;
                rigidBody.applyForceToCenter(cc.v2(normalize.x * addition, normalize.y * addition), true);//给刚体冲量
            }else{
                node.color = cc.Color.GREEN;
            }
        });
    }
}

先创建出炸弹prefab和物品prefab,其中物品prefab上需要添加物理组件->collider组件。然后将脚本挂载在场景中,挂载需要的prefab及节点。运行即可

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然可以!下面是一个使用Cocos Creator和TypeScript创建炸弹人游戏的示例代码: ``` const { ccclass, property } = cc._decorator; enum ItemType { BombRange, SpeedUp } @ccclass export default class BombMan extends cc.Component { @property(cc.Prefab) private playerPrefab: cc.Prefab = null; @property(cc.Prefab) private enemyPrefab: cc.Prefab = null; @property(cc.Prefab) private itemPrefab: cc.Prefab[] = []; @property(cc.SpriteFrame) private brickSprite: cc.SpriteFrame = null; private player: cc.Node = null; private enemies: cc.Node[] = []; private items: cc.Node[] = []; private map: number[][] = []; private gridSize: number = 45; private gridRows: number = 15; private gridCols: number = 15; private bombRange: number = 1; private playerSpeed: number = 200; onLoad() { this.generateMap(); this.spawnPlayer(); this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } generateMap() { for (let i = 0; i < this.gridRows; i++) { this.map[i] = []; for (let j = 0; j < this.gridCols; j++) { let tileType = Math.random() < 0.8 ? 0 : 1; this.map[i][j] = tileType; if (tileType === 1) { let brickNode = new cc.Node(); let sprite = brickNode.addComponent(cc.Sprite); sprite.spriteFrame = this.brickSprite; brickNode.parent = this.node; brickNode.setPosition(j * this.gridSize, i * this.gridSize); } } } } spawnPlayer() { this.player = cc.instantiate(this.playerPrefab); this.player.parent = this.node; this.player.setPosition(this.gridSize / 2, this.gridSize / 2); } spawnEnemy() { let enemyNode = cc.instantiate(this.enemyPrefab); enemyNode.parent = this.node; // 随机生成敌人的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); enemyNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.enemies.push(enemyNode); // 随机生成敌人的生成时间间隔 this.scheduleOnce(this.spawnEnemy, Math.random() * 3 + 2); } spawnItem() { let itemType = Math.floor(Math.random() * this.itemPrefab.length); let itemNode = cc.instantiate(this.itemPrefab[itemType]); itemNode.parent = this.node; // 随机生成道具的位置 let randomRow = Math.floor(Math.random() * this.gridRows); let randomCol = Math.floor(Math.random() * this.gridCols); itemNode.setPosition(randomCol * this.gridSize, randomRow * this.gridSize); this.items.push(itemNode); // 随机生成道具的生成时间间隔 this.scheduleOnce(this.spawnItem, Math.random() * 5 + 3); } // ... } ``` 这只是一个简单的示例代码,包含了地图的生成、玩家、敌人和道具的生成。你可以根据自己的需求进一步完善游戏逻辑和其他功能。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蟹 !

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值