为什么我的子弹不能移动?

酷酷的懒

2 回复17 浏览

来自课程:CocosCreator3.8入门教程-微信飞机大战

跟老师写的一样,但子弹生成后不会移动
 

import { _decorator, Component, EventTouch, instantiate, Node, Prefab, Vec2, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

 

@ccclass('Player')

export class Player extends Component {

    boundaryX: number = 230

    boundaryY: number = 380

    shootTime:number = 0

 

    @property

    shootRate:number = 0.5

    @property(Node)

    bullet1Parent: Node = null

    @property(Prefab)

    bullet1Prefab: Prefab = null

    @property(Node)

    bullet1Position: Node = null


 

    protected onLoad(): void {

        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)

    }

    protected onDestroy(): void {

        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)

    }

    onTouchMove(event: EventTouch) {

        let targetPosistion = new Vec3(this.node.position.x + event.getDeltaX(), this.node.position.y + event.getDeltaY())

        if (targetPosistion.x < -this.boundaryX) {

            targetPosistion.x = -this.boundaryX

        } else if (targetPosistion.x > this.boundaryX) {

            targetPosistion.x = this.boundaryX

        }

        if (targetPosistion.y < -this.boundaryY) {

            targetPosistion.y = -this.boundaryY

        } else if (targetPosistion.y > this.boundaryY) {

            targetPosistion.y = this.boundaryY

        }

        this.node.setPosition(targetPosistion)

    }

    start() {

 

    }

 

    update(deltaTime: number) {

        this.shootTime += deltaTime

        if (this.shootTime >= this.shootRate) {

            this.shootTime = 0

            const bullet1 = instantiate(this.bullet1Prefab)

            this.bullet1Parent.addChild(bullet1)

            bullet1.setWorldPosition(this.bullet1Position.worldPosition)

        }

    }

}


全部回复(2)

  • 老师_Jokey 1 楼

    update里有持续每帧更新子弹位置的代码,但个脚本没有看到位置变量更新的地方

  • 酷酷的懒 楼主 2 楼

    @老师_Jokey 这个问题已经解决了,谢谢老师

发表回复

登录后可以回复