CocosCreator3.8第三节预制体

@property(Prefab) eggPrefab: Prefab; //预制体

import {Prefab,input,Input,KeyCode, Color,_decorator, v3, tween, Component, EventTouch, Node, log,Label, Tween, Vec3, EventKeyboard, instantiate, random } from 'cc';

@property(Prefab) eggPrefab: Prefab; //预制体

缩放会影响设置位置踩坑

/*
 * @Author: Coat [email protected]
 * @Date: 2024-01-26 10:31:06
 * @LastEditors: Coat [email protected]
 * @LastEditTime: 2024-01-26 16:45:35
 * @FilePath: \NewProject\assets\GameRoot.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {Prefab,input,Input,KeyCode, Color,_decorator, v3, tween, Component, EventTouch, Node, log,Label, Tween, Vec3, EventKeyboard, instantiate, random } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GameRoot')
export class GameRoot extends Component {
  
    @property(Node) player: Node;
    @property(Node) jiRoot: Node;
    @property(Node) danRoot: Node;
    @property(Prefab) eggPrefab: Prefab; //预制体

    playerPosIndex = 0
    jiPosArr = []
    initData(){
        for(let i = 0; i<this.jiRoot.children.length;i++){
            const ji = this.jiRoot.children[i]
            this.jiPosArr[i] = ji.position.x
        }
    }
    start() {
        this.initData()
        this.openInputEvent() 
        this.renderPlayPos()
        console.log(this.jiRoot.position.y,'this.jiRoot.position.y');
        
        this.startCreateEggs()
    }
    startCreateEggs(){
        //schedule定时器4个参数后两个可选 动作 间隔 执行多少次,延迟多少s执行
        //定时器有两个
        this.schedule(this.createOneEgg,2)
    }

    createOneEgg(){
        const randomIndex = Math.floor(Math.random()*5)
        const egg = instantiate(this.eggPrefab) //预制体实例化节点
        this.danRoot.addChild(egg) //加入到管理节点
        // 鸡蛋位置怎么设置?  x是位置随机
        egg.setPosition(this.jiPosArr[randomIndex] , this.jiRoot.position.y)
    }
    openInputEvent(){
        input.on(Input.EventType.KEY_DOWN,this.onKeyDown,this)

    }
    movePlay(dir:1|-1){
        this.playerPosIndex +=dir
        if(this.playerPosIndex<0){ 
            this.playerPosIndex = 4
        }
        if(this.playerPosIndex>4){
            this.playerPosIndex =0
        }

        const x = this.jiPosArr[this.playerPosIndex]
        const y = this.player.position.y
        this.player.setPosition(x,y)
    }
    renderPlayPos(){
        const x = this.jiPosArr[this.playerPosIndex]
        const y = this.player.position.y
        this.player.setPosition(x,y) 
    }
    
    onKeyDown(event:EventKeyboard){
        switch(event.keyCode){
            case KeyCode.KEY_A:
                // 左移
                this.movePlay(-1)
                break;
            case KeyCode.KEY_D:
                this.movePlay(1)

                break;
        }
    }
   
    update(deltaTime: number) {
        for (let i = 0; i < this.danRoot.children.length; i++) {
            const egg = this.danRoot.children[i]
            const x = egg.position.x
            const y = egg.position.y - 150*deltaTime //随着时间变大
            egg.setPosition(x,y)
            if(y<-500){
                egg.destroy()
            }
        }
    }
}



你可能感兴趣的:(cocos2d)