#include "WatchOutScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace CocosDenshion;
Scene* WatchOut::createScene() {
auto scene = Scene::create();
auto layer = WatchOut::create();
scene->addChild(layer);
return scene;
}
bool WatchOut::init() {
if ( !Layer::init() )
{
return false;
}
//添加plist到缓冲池
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("watchout.plist", "watchout.png");
//预加载音效文件
preloadMusic();
//初始化View
setupView();
return true;
}
void WatchOut::setupView() {
//窗口大小
Size winSize = Director::getInstance()->getVisibleSize();
//把窗口分为5等份
float winSize_5 = winSize.height / 5;
/*
上部分占3份
-1:黑线占1像素
*/
float bgTopHeight = (winSize_5 * 3) - 1;
//下部分占2等份
float bgBottomHeight = winSize_5 * 2;
//顶部颜色
auto bgTop = LayerColor::create(color_default, winSize.width, bgTopHeight);
bgTop->setPosition(Vec2(0, bgBottomHeight + 1));
bgTop->setTag(tag_bg_top);
bgTop->setAnchorPoint(Vec2::ZERO);
this->addChild(bgTop, 1);
//中间黑色条
auto lineMiddle = LayerColor::create(Color4B::BLACK, winSize.width, 1);
lineMiddle->setPosition(Vec2(0, bgBottomHeight));
this->addChild(lineMiddle, 0);
//底部颜色
auto bgBottom = LayerColor::create(color_default_b, winSize.width, bgBottomHeight);
bgBottom->setPosition(Vec2::ZERO);
bgBottom->setAnchorPoint(Vec2::ZERO);
bgBottom->setTag(tag_bg_bottom);
this->addChild(bgBottom, 0);
auto cache = SpriteFrameCache::getInstance();
//得分背景
auto scoreBg = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("score_bg.png"));
scoreBg->setPosition(Vec2(bgTop->getContentSize().width / 2, bgTop->getPositionY() + bgTop->getContentSize().height / 2));
scoreBg->setScale(0.6, 0.6);
scoreBg->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
scoreBg->setTag(tag_score_bg);
this->addChild(scoreBg, 1);
//得分Label
auto label = LabelTTF::create("0", "Arial", 100);
label->setPosition(Vec2(scoreBg->getContentSize().width / 2, scoreBg->getContentSize().height / 2));
label->setColor(color_default_cc3);
label->setTag(tag_score_label);
scoreBg->addChild(label);
//开始游戏按钮
auto startMenuItem = MenuItemImage::create();
startMenuItem->setNormalSpriteFrame(cache->getSpriteFrameByName("start_game.png"));
startMenuItem->setSelectedSpriteFrame(cache->getSpriteFrameByName("start_game.png"));
startMenuItem->setPosition(Vec2(bgBottom->getContentSize().width / 2, bgBottom->getContentSize().height / 2));
startMenuItem->setCallback(CC_CALLBACK_1(WatchOut::startGame, this));
startMenuItem->setScale(0.8, 0.8);
auto menu = Menu::create(startMenuItem, NULL);
menu->setPosition(Vec2::ZERO);
menu->setTag(tag_start_game);
this->addChild(menu, 2);
//左边圆圆
auto yuanyuan = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("yuanyuan.png"));
yuanyuan->setPosition(Vec2(2, 2));
yuanyuan->setAnchorPoint(Vec2::ZERO);
yuanyuan->setTag(tag_yuanyuan);
yuanyuan->setVisible(false);
bgTop->addChild(yuanyuan);
//右边方方
auto fangfang = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("fangfang.png"));
fangfang->setPosition(Vec2(winSize.width - fangfang->getContentSize().width - 2, 2));
fangfang->setAnchorPoint(Vec2::ZERO);
fangfang->setTag(tag_fangfang);
fangfang->setVisible(false);
bgTop->addChild(fangfang);
//初始化跳跃高度
_jumpHeight = yuanyuan->getContentSize().height * 0.9;
//初始化游戏状态
_gameState = GAME_STATE_START;
//初始化游戏开始速度(方方/圆圆跑的速度)
_runSpeed = 2.0f;
//初始化分数
_score = 0;
//开启每帧调用
scheduleUpdate();
//添加屏幕触摸事件
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(WatchOut::onTouchBegan, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void WatchOut::startGame(Ref* pSender) {
//播放音效
SimpleAudioEngine::getInstance()->playEffect(music_button_touch);
//获得顶部背景
auto top = this->getChildByTag(tag_bg_top);
//获得方方/圆圆
Sprite* yuanyuan = (Sprite*)top->getChildByTag(tag_yuanyuan);
Sprite* fangfang = (Sprite*)top->getChildByTag(tag_fangfang);
/* 重置参数 */
_runSpeed = 2.0f;
_score = 0;
auto scoreBg = this->getChildByTag(tag_score_bg);
//分数Label
LabelTTF* scoreLabel = (LabelTTF*)scoreBg->getChildByTag(tag_score_label);
scoreLabel->setString(StringUtils::format("%i", 0));
//顶部颜色
LayerColor* topBg = (LayerColor*)this->getChildByTag(tag_bg_top);
//底部颜色
LayerColor* bottomBg = (LayerColor*)this->getChildByTag(tag_bg_bottom);
topBg->setColor(ccc3(205, 225, 235));
bottomBg->setColor(ccc3(63, 88, 100));
scoreLabel->setColor(color_default_cc3);
//方方/圆圆重置
resetRunner();
auto startGame = this->getChildByTag(tag_start_game);
//隐藏[开始游戏]按钮
startGame->setVisible(false);
}
void WatchOut::update(float dt) {
auto top = this->getChildByTag(tag_bg_top);
Sprite* yuanyuan = (Sprite*)top->getChildByTag(tag_yuanyuan);
Sprite* fangfang = (Sprite*)top->getChildByTag(tag_fangfang);
if(_gameState == GAME_STATE_PLAYING) {
//移动方方/圆圆
yuanyuan->setPositionX(yuanyuan->getPositionX() + _runSpeed);
fangfang->setPositionX(fangfang->getPositionX() - _runSpeed);
auto yyBox = yuanyuan->getBoundingBox();
auto ffBox = fangfang->getBoundingBox();
//碰撞检测
if(yyBox.intersectsRect(ffBox)) {
_gameState = GAME_STATE_OVER;
SimpleAudioEngine::getInstance()->playEffect(music_player_die);
auto startGame = this->getChildByTag(tag_start_game);
startGame->setVisible(true);
/* 爆炸效果 */
auto pExpVec = Vec2::ZERO;
auto pExpColor = Color4F::BLUE;
auto pExpTextureRect = Rect::ZERO;
//根据当前TAG来决定爆炸对象
if(_currentTag == yuanyuan->getTag()) {
yuanyuan->setVisible(false);
pExpVec = Vec2(
yuanyuan->getPositionX() + yuanyuan->getContentSize().width / 4,
yuanyuan->getPositionY() + yuanyuan->getContentSize().height / 4);
pExpColor = Color4F(57, 99, 207, 255);
pExpTextureRect = Rect(2,2,9,9);
}else {
fangfang->setVisible(false);
pExpVec = Vec2(
fangfang->getPositionX() + fangfang->getContentSize().width / 4,
fangfang->getPositionY() + fangfang->getContentSize().height / 4);
pExpColor = Color4F(212, 74, 123, 255);
pExpTextureRect = Rect(13,2,9,9);
}
auto pExplostion = ParticleExplosion::create();
pExplostion->setStartSize(3);
pExplostion->setStartColor(pExpColor);
pExplostion->setEndColor(pExplostion->getStartColor());
pExplostion->setLife(0.1);
pExplostion->setTotalParticles(100);
pExplostion->setRotationIsDir(false);
pExplostion->setAutoRemoveOnFinish(true);
pExplostion->setPosition(pExpVec);
pExplostion->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("watchout.png"), pExpTextureRect);
pExplostion->setScale(0.5);
top->addChild(pExplostion, 10);
}else if(ffBox.getMaxX() < yyBox.getMinX() &&
ffBox.getMinY() == 2 &&
yyBox.getMinY() == 2) {//如果没有碰撞 && 方方/圆圆都没处于跳跃状态
_gameState = GAME_STATE_RESET; //游戏重置
SimpleAudioEngine::getInstance()->playEffect(music_score);
yuanyuan->setVisible(false);
fangfang->setVisible(false);
_score ++;
//分数Label改变
auto scoreBg = this->getChildByTag(tag_score_bg);
LabelTTF* scoreLabel = (LabelTTF*)scoreBg->getChildByTag(tag_score_label);
scoreLabel->setString(StringUtils::format("%i", _score));
//分数增加(等级增加),速度增加,背景颜色改变
if(_score % 6 == 0) {
_runSpeed += 0.5;
int bgLevel = _score / 6;
LayerColor* topBg = (LayerColor*)this->getChildByTag(tag_bg_top);
LayerColor* bottomBg = (LayerColor*)this->getChildByTag(tag_bg_bottom);
if(bgLevel == 1) {
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
WatchOut.zip (18个子文件)
Resources
sfx_player_jump.wav 13KB
sfx_score.wav 19KB
CloseSelected.png 5KB
watchout.plist 5KB
sfx_player_die.wav 96KB
fonts
Marker Felt.ttf 25KB
CloseNormal.png 6KB
HelloWorld.png 136KB
sfx_button.wav 19KB
sfx_highscore.wav 455KB
watchout.png 10KB
Classes
AppDelegate.cpp 1KB
HelloWorldScene.h 612B
AppDelegate.h 947B
HelloWorldScene.cpp 3KB
resource.h 1KB
WatchOutScene.cpp 11KB
WatchOutScene.h 846B
共 18 条
- 1
54xuu
- 粉丝: 23
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页