Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(三)

前端之家收集整理的这篇文章主要介绍了Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

init函数代码

bool HelloWorld::init()
{
srand(time(NULL));//随机种子

brush=1;//初始化画笔为正常的
lv=1;//初始化难度为初级
Cantouch=true;
winSize=CCDirector::sharedDirector()->getVisibleSize();


CCSprite *bg=CCSprite::create("bg.png");
bg->setPosition(VisibleRect::center());
this->addChild(bg);

gameinit(lv);//根据难度初始化场景



//初级按钮
button1item = CCMenuItemImage::create(
"button1.png",
"button1.png",
this,
menu_selector(HelloWorld::Menu_button));
button1item->setPosition(VisibleRect::leftTop()+ccp(100,-30));
button1item->setTag(1);
CCMenu* button1_menu = CCMenu::create(button1item,NULL);
button1_menu->setPosition(CCPointZero);
this->addChild(button1_menu,0);


//中级按钮
button2item = CCMenuItemImage::create(
"button2.png",
"button2.png",
menu_selector(HelloWorld::Menu_button));
button2item->setPosition(VisibleRect::leftTop()+ccp(200,-30));
button2item->setTag(2);
CCMenu* button2_menu = CCMenu::create(button2item,NULL);
button2_menu->setPosition(CCPointZero);
this->addChild(button2_menu,0);


//高级按钮
button3item = CCMenuItemImage::create(
"button3.png",
"button3.png",
menu_selector(HelloWorld::Menu_button));
button3item->setPosition(VisibleRect::leftTop()+ccp(300,-30));
button3item->setTag(3);
CCMenu* button3_menu = CCMenu::create(button3item,NULL);
button3_menu->setPosition(CCPointZero);
this->addChild(button3_menu,0);

//左边的勾
check1=CCSprite::create("check.png");
check1->setPosition(button1item->getPosition());
this->addChild(check1);


//重新开始按钮
CCSprite *restart=CCSprite::create("13.png");
CCMenuItemLabel *item_restart=CCMenuItemLabel::create(restart,this,callfuncO_selector(HelloWorld::Menu_restart));
CCMenu* menu_restart=CCMenu::create(item_restart,NULL);
menu_restart->setPosition(VisibleRect::leftTop()+ccp(400,-30));
this->addChild(menu_restart);



//画笔1
brush1item = CCMenuItemImage::create(
"9.png",
"9.png",
menu_selector(HelloWorld::Menu_brush));
brush1item->setPosition(VisibleRect::leftTop()+ccp(600,-30));
brush1item->setTag(1);
CCMenu* brush1_menu = CCMenu::create(brush1item,NULL);
brush1_menu->setPosition(CCPointZero);
this->addChild(brush1_menu,0);


//画笔2
brush2item = CCMenuItemImage::create(
"11.png",
"11.png",
menu_selector(HelloWorld::Menu_brush));
brush2item->setPosition(VisibleRect::leftTop()+ccp(650,-30));
brush2item->setTag(2);
CCMenu* brush2_menu = CCMenu::create(brush2item,NULL);
brush2_menu->setPosition(CCPointZero);
this->addChild(brush2_menu,0);


//画笔3
brush3item = CCMenuItemImage::create(
"12.png",
"12.png",
menu_selector(HelloWorld::Menu_brush));
brush3item->setPosition(VisibleRect::leftTop()+ccp(700,-30));
brush3item->setTag(3);
CCMenu* brush3_menu = CCMenu::create(brush3item,NULL);
brush3_menu->setPosition(CCPointZero);
this->addChild(brush3_menu,0);


//右边的勾
check2=CCSprite::create("check.png");
check2->setPosition(brush1item->getPosition());
this->addChild(check2);


//雷数文本
mine_Number_label=CCLabelTTF::create(CCString::createWithFormat("%d",mine_Number)->m_sString.c_str(),"",32);
mine_Number_label->setPosition(VisibleRect::leftTop()+ccp(750,-30));
this->addChild(mine_Number_label);


//提示
tips=CCLabelTTF::create();
tips->setPosition(VisibleRect::leftTop()+ccp(850,-30));
this->addChild(tips);


return true;
}


Menu_brush函数代码

void HelloWorld::Menu_brush(CCObject* pSender)
{
CCMenuItemImage*item=(CCMenuItemImage*)pSender;//这里用CCMenu,可能会出错。 触摸对应的是一个item
switch(item->getTag())
{
case 1:
{
brush=1;
check2->setPosition(brush1item->getPosition());
break;
}


case 2:
{
brush=2;
check2->setPosition(brush2item->getPosition());
break;
}


case 3:
{
brush=3;
check2->setPosition(brush3item->getPosition());
break;
}


}
}



Menu_restart函数代码

void HelloWorld::Menu_restart(CCObject* pSender)
{
tips->setString("");//清空提示
Cantouch=true;//重新开始后,触摸设置为可触摸
this->removeChild(spLayer);//删除原来的棋盘层
gameinit(lv);//初始化棋盘
mine_Number_label->setString(CCString::createWithFormat("%d",mine_Number)->m_sString.c_str());//初始化雷数
}



Menu_button函数代码

void HelloWorld::Menu_button(CCObject* pSender)
{
CCMenuItemImage*item=(CCMenuItemImage*)pSender;//这里用CCMenu,可能会出错。 触摸对应的是一个item
switch(item->getTag())
{
case 1:
{
lv=1;
check1->setPosition(button1item->getPosition());
break;
}


case 2:
{
lv=2;
check1->setPosition(button2item->getPosition());
break;
}


case 3:
{
lv=3;
check1->setPosition(button3item->getPosition());
break;
}


}




}




剩余的函数代码

void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{


}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent)
{


}
void HelloWorld::onEnterTransitionDidFinish()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,true);
}

CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; }

猜你在找的Cocos2d-x相关文章