网站首页 mysql技术
跟我一起做游戏--偷吃蛇来了--第二期
发布时间:2017-02-25 06:44查看次数:4953
本期创建对象蛇 鸟语名字snake 也可以叫she 反正你乐意就可以!
首先.蛇是需要吃东西的 所以他有头 身体 如果加皮肤就是有尾部,
这里我们做简单的没有皮肤,我们把蛇拆分成2部分头和身体
定义变量如下
private snakeHeadColor: number = 0xED2525;
private snakeHead: eui.Rect; //定义蛇头
private snakeBodyList: eui.Rect[]=[];//定义蛇身体
//这里要注意如果没有默认值 这个变量是不存在的会导致错误!!!
public speed: number = 20; //移动速度
1.接下来就是制作头部了
/**初始化蛇 */
private createSnake(X: number, Y: number) {
this.snakeHead = new eui.Rect(60, 60, this.snakeHeadColor);
this.snakeHead.ellipseWidth = 60; //圆形蛇头
this.snakeHead.x = X;
this.snakeHead.y = Y;
this.snakeBodyList.push(this.snakeHead); //把蛇头压入数组中
this.addChild(this.snakeBodyList[this.snakeBodyList.length - 1]); //添加蛇头到界面
this.setChildIndex(this.snakeBodyList[this.snakeBodyList.length - 1], -999);//保证蛇头在第一位置
}
2.蛇的身体是通过吃食物长大的!!
/***
* 吃食物后增加节点
* @param color 食物的颜色
*/
public afterEat(color: number) {
//节点颜色
var node: eui.Rect = new eui.Rect(50, 50, color);
node.ellipseWidth = 50; //这里要注意的是矩形的形状
//节点位置
node.x = this.snakeBodyList[this.snakeBodyList.length - 1].x;
node.y = this.snakeBodyList[this.snakeBodyList.length - 1].y;
//将新增节点添加入蛇身和蛇身节点list
this.snakeBodyList.push(node);
this.addChild(this.snakeBodyList[this.snakeBodyList.length - 1]);
//不要忘了指定新增节点的显示索引,我们将它放在所有节点的最下面。
this.setChildIndex(this.snakeBodyList[this.snakeBodyList.length - 1], 0);
}
3.就是蛇会移动
/**移动 */
public move(e: egret.TouchEvent, during: number) {
var mx: number = e.stageX;
var my: number = e.stageY
var tween: egret.Tween;
for (var i = this.snakeBodyList.length - 1; i >= 1; i--) {
tween = egret.Tween.get(this.snakeBodyList[i]);
tween.to({ x: this.snakeBodyList[i - 1].x, y: this.snakeBodyList[i - 1].y }, during);
}
var hx = this.x + this.snakeBodyList[0].x;
var hy = this.y + this.snakeBodyList[0].y;
//设置当前缓动对象为蛇头
tween = egret.Tween.get(this.getHead());
var tmpx, tmpy;
if (hx == mx && hy == my) {
//位置相同
return;
}
if (hx != mx) {
//非垂直
//斜率
var mk = (my - hy) / (mx - hx);
//角度
var mangle = Math.atan(mk);
if (mx < hx) {
//左边
tmpx = this.snakeBodyList[0].x - this.speed * Math.cos(mangle);
tmpy = this.snakeBodyList[0].y - this.speed * Math.sin(mangle);
tween.to({ x: tmpx, y: tmpy }, during);
} else {
//右边
tmpx = this.snakeBodyList[0].x + this.speed * Math.cos(mangle);
tmpy = this.snakeBodyList[0].y + this.speed * Math.sin(mangle);
tween.to({ x: tmpx, y: tmpy }, during);
}
} else {
//垂直
if (mx < hx) {
//水平向左
tmpx = this.snakeBodyList[0].x - this.speed;
tween.to({ x: tmpx, y: tmpy }, during);
} else {
//水平向右
tmpx = this.snakeBodyList[0].x + this.speed;
tween.to({ x: tmpx, y: tmpy }, during);
}
}
}
4.因为蛇吃食物 是需要检测碰撞的
public getHead() {
return this.snakeBodyList[0];
}
至此,我们的蛇的对象就完成了!!!是不是很容易啊
最后我们的源码是这个样子的
class snake extends eui.Component {
//定义蛇对象
private snakeHeadColor: number = 0xED2525;
private snakeHead: eui.Rect; //定义蛇头
private snakeBodyList: eui.Rect[]=[];//定义蛇身体
public speed: number = 20; //移动速度
public constructor() {
super();
this.createSnake(333, 260);
}
/**初始化蛇 */
private createSnake(X: number, Y: number) {
this.snakeHead = new eui.Rect(60, 60, this.snakeHeadColor);
this.snakeHead.ellipseWidth = 60; //圆形蛇头
this.snakeHead.x = X;
this.snakeHead.y = Y;
console.log(this.snakeBodyList.length);
this.snakeBodyList.push(this.snakeHead); //把蛇头压入数组中
this.addChild(this.snakeBodyList[this.snakeBodyList.length - 1]); //添加蛇头到界面
this.setChildIndex(this.snakeBodyList[this.snakeBodyList.length - 1], -999);//保证蛇头在第一位置
}
/***
* 吃食物后增加节点
* @param color 食物的颜色
*/
public afterEat(color: number) {
//节点颜色
var node: eui.Rect = new eui.Rect(50, 50, color);
node.ellipseWidth = 50;
//节点位置
node.x = this.snakeBodyList[this.snakeBodyList.length - 1].x;
node.y = this.snakeBodyList[this.snakeBodyList.length - 1].y;
//将新增节点添加入蛇身和蛇身节点list
this.snakeBodyList.push(node);
this.addChild(this.snakeBodyList[this.snakeBodyList.length - 1]);
//不要忘了指定新增节点的显示索引,我们将它放在所有节点的最下面。
this.setChildIndex(this.snakeBodyList[this.snakeBodyList.length - 1], 0);
}
/**移动 */
public move(e: egret.TouchEvent, during: number) {
var mx: number = e.stageX;
var my: number = e.stageY
var tween: egret.Tween;
for (var i = this.snakeBodyList.length - 1; i >= 1; i--) {
tween = egret.Tween.get(this.snakeBodyList[i]);
tween.to({ x: this.snakeBodyList[i - 1].x, y: this.snakeBodyList[i - 1].y }, during);
}
var hx = this.x + this.snakeBodyList[0].x;
var hy = this.y + this.snakeBodyList[0].y;
//设置当前缓动对象为蛇头
tween = egret.Tween.get(this.getHead());
var tmpx, tmpy;
if (hx == mx && hy == my) {
//位置相同
return;
}
if (hx != mx) {
//非垂直
//斜率
var mk = (my - hy) / (mx - hx);
//角度
var mangle = Math.atan(mk);
if (mx < hx) {
//左边
tmpx = this.snakeBodyList[0].x - this.speed * Math.cos(mangle);
tmpy = this.snakeBodyList[0].y - this.speed * Math.sin(mangle);
tween.to({ x: tmpx, y: tmpy }, during);
} else {
//右边
tmpx = this.snakeBodyList[0].x + this.speed * Math.cos(mangle);
tmpy = this.snakeBodyList[0].y + this.speed * Math.sin(mangle);
tween.to({ x: tmpx, y: tmpy }, during);
}
} else {
//垂直
if (mx < hx) {
//水平向左
tmpx = this.snakeBodyList[0].x - this.speed;
tween.to({ x: tmpx, y: tmpy }, during);
} else {
//水平向右
tmpx = this.snakeBodyList[0].x + this.speed;
tween.to({ x: tmpx, y: tmpy }, during);
}
}
}
public getHead() {
return this.snakeBodyList[0];
}
}
关键字词:EGRET##