预设体 和 随机生成预设体
@ccclass
export default class EnemyConfigContro extends cc.Component {
@property(cc.Prefab)
enemy: cc.Prefab=null;
onLoad () {}
start () {
// 每2秒生成一个敌机
this.schedule(()=>{
let en=cc.instantiate(this.enemy);
en.setParent(cc.director.getScene())
en.y=this.node.y;
en.x=Math.random()*400+20;
},2)
}
update (dt) {}
}
死亡 加载图片 更换 精灵
// 自己死亡
die(){
this.isDie=true;
// 先加载爆炸图片
cc.loader.loadRes("enemy0_die",cc.SpriteFrame,(err,res)=>{
this.node.getComponent(cc.Sprite).spriteFrame=res;
})
// 300 毫秒后销毁
setTimeout(()=>{
this.node.destroy();
},300);
}
碰撞
//碰撞方法
onCollisionEnter(other){
// 自己销毁 和让敌军死亡
if(other.tag==1){
// 说明是敌军 需要将敌军销毁
other.getComponent(EnemyContro).die();
//销毁自己
this.node.destroy();
}
}
// 区分碰撞对象是根据对象身上绑定的tag 来确定的
// 开启碰撞检测
cc.director.getCollisionManager().enabled=true;
碰撞需要开启碰撞检测
监听事件
start () {
//绑定手指监听事件
this.node.on(cc.Node.EventType.TOUCH_MOVE,(event)=>{
this.node.setPosition(event.getLocation());
})
// 攻击 每隔一秒的时间创建一个子弹 子弹是 预设体
this.schedule(()=>{
// 创建子弹
let bullet=cc.instantiate(this.bulletPre);
// 创建了预设体 第一步一定是设置父级
bullet.setParent(cc.director.getScene()) // 父级是当前场景
bullet.x=this.node.x;
bullet.y=this.node.y+65;
},1)
// 开启碰撞检测
cc.director.getCollisionManager().enabled=true;
}
播放音乐
cc.loader.loadRes("/music/y1271",cc.AudioClip,(res,clip)=>{
// 赋值音频
player.clip=clip;
player.play(); // 播放
// player.stop(); //停止
// player.isPlaying; // 是否正在播放
// player.pause(); //暂停
// player.resume(); //恢复
player.loop=true;// 循环播放
player.volume=1; //声音大小
});
// 加载音频 第二种方式
cc.loader.loadRes("/music/y1271",cc.AudioClip,(res,clip)=>{
// 赋值音频
let audioId= cc.audioEngine.playMusic(clip,true);
// 是否正在播放
cc.audioEngine.isPlaying(); //
// // 停止
// cc.audioEngine.stop(audioId);
// //暂停
// cc.audioEngine.pause(audioId);
// //恢复
// cc.audioEngine.resume(audioId);
// //循环播放
// cc.audioEngine.setLoop(audioId,true);
// 声音大小
cc.audioEngine.setVolume(audioId,1);
});