cocos发射子弹

@property(Prefab)

    bbul2:Prefab = null

start() {

 //攻击计时器

        this.schedule(()=>{

            //创建子弹

            let zzd2 = instantiate(this.bbul2)

            //设置父物体

            zzd2.setParent(director.getScene().getChildByName('Canvas').getChildByName('nnaa1'))

            //设置子弹位置

            zzd2.setPosition(this.node.position.x,this.node.position.y + 50)

        },0.5)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用 Cocos Creator 引擎和 JavaScript 实现飞机大战的示例代码: 首先,在 Cocos Creator 中创建一个场景,在场景中放置玩家角色、敌机和子弹的预制体对象。 然后,创建一个脚本组件来控制玩家角色的移动和发射子弹: ```javascript cc.Class({ extends: cc.Component, properties: { bulletPrefab: cc.Prefab, // 子弹预制体 bulletSpeed: 500, // 子弹速度 }, onLoad() { this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); }, onTouchMove(event) { let delta = event.getDelta(); let playerPos = this.node.position; playerPos.addSelf(delta); this.node.position = playerPos; }, onTouchEnd() { this.shootBullet(); }, shootBullet() { let bullet = cc.instantiate(this.bulletPrefab); bullet.position = this.node.position; cc.director.getScene().addChild(bullet); let bulletMoveAction = cc.moveBy(2, cc.v2(0, cc.winSize.height)); let bulletDestroyAction = cc.callFunc(() => { bullet.destroy(); }); bullet.runAction(cc.sequence(bulletMoveAction, bulletDestroyAction)); }, }); ``` 以上代码展示了玩家角色的移动和子弹发射。玩家角色通过触摸移动,松开触摸时发射子弹子弹通过移动动作实现飞行,并在到达屏幕顶部时销毁。 接下来,你可以创建一个敌机脚本组件来控制敌机的移动和发射子弹: ```javascript cc.Class({ extends: cc.Component, properties: { bulletPrefab: cc.Prefab, // 子弹预制体 bulletSpeed: 300, // 子弹速度 }, onLoad() { this.schedule(this.shootBullet, 1); }, shootBullet() { let bullet = cc.instantiate(this.bulletPrefab); bullet.position = this.node.position; cc.director.getScene().addChild(bullet); let bulletMoveAction = cc.moveBy(2, cc.v2(0, -cc.winSize.height)); let bulletDestroyAction = cc.callFunc(() => { bullet.destroy(); }); bullet.runAction(cc.sequence(bulletMoveAction, bulletDestroyAction)); }, }); ``` 以上代码展示了敌机的移动和子弹发射。敌机通过定时器每秒发射一颗子弹子弹通过移动动作实现飞行,并在到达屏幕底部时销毁。 你可以根据需要进一步扩展玩家角色、敌机和子弹的功能和属性,例如不同的子弹运行轨迹、不同的敌机类型等。 希望对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值