上一篇写的贪食蛇的重力感应控制版,这一篇就讲下触摸控制版吧。额,也不知道写了那个贪食蛇教程究竟有没有获得沈老师的书,假设没有的话,看我不拿西瓜刀砍掉 偶尔E往事 的那啥!
重力版链接:
触摸版与重力版的代码大部分都一样,不一样的地方就是要将打开重力感应换成创建一个触摸监听 。步骤例如以下:1、在GameLayer的init()中创建一个触摸监听事件
- //初始化游戏层
- bool GameLayer::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- auto labhelp = LabelTTF::create("this is game","宋体",15);
- labhelp->setPosition(Point(0,340));
- this->addChild(labhelp);
- auto labback = LabelTTF::create("MainMenu","宋体",15);
- auto miback = MenuItemLabel::create(labback,[](Ref* sender)
- {
- Director::getInstance()->replaceScene(HelloWorld::createScene());
- });
- miback->setPosition(Point(360,200));
- //接受触摸事件
- auto listener = EventListenerTouchOneByOne::create();
- listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定触摸的回调函数
- listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
- listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//将listener放入事件托付中
- //初始化蛇头坐标和食物的坐标
- sHead = new SnakeNode();
- sHead->row = rand()%10;
- sHead->col = rand()%10;
- //初始化食物的坐标
- sFood = new SnakeNode();
- sFood->row = rand()%10;
- sFood->col = rand()%10;
- //运行定时任务
- this->schedule(schedule_selector(GameLayer::logic01),0.5);
- return true;
- }
2、在touchBegan()中检測触摸的位置,
- bool GameLayer::onTouchBegan(Touch* touch, Event *event)
- {
- auto p = touch->getLocation();//获取当前的触点
- int nowrow; //当前用户触摸的行
- nowrow = ((int)p.y)/32;
- int nowcol; //当前用户触摸的列
- nowcol = ((int)p.x)/32;
- //假设触点的Y坐标相对于蛇头的Y坐标 大于 触点的X坐标相对于蛇头的X坐标,那么是上下移动,否则左右移动
- if( abs(nowrow - sHead->row) > abs(nowcol - sHead->col)) //上下移动
- {
- if(nowrow > sHead->row)
- {
- sHead->dir = DIR_DEF::UP;
- }
- else
- {
- sHead->dir=DIR_DEF::DOWN;
- }
- }
- else //左右移动
- {
- if(nowcol > sHead->col)
- {
- sHead->dir=DIR_DEF::RIGHT;
- }
- else
- {
- sHead->dir=DIR_DEF::LEFT;
- }
- }
- CCLOG("rand %d",rand());
- CCLOG("you touchbegan%f,%f",p.x,p.y);
- return true;
- }
3、第三点仅仅是凑字数的。感觉写这么点东西对不起大家。所以祝大伙身体健康,万事如意,马年吉祥,合家欢乐, 幸福安康,大吉大利,早生贵子。早日成双,摆脱右手...啊呸,脱离单身!尊重原创,转载请注明来源:
重力版链接:
触摸版与重力版的代码大部分都一样,不一样的地方就是要将打开重力感应换成创建一个触摸监听 。步骤例如以下:1、在GameLayer的init()中创建一个触摸监听事件
- //初始化游戏层
- bool GameLayer::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- auto labhelp = LabelTTF::create("this is game","宋体",15);
- labhelp->setPosition(Point(0,340));
- this->addChild(labhelp);
- auto labback = LabelTTF::create("MainMenu","宋体",15);
- auto miback = MenuItemLabel::create(labback,[](Ref* sender)
- {
- Director::getInstance()->replaceScene(HelloWorld::createScene());
- });
- miback->setPosition(Point(360,200));
- //接受触摸事件
- auto listener = EventListenerTouchOneByOne::create();
- listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定触摸的回调函数
- listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
- listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//将listener放入事件托付中
- //初始化蛇头坐标和食物的坐标
- sHead = new SnakeNode();
- sHead->row = rand()%10;
- sHead->col = rand()%10;
- //初始化食物的坐标
- sFood = new SnakeNode();
- sFood->row = rand()%10;
- sFood->col = rand()%10;
- //运行定时任务
- this->schedule(schedule_selector(GameLayer::logic01),0.5);
- return true;
- }
2、在touchBegan()中检測触摸的位置,
- bool GameLayer::onTouchBegan(Touch* touch, Event *event)
- {
- auto p = touch->getLocation();//获取当前的触点
- int nowrow; //当前用户触摸的行
- nowrow = ((int)p.y)/32;
- int nowcol; //当前用户触摸的列
- nowcol = ((int)p.x)/32;
- //假设触点的Y坐标相对于蛇头的Y坐标 大于 触点的X坐标相对于蛇头的X坐标,那么是上下移动,否则左右移动
- if( abs(nowrow - sHead->row) > abs(nowcol - sHead->col)) //上下移动
- {
- if(nowrow > sHead->row)
- {
- sHead->dir = DIR_DEF::UP;
- }
- else
- {
- sHead->dir=DIR_DEF::DOWN;
- }
- }
- else //左右移动
- {
- if(nowcol > sHead->col)
- {
- sHead->dir=DIR_DEF::RIGHT;
- }
- else
- {
- sHead->dir=DIR_DEF::LEFT;
- }
- }
- CCLOG("rand %d",rand());
- CCLOG("you touchbegan%f,%f",p.x,p.y);
- return true;
- }
3、第三点仅仅是凑字数的。感觉写这么点东西对不起大家。所以祝大伙身体健康,万事如意,马年吉祥,合家欢乐, 幸福安康,大吉大利,早生贵子。早日成双,摆脱右手...啊呸,脱离单身!尊重原创,转载请注明来源: