#include "GameLayer.h"
#include "Player.h"
#include "DataMgr.h"
CGameLayer* CGameLayer::m_spInstance = nullptr;
CGameLayer::CGameLayer()
:m_nScore(0)
{
}
CGameLayer::~CGameLayer()
{
}
cocos2d::Scene* CGameLayer::creatScene()
{
Scene* pScene = Scene::create();
CGameLayer* pLayer = CGameLayer::getInstance();
pScene->addChild(pLayer);
return pScene;
}
bool CGameLayer::init()
{
if (!cocos2d::Layer::init())
{
return false;
}
Size sizeWin = Director::getInstance()->getVisibleSize();
//获取原点位置
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//创建背景精灵
sGateDt* pData = CDataMgr::getInstance()->getGataDtMgr()->getGateDt();
Sprite* pBg = Sprite::create(pData->strMap);
pBg->setPosition(origin.x + sizeWin.width / 2, origin.y + sizeWin.height / 2);
this->addChild(pBg);
//人物
m_pPlayer = CPlayer::create();
m_pPlayer->setAnchorPoint(Vec2::ZERO);
this->addChild(m_pPlayer);
//男人
m_pMan = Sprite::create("mannormal.png");
m_pMan->setPosition(origin.x + 350, 120);
m_pMan->setScale(0.4);
this->addChild(m_pMan);
//梨
m_pPear = Sprite::create("target.png");
m_pPear->setPosition(origin.x + 355, 220);
m_pPear->setScale(0.5);
this->addChild(m_pPear);
//墙
m_pWall = Sprite::create("Wall.png");
m_pWall->setPosition(origin.x + 450, 150);
m_pWall->setScale(0.4);
this->addChild(m_pWall);
//得分
m_Score = Label::createWithTTF("", "fonts/Marker Felt.ttf", 50);
m_Score->setPosition(origin.x + sizeWin.width / 2, 430);
this->addChild(m_Score);
//箭
m_pArrowMgr = CArrowMgr::create();
m_pArrowMgr->setAnchorPoint(Vec2::ZERO);
this->addChild(m_pArrowMgr);
this->scheduleUpdate();
return true;
}
CGameLayer* CGameLayer::getInstance()
{
if (NULL == m_spInstance)
{
m_spInstance = CGameLayer::create();
}
return m_spInstance;
}
void CGameLayer::resum()
{
m_spInstance = nullptr;
}
void CGameLayer::collision()//碰撞
{
Vector<Node*> vecArrowNode = this->getArrowMgr()->getChildren();
for (Node* pArrowNode : vecArrowNode)
{
CArrow* pArrow = static_cast<CArrow*>(pArrowNode);
Rect pearBox = m_pPear->getBoundingBox();
Rect manBox = m_pMan->getBoundingBox();
Rect arrowBox = pArrow->getSprite()->getBoundingBox();
Vec2 newMinPos = pArrow->convertToWorldSpace(Vec2(arrowBox.getMinX(), arrowBox.getMinY()));
Rect newArrowBox = Rect(newMinPos, arrowBox.size);
if (pearBox.intersectsRect(newArrowBox))//射中目标
{
CCLOG("crashPear");
pArrow->removeFromParent();
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("targethit.png");
m_pPear->setTexture(texture);
m_pPear->setScale(1.2);
m_nScore++;
this->scheduleOnce(CC_CALLBACK_0(CGameLayer::upGrade, this), 0.5,"upGrade");
break;
}
else if (manBox.intersectsRect(newArrowBox))//射到男人
{
if (pArrow->getPositionY() < 100)
{
CCLOG("leg");
pArrow->removeFromParent();
m_pPear->removeFromParent();
this->scheduleOnce(CC_CALLBACK_0(CGameLayer::addManDiedAnimation3, this), 0.005f, "Leg");
}
else if (pArrow->getPositionY()>150)
{
CCLOG("head");
pArrow->removeFromParent();
m_pPear->removeFromParent();
this->scheduleOnce(CC_CALLBACK_0(CGameLayer::addManDiedAnimation2, this), 0.005f, "Head");
}
else
{
CCLOG("body");
pArrow->removeFromParent();
m_pPear->removeFromParent();
this->scheduleOnce(CC_CALLBACK_0(CGameLayer::addManDiedAnimation1, this), 0.005f, "Body");
}
this->scheduleOnce(CC_CALLBACK_0(CGameLayer::GameOver, this), 1.0f, "GameOver");
break;
}
}
}
void CGameLayer::update(float delta)
{
this->collision();
m_Score->setString(StringUtils::format("%d", m_nScore));
}
void CGameLayer::addManDiedAnimation1()//动画1
{
Animation* pAnimationBody = Animation::create();
char szName[32];
for (int i = 1; i < 6;++i)
{
sprintf_s(szName, "mandie1_%d.png", i);
pAnimationBody->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName));
}
pAnimationBody->setDelayPerUnit(0.1f);
pAnimationBody->setRestoreOriginalFrame(false);
AnimationCache* pCache = AnimationCache::getInstance();
pCache->addAnimation(pAnimationBody, "Body");
Animate* pAnimateBody = Animate::create(pAnimationBody);
m_pMan->runAction(pAnimateBody);
}
void CGameLayer::addManDiedAnimation2()//动画2
{
Animation* pAnimationHead = Animation::create();
char szName[32];
for (int i = 1; i < 6; ++i)
{
sprintf_s(szName, "mandie2_%d.png", i);
pAnimationHead->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName));
}
pAnimationHead->setDelayPerUnit(0.1f);
pAnimationHead->setRestoreOriginalFrame(false);
AnimationCache* pCache = AnimationCache::getInstance();
pCache->addAnimation(pAnimationHead, "Head");
Animate* pAnimateHead = Animate::create(pAnimationHead);
m_pMan->runAction(pAnimateHead);
}
void CGameLayer::addManDiedAnimation3()//动画3
{
Animation* pAnimationLeg = Animation::create();
char szName[32];
for (int i = 1; i < 9; ++i)
{
sprintf_s(szName, "mandie3_%d.png", i);
pAnimationLeg->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName));
}
pAnimationLeg->setDelayPerUnit(0.1f);
pAnimationLeg->setRestoreOriginalFrame(false);
AnimationCache* pCache = AnimationCache::getInstance();
pCache->addAnimation(pAnimationLeg, "Leg");
Animate* pAnimateLeg = Animate::create(pAnimationLeg);
m_pMan->runAction(pAnimateLeg);
}
void CGameLayer::upGrade()//进入下一关
{
MoveBy* pMoveWall = MoveBy::create(0.5, Vec2(50, 0));
MoveBy* pMoveMan = MoveBy::create(0.5, Vec2(50, 0));
MoveBy* pMovePear = MoveBy::create(0.5, Vec2(50, 0));
m_pWall->runAction(pMoveWall);
m_pMan->runAction(pMoveMan);
m_pPear->runAction(pMovePear);
CCTexture2D* texture1 = CCTextureCache::sharedTextureCache()->addImage("target.png");
m_pPear->setTexture(texture1);
m_pPear->setScale(0.5);
}
void CGameLayer::GameOver()//Gameover
{
Scene* pScene = CGameover::createScene();
TransitionFadeTR* pMoveInT = TransitionFadeTR::create(1.0f, pScene);
/*this->removeFromParent();*/
this->resum();
Director::getInstance()->replaceScene(pMoveInT);
/*CCDirector::sharedDirector()->end();
exit(0);*/
}
没有合适的资源?快使用搜索试试~ 我知道了~
cocos2dx射箭游戏
共115个文件
png:80个
h:11个
cpp:11个
需积分: 49 16 下载量 34 浏览量
2018-05-10
15:05:27
上传
评论 1
收藏 11.2MB RAR 举报
温馨提示
前段时间写的射箭小游戏,射箭动作以及箭的抛物线运动以及随着运动改变状态,碰撞升级等均已实现。
资源推荐
资源详情
资源评论
收起资源包目录
cocos2dx射箭游戏 (115个子文件)
GameLayer.cpp 6KB
AppDelegate.cpp 4KB
Player.cpp 4KB
HelloWorldScene.cpp 4KB
Gamemenu.cpp 3KB
GameLogo.cpp 3KB
GameOver.cpp 1KB
StructData.cpp 1KB
Arrow.cpp 1KB
DataMgr.cpp 423B
ArrowMgr.cpp 370B
.DS_Store 6KB
AppDelegate.h 941B
Player.h 915B
GameLayer.h 849B
StructData.h 803B
Arrow.h 585B
GameLogo.h 580B
HelloWorldScene.h 465B
Gamemenu.h 451B
DataMgr.h 423B
GameOver.h 381B
ArrowMgr.h 319B
ResData.json 1KB
GateData.json 259B
華康金文體W31.ttf.meta 88B
upBody.plist 6KB
mandie3.plist 4KB
mandie2.plist 4KB
mandie2.plist 3KB
mandie1.plist 3KB
background_01.png 438KB
background_04.png 408KB
background_02.png 381KB
background_03.png 378KB
upBody08.png 327KB
upBody07.png 327KB
bg.png 315KB
mandie3.png 213KB
mandie2.png 213KB
background.png 185KB
mandie2.png 135KB
upBody.png 119KB
mandie1.png 106KB
mandie3_5.png 49KB
mandie3_3.png 49KB
mandie2_3.png 48KB
mandie3_4.png 48KB
mandie2_4.png 48KB
mandie3_7.png 48KB
mandie3_2.png 47KB
mandie2_5.png 47KB
mandie3_6.png 47KB
mandie3_8.png 47KB
mandie2_2.png 47KB
mandie1_3.png 43KB
mandie1_4.png 42KB
mandie1_5.png 42KB
mandie1_2.png 40KB
mandie3_1.png 40KB
mandie2_1.png 38KB
mandie1_1.png 37KB
upBody04.png 31KB
upBody03.png 31KB
upBody010.png 31KB
upBody09.png 31KB
upBody02.png 31KB
upBody01.png 31KB
upBody01.png 31KB
upBody09.png 31KB
upBody08.png 31KB
upBody07.png 30KB
upBody06.png 30KB
upBody06.png 30KB
upBody05.png 30KB
upBody.png 30KB
loding.png 29KB
upBody011.png 28KB
upBody10.png 28KB
mannormal.png 28KB
lodingBg.png 28KB
gameover.png 26KB
DownBody.png 25KB
morenormal.png 24KB
nextnormal.png 24KB
startnormal.png 24KB
morehover.png 24KB
complete.png 24KB
nexthover.png 23KB
starthover.png 23KB
contnormal.png 23KB
youmis.png 23KB
conthover.png 23KB
background_03Bk.png 22KB
background_01Bk.png 22KB
background_04Bk.png 22KB
mainmenunormal.png 21KB
Arrow.png 21KB
mainmenuhover.png 21KB
target.png 21KB
共 115 条
- 1
- 2
资源评论
阳光被风吹散2020
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功