背景滚动方法

//每一帧都移动
    //遍历子物体(背景)
    update (dt) { //dt是每秒有dt帧
        for(let bgNode of this.node.children){
            //移动 帧 -> 秒
            bgNode.y -= 50 * dt;//变成每秒移动50

            if(bgNode.y < -850){
                bgNode.y += 852*2;
            }
        }
    }

做物体移动的基本方法

//每一帧都移动
    //遍历子物体(背景)
    update (dt) { //dt是每秒有dt帧
        for(let bgNode of this.node.children){
            //移动 帧 -> 秒
            bgNode.y -= 50 * dt;//变成每秒移动50

            if(bgNode.y < -850){
                bgNode.y += 852*2;
            }
        }
    }

setTimeot()延时方法

 setTimeout(() => {//延时方法,多长时间后执行什么方法
            this.node.destroy();
        }, 200);//单位毫秒

做碰撞的基本方法

//碰撞
    onCollisionEnter(other){
        //如果碰到敌人,销毁自己,敌人死亡
        if(other.tag == 1){
            //销毁敌人
            other.getComponent(EnemyControl).die();//获取敌人的脚本

            //销毁自己
            this.node.destroy();
        }
    }

动态加载图片方法

//加载动态图片
        cc.resources.load("enemy0_die",cc.SpriteFrame,(err,spriteFrame)=>{
            
            this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });

创建随机敌人方法

//每隔1秒创建一个敌机
        this.schedule(()=>{
            let enemy = cc.instantiate(this.enemyPre);
            enemy.setParent(cc.director.getScene());
            enemy.y = this.node.y;
            enemy.x = Math.random() * 400;//默认从0-400, 格式为400 + 20  20到400的随机数
        },1);

背景脚本源码


const {ccclass, property} = cc._decorator;

@ccclass
export default class BgControl extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }
    //每一帧都移动
    //遍历子物体(背景)
    update (dt) { //dt是每秒有dt帧
        for(let bgNode of this.node.children){
            //移动 帧 -> 秒
            bgNode.y -= 50 * dt;//变成每秒移动50

            if(bgNode.y < -850){
                bgNode.y += 852*2;
            }
        }
    }
}

玩家飞机脚本源码


const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerControl extends cc.Component {
    //创建子弹预设体
    @property(cc.Prefab)
    bulletPre:cc.Prefab =null;
    
    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        //移动 通过监听
        let self = this;
        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 + 60;

        },0.5,); //四个参数:函数,0.5秒重复一次,重复几次,多长时间开始第一次
    
        //开启碰撞检测
        cc.director.getCollisionManager().enabled = true;
    
    }
    die(){
        
        //变为被攻击后图片
        cc.resources.load("hero1_die",cc.SpriteFrame,(err,res)=>{
            this.node.getComponent(cc.Sprite).spriteFrame = res;
        });

        //九百毫秒后消失
        setTimeout(() => {
            this.node.destroy();
        }, 900);
    
    }

    update (dt) {}
}

子弹脚本源码

import EnemyControl from "./EnemyControl";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BulletControl extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    speed:number = 800;


    start () {

    }

    update (dt) {
        //子弹移动
        this.node.y += this.speed * dt;//每秒向上移动800像素
        
        //出屏幕销毁
        if(this.node.y > 820){
            this.node.destroy();
        }
    }
    
    //碰撞
    onCollisionEnter(other){
        //如果碰到敌人,销毁自己,敌人死亡
        if(other.tag == 1){
            //销毁敌人
            other.getComponent(EnemyControl).die();//获取敌人的脚本

            //销毁自己
            this.node.destroy();
        }
    }
}

敌人脚本源码

const {ccclass, property} = cc._decorator;
import PlayerControl from "./PlayerControl";
@ccclass
export default class EnemyControl extends cc.Component {

    //是否死亡
    isDie:boolean = false;

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    start () {

    }

    update (dt) {
        //敌人移动
        if(this.isDie ==false){
            this.node.y -= 300*dt;
        }
        if(this.node.y < -50){
            this.node.destroy();
        }

    }
    
    //死亡方法
    die(){
        this.isDie = true
        //加载动态图片
        cc.resources.load("enemy0_die",cc.SpriteFrame,(err,spriteFrame)=>{
            
            this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });

        //200毫秒后销毁
        setTimeout(() => {//延时方法,多长时间后执行什么方法
            this.node.destroy();
        }, 200);//单位毫秒

    }
    onCollisionEnter(other){
        //碰到敌人 玩家死亡
        if(other.tag == 2){
            other.getComponent(PlayerControl).die();
        }
    }
}

敌人生成器脚本源码



const {ccclass, property} = cc._decorator;

@ccclass
export default class EnemyManger extends cc.Component {

    //敌机预设体
    @property(cc.Prefab)
    enemyPre: cc.Prefab = null; 

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        //每隔1秒创建一个敌机
        this.schedule(()=>{
            let enemy = cc.instantiate(this.enemyPre);
            enemy.setParent(cc.director.getScene());
            enemy.y = this.node.y;
            enemy.x = Math.random() * 400;//默认从0-400, 格式为400 + 20  20到400的随机数
        },1);
    }

    
    // update (dt) {}
}

cocos creator
在这里插入图片描述
效果图
在这里插入图片描述
在这里插入图片描述

Logo

这里是一个专注于游戏开发的社区,我们致力于为广大游戏爱好者提供一个良好的学习和交流平台。我们的专区包含了各大流行引擎的技术博文,涵盖了从入门到进阶的各个阶段,无论你是初学者还是资深开发者,都能在这里找到适合自己的内容。除此之外,我们还会不定期举办游戏开发相关的活动,让大家更好地交流互动。加入我们,一起探索游戏开发的奥秘吧!

更多推荐