MainScene.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { _decorator, Component, director, instantiate, Node, Prefab, UITransform, Vec3 } from 'cc';
  2. import { GameData } from './GameData';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('MainScene')
  5. export class MainScene extends Component {
  6. @property(Prefab)
  7. countDown:Prefab;
  8. speed:number = 0;
  9. boss:Node = null;
  10. countDownTS:any = null;
  11. protected onLoad(){
  12. //let gameData = instantiate(GameData);
  13. this.speed = GameData.speed;
  14. }
  15. start() {
  16. let countDown = instantiate(this.countDown);
  17. this.node.addChild(countDown);
  18. //获取倒计时预制体脚本
  19. this.countDownTS = countDown.getComponent("CountDown");
  20. this.boss = this.node.getChildByName("boss_05_normal");
  21. }
  22. update(deltaTime: number) {
  23. //倒计时结束,游戏开始
  24. if(this.countDownTS.isOver){
  25. let x =this.boss.getPosition().x;
  26. x += this.speed * deltaTime;
  27. if(x >= (this.node.getComponent(UITransform).contentSize.width)/2){
  28. this.speed = 0;
  29. }else{
  30. this.boss.setPosition(new Vec3(x,this.boss.getPosition().y));
  31. }
  32. }
  33. }
  34. }