白鹭开发-七彩贪吃蛇(二)
上篇文章介绍我们项目的各个文件夹,接下来开始介绍果实篇。
class Food extends egret.Sprite {
/**
* 食物颜色
*/
private static colorList: number[] =
[0x70f3ff, 0xff461f, 0x00bc12, 0x21a675, 0x4c221b, 0xbf242a, 0x161823, 0xffa400,];
//private static colorList: number[] =
//[0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000,];
/**
* @param x 横坐标
* @param y 纵坐标
* @param r 半径
*/
public constructor(x: number, y: number, r: number) {
super();
this.init(x, y, r);
// var timer:egret.Timer = new egret.Timer(2000,0);
// timer.addEventListener(egret.TimerEvent.TIMER,this.timerFunc,this);
// timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.timerComFunc,this);
//timer.start();
}
private food: egret.Shape;
public color: number;
/**
* 初始化
*
* 1.绘制果实
*/
// private timerComFunc()
// {
// console.log(“计时结束”);
// }
private init(x: number, y: number, r: number): void {
//获取随机颜色
this.color = this.randomColor();
this.food = new egret.Shape();
this.food.graphics.beginFill(this.color);
this.food.graphics.drawCircle(0, 0, r);
this.food.graphics.endFill();
this.food.x = r;
this.food.y = r;
//位置
this.x = x;
this.y = y;
this.addChild(this.food);
egret.Tween.get(this.food,{loop:true}).to({alpha:.0 },300,egret.Ease.circIn).to({alpha:1 },300,egret.Ease.circIn);
}
/**
* 获取随机的颜色
*/
private randomColor(): number {
// return Food.colorList[Math.round(Math.random() * Food.colorList.length)];
return parseInt(“0x” + (“000000” + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6));
}
/**
* 被吃
*/
public onEat() {
this.parent.removeChild(this);
}
}
颜色数组得到的是果实的颜色,下面的随机取色是从数组里面取,
缓动动画使的我们果实更加具有动效,最后执行被吃操作,
今天又是代码满满的一天