Snake *_head_1= new Snake(500.0, 300.0, 2,true);
this->addChild(_head_1->_snake_body_sprite,3);
_head_1->_snake_body_sprite->setRotation(-90);
snake.push_back(_head_1);
//设置蛇身体图片 通过对象来创建
for(int a=1; a<3;a++)
{
Snake *sna2 =new Snake(500+a*20,300);
this->addChild(sna2->_snake_body_sprite,2);
snake.push_back(sna2);
}
而且在init中注冊触屏事件。
接下来我把屏幕分为了四块 而且当点击响应块的时候让蛇头进行旋转 改变dir的值 (事实上是能够改为用手势的)
bool HelloWorld::onTouchBegan(Touch* touch,Event *event){
if(bar)
{
Point touchpoint = touch->getLocation(); //获取点击之后的点的坐标
printf("touchpoint.x = %f, touchpoint.y =%f",touchpoint.x,touchpoint.y);
float touch_x =touchpoint.x;
float touch_y =touchpoint.y;
if(touch_x >= 0 && touch_x < 350){ //点击了左側的区域 设置蛇头方向为2 上0 下1 左 2,右 3
if(sn_head->_snake_dir != 3){
switch (sn_head->_snake_dir) {
case 0:sn_head->_snake_body_sprite->setRotation(-90);break;
case 1:sn_head->_snake_body_sprite->setRotation(-90);break;
case 2:break;
case 3:break;
default:break;
}
sn_head->_snake_dir = 2; //左
}
}
else if (touch_x >= 350 && touch_x < 785 && touch_y< 320){
if(sn_head->_snake_dir != 0){
switch (sn_head->_snake_dir) {
case 0: break;
case 1: break;
case 2:sn_head->_snake_body_sprite->setRotation(180);break;
case 3:sn_head->_snake_body_sprite->setRotation(180);break;
default:break;
}
sn_head->_snake_dir = 1;
}
}
else if(touch_x >= 350 && touch_x < 785 && touch_y >=320){
if(sn_head->_snake_dir != 1){
switch (sn_head->_snake_dir) {
case 0: break;
case 1: break;
case 2:sn_head->_snake_body_sprite->setRotation(0); //sn_head->_snake_body_sprite->setPosition(Point(sn_head->_snake_x,sn_head->_snake_y+=20));break; //它是相对于原始图片旋转的不是旋转过的图片旋转地
case 3:sn_head->_snake_body_sprite->setRotation(0);break;
default:break;
}
sn_head->_snake_dir = 0;
}
}
else if(touch_x >=785){
if(sn_head->_snake_dir != 2){
switch (sn_head->_snake_dir) {
case 0:sn_head->_snake_body_sprite->setRotation(90);break;
case 1:sn_head->_snake_body_sprite->setRotation(90);break;
case 2: break;
case 3: break;
default:break;
}
sn_head->_snake_dir =3;
}
}
}
}
让蛇移动起来。 这里用了一个二维数组用来存储snake的全部坐标信息。
然后当蛇头移动以后。遍历容器中的全部对象除了蛇头,把二维数组中的x,y值赋给遍历的对象的X,Y。
如图 mac不知道画图工具在哪自己用PS随便画的:
void HelloWorld::body(float bo) {
int length_snake = (int)snake.size();
int _a_2array[length_snake][2];
for(int i=0;i<snake.size();i++)
{
Snake *snake_array;
snake_array = snake.at(i);
_a_2array[i][0] = snake_array->_snake_x;
_a_2array[i][1] = snake_array->_snake_y;
}
if(sn_head->life)
{
switch (sn_head->_snake_dir) {
case 0:sn_head->_snake_y+=22;
break;
case 1:sn_head->_snake_y-=22;
break;
case 2:sn_head->_snake_x-=22;
break;
case 3:sn_head->_snake_x+=22;
break;
}
sn_head->_snake_body_sprite->setPosition(Point(sn_head->_snake_x,sn_head->_snake_y));
if(length_snake >1){
for(int k =1; k <snake.size();k++){
Snake *snake_body;
snake_body = snake.at(k);
snake_body->_snake_x= _a_2array[k-1][0];
snake_body->_snake_y= _a_2array[k-1][1];
snake_body->_snake_body_sprite->setPosition(Point(snake_body->_snake_x,snake_body->_snake_y));
}
}
}
}
float HelloWorld::impact_round(float x1,float y1,float x2,float y2)
{
float xie = sqrt(pow(abs(x1-x2),2)+pow(abs(y1-y2),2));
return xie;
}
对了昨晚上把之前做过的flappybird移植到了IOS上 可是没有做屏幕适配就发张图上来吧: