立即学习:https://edu.youkuaiyun.com/course/play/8692/178922?utm_source=blogtoedu
了解top值 ,拖动canvas小球到top,查看坐标值
1 本次加入随机星星
2 ps一个星星 核武100x100->25x25
3 star.js
4
onLoad(){
let seq=cc.repeatForever(cc.rotateBy(2,360));
#旋转,持续两秒2,旋转360度
this.node.runAction(seq);
}
5 核武标志透明度改成0
6
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;
}