#include "TollgateScene.h"
#include "BackgroundLayer.h"
#include "ObjectTag.h"
#include "Player.h"
#include "Monster.h"
Scene* TollgateScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
//micro gravity
Vect gravity(0.0f, -0.5f);
scene->getPhysicsWorld()->setGravity(gravity);
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
//create a bordery
auto size = Director::getInstance()->getVisibleSize();
//create a box
auto box = PhysicsBody::createEdgeBox(size, PHYSICSBODY_MATERIAL_DEFAULT, 3);
//create a node
auto node = Node::create();
node->setPosition(size.width / 2, size.height / 2);
node->setPhysicsBody(box);
auto backgroundLayer = BackgroundLayer::create();
scene->addChild(backgroundLayer, 0);
auto monsterLayer = MonsterLayer::create();
scene->addChild(monsterLayer, 1);
// 'layer' is an autorelease object
auto layer = TollgateScene::create();
// add layer as a child to scene
scene->addChild(node);
scene->addChild(layer, 10);
layer->m_backgroundLayer = backgroundLayer;
layer->m_monsterLayer = monsterLayer;
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool TollgateScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
m_player = Player::create();
m_player->setPosition(Point(visibleSize.width * 0.5f, visibleSize.height * 0.85f));
this->addChild(m_player, 5);
this->createUI();
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = CC_CALLBACK_1(TollgateScene::onContactBegin, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
this->schedule(schedule_selector(TollgateScene::logic));
return true;
}
bool TollgateScene::onContactBegin(PhysicsContact& contact)
{
auto nodeA = (Sprite*)contact.getShapeA()->getBody()->getNode();
auto nodeB = (Sprite*)contact.getShapeB()->getBody()->getNode();
if (nodeA == NULL || nodeB == NULL)
{
return true;
}
Node* playerNode = NULL;
Node* other = NULL;
if (nodeA->getTag() == ObjectTag_Player)
{
playerNode = nodeA;
other = nodeB;
}
else if (nodeB->getTag() == ObjectTag_Player)
{
playerNode = nodeB;
other = nodeA;
}
else
{
return true;
}
Player* player = (Player*)playerNode;
if (other->getTag() == ObjectTag_Border)
{
player->beAtked(-1);
log("player cur HP:%d", player->getiHP());
}
else if (other->getTag() == ObjectTag_Monster)
{
/* 碰撞到怪物,根据怪物的属性进行操作 */
Monster* monster = (Monster*)other;
if (monster->getiAtk() != 0)
{
/* 攻击玩家(可能是加血) */
player->beAtked(monster->getiAtk());
/* 碰撞之后,怪物消失 */
monster->removeFromParent();
}
}
return true;
}
void TollgateScene::logic(float dt)
{
m_backgroundLayer->logic(dt);
m_monsterLayer->logic(dt);
}
void TollgateScene::createUI()
{
//auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("OprUI_1.ExportJson");
//auto UI = cocostudio::NodeReader:getInstance():createNode("OprUI_1.ExportJson");
auto UI = cocostudio::timeline::NodeReader::getInstance()->createNode("OprUI_1.ExportJson");
this->addChild(UI);
//Create button
auto leftBtn = (Button*)UI->getChildByName("leftBtn");
auto rightBtn = (Button*)UI->getChildByName("rightBtn");
auto quickMoveBtn = (Button*)UI->getChildByName("quickMoveBtn");
//childeBtn->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent,this));
leftBtn->addTouchEventListener(this, toucheventselector(TollgateScene::moveToLeft));
rightBtn->addTouchEventListener(this, toucheventselector(TollgateScene::moveToRight));
quickMoveBtn->addTouchEventListener(this, toucheventselector(TollgateScene::quickMove));
//Button* rightBtn = (Button*)Helper::seekWidgetByName(UI, "rightBtn");
//Button* quickMoveBtn = (Button*)Helper::seekWidgetByName(UI, "quickMoveBtn");
//Button* leftBtn = (Button*)Helper::seekWidgetByName(UI, "leftBtn");
}
void TollgateScene::moveToLeft(Ref* sender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_ENDED:
m_player->moveToLeft();
break;
}
}
void TollgateScene::moveToRight(Ref* sender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_ENDED:
m_player->moveToRight();
break;
}
}
void TollgateScene::quickMove(Ref* sender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_ENDED:
m_player->quickMove();
break;
}
}
void TollgateScene::onExit()
{
Layer::onExit();
_eventDispatcher->removeEventListenersForTarget(this);
}
void TollgateScene::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
没有合适的资源?快使用搜索试试~ 我知道了~
Cocos2dx 3.2 别救我
共31个文件
png:8个
h:7个
cpp:6个
需积分: 10 9 下载量 58 浏览量
2014-09-28
23:01:27
上传
评论
收藏 294KB ZIP 举报
温馨提示
使用cocos2dx 3.2和Mac 10.9环境下编译通过的别救我源码,包括json文件的读写,cocostudio文件的Mac加载。
资源推荐
资源详情
资源评论
收起资源包目录
.zip (31个子文件)
Resources
background.jpg 96KB
monsterConf.json 417B
NoSaveMe_UI0.plist 1KB
CloseSelected.png 5KB
monster.png 3KB
fonts
Marker Felt.ttf 25KB
CloseNormal.png 6KB
HelloWorld.png 136KB
NoSaveMe_UI0.png 19KB
.DS_Store 6KB
tg1.tmx 3KB
border.png 4KB
monster.plist 2KB
OprUI_1.ExportJson 4KB
Export
OprUI
NoSaveMe_UI0.plist 1KB
NoSaveMe_UI0.png 19KB
OprUI_1.ExportJson 4KB
player.png 9KB
Classes
MosterLayer.h 689B
AppDelegate.cpp 1KB
Player.h 593B
AppDelegate.h 947B
BackgroundLayer.h 568B
TollgateScene.h 1KB
BackgroundLayer.cpp 2KB
Monster.h 683B
ObjectTag.h 286B
MosterLayer.cpp 2KB
Player.cpp 1KB
Monster.cpp 4KB
TollgateScene.cpp 6KB
共 31 条
- 1
资源评论
安全飞天狗
- 粉丝: 2
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功