cocos creat3.7 的飞机大战练习(小白笔记)

BgControl.ts

import { _decorator, Component, Node, Vec3, ValueType } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('BgControl')
export class BgControl extends Component {

    public abc = new Vec3();

    protected aaa: number = 0;

    start() {
    }

    update(deltaTime: number) {

        for (let bgNode of this.node.children) {
            this.abc = bgNode.getPosition();
            this.aaa = this.abc.y - 50 * deltaTime;
            bgNode.setPosition(this.abc.x, this.aaa);
            if (this.aaa < -694) {
                this.abc.y += 694 * 2;
                bgNode.setPosition(this.abc.x, this.abc.y);
            }

        }
        
    }
}

playercontrol.ts

import { _decorator, Component, Node, input, EventTouch, Input, Event, Vec3, Vec2, Prefab, instantiate, director, find, Canvas, Collider2D, Collider, Contact2DType, Director, BoxCollider2D, RigidBody } from 'cc';
import { bullet } from './bullet';
const { ccclass, property } = _decorator;

@ccclass('Playercontrol')
export class Playercontrol extends Component {
    @property(Prefab)
    bulletpre: Prefab = null;

 
    start() {

        
        input.on(Input.EventType.TOUCH_MOVE, (event) => {
            
            this.node.setWorldPosition(event.getUILocation().x, event.getUILocation().y, 0);
        });

       //创建定时器
        this.schedule(() => {

            //创建子弹预设体
            let bullet: Node = instantiate(this.bulletpre);
            //添加到Canvas
            this.node.parent.addChild(bullet);
            //设置子弹的初始位置
            bullet.setPosition(this.node.getPosition().x, this.node.getPosition().y + 60);
        //设置定时器时间2秒
        }, 1);

        //设置碰撞开
        this.enabled = true;
        
    }

    onthouch() {

    }

    update(deltaTime: number) {
        
    }

 bullet.ts

import { _decorator, Component, Node, Collider2D, Contact2DType, Collider, ICollisionEvent, TriggerCallback, ITriggerEvent, v3, Vec3, BoxCollider2D, PhysicsSystem2D, IPhysics2DContact } from 'cc';
import { EnenyControl } from './EnenyControl';
const { ccclass, property } = _decorator;

@ccclass('bullet')
export class bullet extends Component {
    
    speed: number = 100;
    public posY: number = 0;
    //这里
    pos: Vec3 = new Vec3();
    
    start() {

        this.pos = this.node.getPosition();
        let collider = this.getComponent(Collider2D);
        if (collider) {
        collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }

        
    }
    onBeginContact(selfCollider:Collider2D, otherCollider:Collider2D|null){
        
        if (otherCollider.tag == 1) {
            otherCollider.getComponent(EnenyControl).die();
        }
                

        }
       


    update(deltaTime: number) {
        
        this.node.setPosition(this.node.getPosition().x, this.node.getPosition().y + this.speed * deltaTime);
        if (this.node.getPosition().y > 800) {
            this.node.destroy();
        }

    }
}

enenycontrol.ts

import { _decorator, Component, Node, Animation, loader, resources, AudioClip, AudioSource, Collider2D } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnenyControl')
export class EnenyControl extends Component {
//是否死亡
    isdie: boolean = false;
    start() {
        
    }

    update(deltaTime: number) {
        if (this.isdie == false) {
           
            this.node.setPosition(this.node.getPosition().x, this.node.getPosition().y - 200 * deltaTime);
        } 
        if (this.node.getPosition().y<-800) {
            this.node.destroy();
        }
    }

    die() {
        //
        this.isdie == true;
        var anim = this.getComponent(Animation);
        anim.play("enemy_clip");
        const audio = this.node.getComponent(AudioSource);
        resources.load("boom02", AudioClip, (err, clip) => {
            audio.play();
            //this.enabled = false;
        });

        setTimeout(() => {
            this.node.destroy();
        },500);
        //this.node.destroy();
    }
}

EnemyManager

import { _decorator, Component, Node, Prefab, instantiate, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {
    @property(Prefab)
    enemyPre: Prefab = null;
    enemypos: Vec3 = new Vec3();
    start() {
        //生成敌人
        this.schedule(()=> {
            let enemy = instantiate(this.enemyPre);

            this.node.parent.addChild(enemy);

            enemy.position = this.node.position;

            
            let pos = this.node.position.x;
           
            pos=  Math.random() * 400 + 20;

            this.node.setPosition(pos, this.node.getPosition().y);
            console.log(pos);
        } ,1);
    }

    update(deltaTime: number) {
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我救我自己

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

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

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

打赏作者

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

抵扣说明:

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

余额充值