话不多说,直接上代码,不喜勿喷,谢谢。
class Fruits:public Layer//游戏的场景
{
public:
static Scene* createScene();
bool init() override;
CREATE_FUNC(Fruits);
void onEnter()override;
void onExit()override;
bool onTouchBegan(Touch*touch,Event *unused_event);
void onTouchMoved(Touch*touch,Event *unused_event);
void onTouchEnded(Touch*touch,Event *unused_event);
void onTouchCancelled(Touch*touch,Event *unused_event);
public:
Sprite*wan;
int num;
Label*sijian1;
};
Scene* Fruits::createScene()
{
auto scene=Scene::create();
auto layer=Fruits::create();
scene->addChild(layer);
return scene;
}
bool Fruits::init()
{
if (!Layer::init())
{
return false;
}
auto beijing=Sprite::create("beijin.png");
beijing->setContentSize(Size(320,480));
beijing->setPosition(Vec2(160, 240));
this->addChild(beijing);
auto jisuqi=Label::createWithSystemFont("倒计时:","",30);
jisuqi->setColor(Color3B::BLUE);
jisuqi->setPosition(Vec2(220, 450));
this->addChild(jisuqi);
auto sijian=Label::createWithSystemFont("00","",30);
sijian->setColor(Color3B::BLUE);
sijian->setPosition(Vec2(290, 450));
this->addChild(sijian);
auto jisuqi1=Label::createWithSystemFont("分数:","",30);
jisuqi1->setColor(Color3B::BLUE);
jisuqi1->setPosition(Vec2(30, 450));
this->addChild(jisuqi1);
sijian1=Label::createWithSystemFont("0","",30);
sijian1->setColor(Color3B::BLUE);
sijian1->setPosition(Vec2(100, 450));
this->addChild(sijian1);
num=0;
auto jisuqi10=Button::create ();
jisuqi10->setColor(Color3B::BLUE);
jisuqi10->setTitleText("开始");
jisuqi10->setTitleFontSize(50);
jisuqi10->setPosition(Vec2(160, 240));
this->addChild(jisuqi10,10);
jisuqi10->addClickEventListener([=](Ref*){
wan=Sprite::create("123123.png");
wan->setPosition(160, 100);
this->addChild(wan,3);
//计时器
schedule([=](float a)
{
int b1=rand()%5;
//创造水果
auto shuiguo1=Shuiguolei::create(b1);
this->addChild(shuiguo1,100);
}, 2,"www1" );
//倒计时
schedule([=](float a)
{
static int b=60;
--b;
string str=StringUtils::format("%d",b);
sijian->setString(str);
if (0==b)
{ unschedule("www1");
unschedule("www");
b=60;
string str11=StringUtils::format("分数:%d",num);
MessageBox(str11.c_str(),"得分情况");
auto scene1=Fruits::createScene();
Director::getInstance()->replaceScene(scene1);
}
}, 1,"www" );
jisuqi10->removeFromParentAndCleanup(true);
});
return true;
}
void Fruits::onExit()
{
Director::getInstance()->getEventDispatcher()->removeEventListenersForTarget(this);
Layer::onExit();
}
void Fruits::onEnter()
{
Layer::onEnter();
auto touchListener=EventListenerTouchOneByOne::create();//触摸监听
touchListener->onTouchBegan=CC_CALLBACK_2(Fruits::onTouchBegan, this);
touchListener->onTouchMoved=CC_CALLBACK_2(Fruits::onTouchMoved, this);
touchListener->onTouchEnded=CC_CALLBACK_2(Fruits::onTouchEnded, this);
touchListener->onTouchCancelled=CC_CALLBACK_2(Fruits::onTouchCancelled, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority (touchListener,this);//注册该监听
}
bool Fruits::onTouchBegan(Touch*touch,Event *unused_event)
{
log("Touch Began\r\n");
Vec2 touchPoint =touch->getLocation();
log("x:%f,y%f",touchPoint.x,touchPoint.y);
return true;
}
void Fruits::onTouchMoved(Touch*touch,Event *unused_event)
{
wan->setPosition(Vec2(touch->getLocation()));
auto vecManager = VecManager1::getInstance();//容器对象
for (auto diglett : vecManager->getShuiguoleiVec())//
{
auto _diglett = (Shuiguolei*)diglett;
if (_diglett->getIsAnt()==true)
{
continue;
}
auto rect = _diglett->getBoundingBox();
// if (rect.containsPoint(touch->getLocation()))
if (rect.intersectsRect(wan->getBoundingBox()))
{
_diglett->setHit(true);
num=num+10;
string str10=StringUtils::format("%d",num);
sijian1->setString(str10);
_diglett->removeFromParentAndCleanup(true);
}
}
}
void Fruits::onTouchEnded(Touch*touch,Event *unused_event)
{
Vec2 touchPoint =touch->getStartLocation();
log("Touch Ended\r\n");
}
void Fruits::onTouchCancelled(Touch*touch,Event *unused_event)
{
long("Touch Cancelled\r\n");
}
class Shuiguolei:public Sprite//水果类
{
public:
static Shuiguolei* create(int type);
virtual bool init(int type);
//用来判断 水果是否已经删除过一次
bool getIsAnt()
{
return isHit;
}
void setHit(bool yes)
{
this->isHit=yes;
}
private://没用
bool isHit;
};
#include "shuiguolei.hpp"
#include "VecManager1.hpp"
Vec2 z[10]={Vec2(20, 500),Vec2(40, 500),Vec2(60, 500),Vec2(80, 500),Vec2(120, 500),Vec2(150, 500),Vec2(200, 500),Vec2(232, 500),Vec2(260, 500),Vec2(300, 500)};
Shuiguolei* Shuiguolei::create(int type)
{
Shuiguolei* shuiguolei = new Shuiguolei();
if ( shuiguolei && shuiguolei->init(type))
{
shuiguolei->autorelease();
return shuiguolei;
}
CC_SAFE_DELETE( shuiguolei);
return nullptr;
}
bool Shuiguolei::init(int type)
{
if (!Sprite::init())
{
return false;
}
isHit=false;
int b2=rand()%10;
string str1=StringUtils::format("f%d.png",type);
this->setTexture(str1);
this->setPosition(Vec2(z[b2]));
auto yidong=MoveBy::create(2,Vec2(0,-550));
this->runAction(yidong);
VecManager1::getInstance()->addShuiguolei(this);
return true;
}
class VecManager1
{
public:
static VecManager1*getInstance()
{
if(vecManager==nullptr)
{
vecManager=new VecManager1();
}
return vecManager;
}
void addShuiguolei(Shuiguolei*shuiguolei);
void deleteShuiguolei(Shuiguolei*shuiguolei);
Vector<Shuiguolei*>&getShuiguoleiVec();
int getShuiguoleiVecSize();
Vector<Shuiguolei*>ShuiguoleiVec;
void vecClear();
public:
static VecManager1*vecManager;
};
#include "VecManager1.hpp"
VecManager1*VecManager1::vecManager=new VecManager1();
void VecManager1::addShuiguolei(Shuiguolei*shuiguolei)
{
this->ShuiguoleiVec.pushBack(shuiguolei);
}
void VecManager1::deleteShuiguolei(Shuiguolei*shuiguolei)
{
this->ShuiguoleiVec.eraseObject (shuiguolei);
shuiguolei->removeFromParentAndCleanup( true);
}
Vector<Shuiguolei*>&VecManager1::getShuiguoleiVec()
{
return this->ShuiguoleiVec;
}
int VecManager1::getShuiguoleiVecSize()
{
return this->ShuiguoleiVec.size();
}
void VecManager1::vecClear()
{
ShuiguoleiVec.clear();
}