//
// MainLayer.cpp
// GoldMiner
//
// Created by user on 15/10/1.
//
//
#include "MainGameLayer.h"
#include "GameOverLayer.h"
#include "GameWinLayer.h"
#include "GameFinish.h"
#include "ShopLayer.h"
USING_NS_CC;
Miner *spriteMiner = NULL; //矿工
int level = 0; //关卡
Scene* MainGameLayer::scene(){
Scene *scene = Scene::create();
MainGameLayer *layer = MainGameLayer::create();
scene->addChild(layer);
return scene;
}
bool MainGameLayer::init(){
if(!Layer::init()){
return false;
}
//加载plist文件
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("minerAction.plist");
cache->addSpriteFramesWithFile("shop_npc.plist");
//初始化窗口尺寸、关卡
this->winSize = Director::getInstance()->getWinSize();
score = 1000;
timeGone = 0;
const char* str = __String::createWithFormat("mineBG%d.png", level + 1)->getCString();
//添加背景
Sprite *spriteBg = Sprite::create(str);
spriteBg->setPosition(Vec2(winSize.width / 2.0, winSize.height / 1.5));
this->addChild(spriteBg, -1);
//添加矿工
spriteMiner = Miner::getInstance("miner.png");
spriteMiner->spriteCatched = NULL;
spriteMiner->setPosition(Vec2(winSize.width / 2.0, winSize.height / 1.5));
this->addChild(spriteMiner);
//画线
drawgement = DrawNode::create();
this->addChild(drawgement);
//添加炸弹
if(spriteMiner->hasGoods(goods_boom)){
MenuItemImage *itemBoom = MenuItemImage::create("article_2001.png", "article_2001.png",CC_CALLBACK_0(MainGameLayer::selBoom, this));
Menu *menuBoom = Menu::create(itemBoom, NULL);
menuBoom->setPosition(Vec2(winSize.width / 2.0 + 80, winSize.height / 1.5));
menuBoom->setTag(100);
this->addChild(menuBoom);
//炸弹个数标签
this->labelBoom = Label::createWithSystemFont("", "", 16);
char str[100];
sprintf(str, "x %d", spriteMiner->getBoomCount());
this->labelBoom->setString(str);
this->labelBoom->setPosition(Vec2(winSize.width / 2.0 + 80, winSize.height / 1.5 - 25));
this->labelBoom->setColor(Color3B(255, 0, 0));
//判断炸弹的个数(个数为0,不添加到layer里,不显示)
if(0 != spriteMiner->getBoomCount()){
this->addChild(this->labelBoom);
}
}
//添加强力水
if(spriteMiner->hasGoods(goods_water)){
MenuItemImage *itemWater = MenuItemImage::create("pic/qianglishui2.png", "pic/qianglishui2.png",CC_CALLBACK_0(MainGameLayer::selWater, this));
Menu *menuWater = Menu::create(itemWater, NULL);
menuWater->setPosition(Vec2(winSize.width / 2.0 - 80, winSize.height / 1.5));
menuWater->setTag(101);
this->addChild(menuWater);
//强力水个数标签
this->labelWater = Label::createWithSystemFont("", "", 16);
char str[100];
sprintf(str, "x %d", spriteMiner->getWaterCount());
this->labelWater->setString(str);
this->labelWater->setPosition(Vec2(winSize.width / 2.0 - 80, winSize.height / 1.5 - 25));
this->labelWater->setColor(Color3B(255, 0, 0));
if(0 != spriteMiner->getWaterCount())
{
this->addChild(this->labelWater);
}
}
//添加钩子
spriteHook = Hook::create("hook.png");
spriteHook->setPosition(Vec2(winSize.width/2.0 - 2, winSize.height/1.5 - 20));
this->addChild(spriteHook);
spriteHook->setScale(1.5);
spriteHook->isBack = true;
spriteHook->setToSpeed(70.0);
spriteHook->setRotateSpeed(75.0);
spriteHook->startRotateAction();
//添加闹钟、关卡数、过关需要分数、当前得分
spriteClock = Sprite::create("clockPlate.png");
spriteClock->setPosition(Vec2(380, 260));
this->addChild(spriteClock);
this->labelClock = Label::createWithSystemFont("", "", 16);
this->labelClock->setPosition(Vec2(420, 260));
this->labelClock->setColor(Color3B(0, 0, 255));
this->addChild(this->labelClock);
Sprite *spriteLevelNumber = Sprite::create("miner_0707.png");
spriteLevelNumber->setPosition(Vec2(380, 300));
this->addChild(spriteLevelNumber);
this->labelLevel = Label::createWithSystemFont("", "", 16);
this->labelLevel->setPosition(Vec2(420, 300));
this->labelLevel->setColor(Color3B(0, 0, 255));
this->addChild(this->labelLevel);
Label *labelMoney = Label::createWithSystemFont("目前金钱:", "", 16);
labelMoney->setPosition(Vec2(250, 260));
labelMoney->setColor(Color3B(255,0,0));
this->addChild(labelMoney);
this->labelTotalScore = Label::createWithSystemFont("", "", 16);
this->labelTotalScore->setPosition(Vec2(300, 260));
this->labelTotalScore->setColor(Color3B(0, 0, 255));
this->addChild(this->labelTotalScore);
Sprite *spriteScore = Sprite::create("rmbImage.png");
spriteScore->setAnchorPoint(Vec2(0.5,0.5));
spriteScore->setPosition(Vec2(330, 260));
this->addChild(spriteScore);
Label *labelGoalMoney = Label::createWithSystemFont("通关金钱:", "", 16);
labelGoalMoney->setPosition(Vec2(250, 300));
labelGoalMoney->setColor(Color3B(255,0,0));
this->addChild(labelGoalMoney);
this->labelGoalScore = Label::createWithSystemFont("", "", 16);
this->labelGoalScore->setColor(Color3B(0,0,255));
this->labelGoalScore->setPosition(Vec2(300, 300));
this->addChild(this->labelGoalScore);
Sprite *spriteGoalNumber = Sprite::create("rmbImage.png");
spriteGoalNumber->setPosition(Vec2(330, 300));
this->addChild(spriteGoalNumber);
//退出游戏按钮
MenuItemImage *closeItem = MenuItemImage::create("CloseSelected.png","CloseNormal.png",[=](Ref *){
timeGone = 0;
this->score = 0;
Director::getInstance()->replaceScene(GameOverLayer::scene());
});
closeItem->setScale(0.6);
Menu *m = Menu::create(closeItem,NULL);
m->setPosition(Vec2(winSize.width/8,winSize.height-20));
this->addChild(m);
//触摸
//1 创建监听器
auto l = EventListenerTouchOneByOne::create();//单点触摸的监听器(目标触摸)
l->onTouchBegan = CC_CALLBACK_2(MainGameLayer::onTouchBegan,this);
l->onTouchMoved = CC_CALLBACK_2(MainGameLayer::onTouchMoved,this);
l->onTouchEnded = CC_CALLBACK_2(MainGameLayer::onTouchEnded,this);
//2 分发中心
auto d = Director::getInstance()->getEventDispatcher();
//3 注册
d->addEventListenerWithSceneGraphPriority(l, this);
this->schedule(schedule_selector(MainGameLayer::drawgline));
const int TIME[6] = {30, 35, 40, 45, 50, 60}; //每关的时间
const int NEEDSCORE[6] = {2000, 3000, 4000, 5000, 5500, 6000}; //每关通关需要的分数
this->setData(TIME[level], this->score, level, NEEDSCORE[level]);
//初始化关卡
this->initThingsByLevel(level);
this->schedule(schedule_selector(MainGameLayer::selTestTime), 1);
return true;
}
void MainGameLayer::drawgline(float t)
{
drawgement->clear();
drawgement->drawLine(Vec2(spriteMiner->getPosition().x-3, spriteMiner->getPosition().y-18), spriteHook->getPosition(), Color4F(1,0,0,1));
}
//设置参数。clock:闹钟秒数、totalScore:当前分数、level:关卡数、needScore:通关需要分数
void MainGameLayer::setData(int clock, int totalScore, int level, int needScore){
char strTemp[20];
//设置闹钟标签、分数、关卡、通关需要分数
sprintf(strTemp, "%d", clock);
this->labelClock->setString(strTemp);
sprintf(strTemp, "%d", totalScore);
this->labelTotalScore->setString(strTemp);
sprintf(strTemp, "%d", level + 1);
this->labelLevel->setString(strTemp);
sprintf(strTemp, "%d", needScore);
this->labelGoalScore->setString(strTemp);
}
//初始化关卡。level:当前等级
没有合适的资源?快使用搜索试试~ 我知道了~
黄金矿工cocos2d源代码+素材
共162个文件
png:94个
cpp:26个
h:25个
3星 · 超过75%的资源 需积分: 44 76 下载量 26 浏览量
2016-01-04
16:42:07
上传
评论 2
收藏 3.1MB ZIP 举报
温馨提示
这是一个cocos2dx黄金矿工的源代码,里面还有图片素材,供大家学习!
资源推荐
资源详情
资源评论
收起资源包目录
黄金矿工cocos2d源代码+素材 (162个子文件)
shop_npc.pvr.ccz 548KB
._shop_npc.pvr.ccz 4KB
MainGameLayer.cpp 17KB
ShopLayer.cpp 7KB
Miner.cpp 4KB
._Water.cpp 4KB
._AppDelegate.cpp 4KB
._Boom.cpp 4KB
._GameWinLayer.cpp 4KB
._GameFinish.cpp 4KB
._HelloWorldScene.cpp 4KB
._Things.cpp 4KB
._ShopLayer.cpp 4KB
._Miner.cpp 4KB
._GameOverLayer.cpp 4KB
._MainGameLayer.cpp 4KB
._Hook.cpp 4KB
._Woman.cpp 4KB
Hook.cpp 3KB
Things.cpp 3KB
Woman.cpp 3KB
HelloWorldScene.cpp 2KB
AppDelegate.cpp 2KB
GameFinish.cpp 2KB
GameWinLayer.cpp 2KB
GameOverLayer.cpp 2KB
Boom.cpp 1KB
Water.cpp 745B
Thumbs.db 11KB
.DS_Store 10KB
.DS_Store 6KB
._.DS_Store 4KB
._.DS_Store 4KB
._MainGameLayer.h 4KB
._Things.h 4KB
._GameOverLayer.h 4KB
._Water.h 4KB
._Miner.h 4KB
._Hook.h 4KB
._HelloWorldScene.h 4KB
._ShopLayer.h 4KB
._GameFinish.h 4KB
._Woman.h 4KB
._GameWinLayer.h 4KB
._Boom.h 4KB
MainGameLayer.h 2KB
Things.h 1KB
Miner.h 1KB
Hook.h 1KB
ShopLayer.h 1KB
AppDelegate.h 987B
Woman.h 775B
Boom.h 710B
Water.h 662B
GameWinLayer.h 582B
GameFinish.h 568B
GameOverLayer.h 560B
HelloWorldScene.h 555B
backMusic.mp3 435KB
._backMusic.mp3 4KB
Boom.plist 9KB
minerAction.plist 6KB
._fengye.plist 4KB
._Boom.plist 4KB
._shop_npc.plist 4KB
._minerAction.plist 4KB
shop_npc.plist 3KB
fengye.plist 3KB
mineBG5.png 310KB
mineBG3.png 282KB
shopBack.png 235KB
MainMenu.png 184KB
mineBG1.png 169KB
mineBG2.png 168KB
mineBG6.png 152KB
mineBG4.png 130KB
tableImage.png 91KB
minerAction.png 89KB
PlayMenu.png 54KB
information.png 36KB
information2.png 26KB
information1.png 25KB
shopArrow.png 21KB
Icon.png 11KB
miner.png 11KB
qianglishui.png 10KB
gold_large.png 10KB
stone_large.png 7KB
soundController2.png 7KB
soundController.png 7KB
gold_middle.png 7KB
diamond_large.png 6KB
CloseNormal.png 6KB
stone_middle.png 6KB
fengye.png 5KB
CloseSelected.png 5KB
qianglishui2.png 5KB
buyButtonSelect.png 5KB
._qianglishui2.png 5KB
secret_large.png 5KB
共 162 条
- 1
- 2
资源评论
- c魔鬼中的天使2019-09-03拿来,学习学习
tiantangmingyue
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功