学习笔记(4):走入 Cocos Creator视频教程-场景动态中添加核武标志

立即学习:https://edu.youkuaiyun.com/course/play/8692/178922?utm_source=blogtoedu

了解top值 ,拖动canvas小球到top,查看坐标值

1 本次加入随机星星

2 ps一个星星 核武100x100->25x25

3 star.js

onLoad(){

           let seq=cc.repeatForever(cc.rotateBy(2,360));

          #旋转,持续两秒2,旋转360度

           this.node.runAction(seq);          

}

5 核武标志透明度改成0

           let apa=cc.fadeIn(1.0);

            #渐进 显示到1

7  apa.setDuration(3)

#设定动作持续几秒完成

    this.node.runAction(apa)

8 计算一个目标地址:moveTo的目标地址

     this._targetX=RandomNumBoth(this._left,this._right)

     this._targetY=RandomNumBoth(this._bottom,this._top)

     console.log(this._targetX,this._targetY)

     this._function RandomNumBoth(Min,Max){

                   var Range=Max-Min;

                   var Rand=Math.random();

                   var num=Min+Math.round(Rand*Range)

                   return num

       }

9 开始移动moveTo

        var self=this

        setTimeout(function(){

                      self.node.runAction(cc.moveTo(2,self._targetX,self.targetY)

         },3000)

        setTimeout(function(){

        self.node.runAction(function(){

                     var apa2=cc.fadeOut(0)

                     apa2.setDurationn(1);

                     self.node.runAction(apa2);

                     self.node.destory();},5000)

小球碰撞实验:

properties:{

me:{

       default:null,

       type:cc.Node

}

gameoverTitle:{

        default:null,

        type:cc.Node

}

update (dt) {

        //console.log(this.me);

       // console.log(this.node);

if(window.Global.run==true){#这里时为了防止程序停止后还是一直执行

          let me_x=this.me.x;

          let me_y=this.me.y;

          let star_x=this.node.x;

          let star_y=this.node.y;

          var star=cc.v2(star_x,star_y);

          var me=cc.v2(me_x,me_y);

          var interval=me.sub(star).mag();

          if(interval<30){

              this.me.stopAllActions();

              window.Global.run=false;

              this.gameoverTitle.active=true;

          }

    }

    },

#这里me绑定小球

#这里gameoverTitle绑定gameover Label

#的开始的时候需要把gameover Label给取消掉

预置资源类类

game.js

properties:{

        default:null,

        type:cc.Prefab

}

me:{

       default:null,

       type:cc.Node

}

gameoverTitle:{

        default:null,

        type:cc.Node

}

随机生成资源类

start(){

       let self=this

        setInterval(function(){

                   self.spawnNewStar()

         },1000)

        }

spawnNewStar:function(){

            var newStar=cc.instantiate(this.starPrefab,1)

#instantiate用于克隆节点,第一个参数未需要克隆的节点你,第二个参数表示要插入的位置

          this.node.addChild(newStar)

#节点插入

        newStar.setPosition(getNewStarPosition())

        newStar.getComponent("star").me=this.me

        newStar.getComponent("star").gameoverTitle=this.gameoverTitle

}

getNewStarPosition:function(){

           let _top=270;

           let _bottom=-296;

           let _left=-150;

           let _right=150;

           let _targetX=RandomNumBoth(_left,_right)

           let _targetY=RandomNumBoth(_top,_bottom)

           return {"x":_targetX,"y":_targetY)

}

function RandomNumBoth(min,max){

        var Range=Max-Min;

        var Rand=Math.random();

        var num=Min+Math.round(Rand*Range)

        return num;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值