cocos2dx 3.3 魂斗罗初步尝试 游戏层(暂停段时间,以后再写)。。。
2014-10-27 11:42
323 查看
//重点且复杂。。。
//控制采用了模拟方向键,长按使用定时器实现 //设计的是单点触控,败笔啊(准备以后再改) //移植到安卓下按键会有点问题,windows下正常(不知道为什么) //目前只实现了地图部分地面,主角的动作,碰撞检测 //8方向还未实现,敌人也只做了一种,子弹也只做了一种 //待实现:敌人的移动,敌人出现时机,子弹的部分完善(子弹的移动速度和随着人物移动子弹的移动) #pragma once #include "cocos2d.h" #include "Def.h" #include "Player.h" #include "Bullt.h" #include "Enemy.h" using namespace std; USING_NS_CC; class GameLayer :public Layer { public: CREATE_FUNC(GameLayer); bool init(); Player *player;//主角 CCTMXTiledMap *tmxMap;//tmx的地图 CCTMXLayer *tmxLayer; CCTMXObjectGroup *tmxObject; Size visibleSize;//常用就申明为成员变量了 //for 碰撞检测 vector<Sprite*> vector_Ground;//存放地图中的地面 vector<Bullt*> vector_PlayerBullt;//主角子弹 vector<Enemy*> vector_Enemy;//敌人 int bulltTime; void MoveBullt(); //按钮 Sprite *downButton; Sprite *leftButton; Sprite *rightButton; Sprite *upButton; Sprite *jumpButton; Sprite *bulltButton; int oldy; //end bool initWithFile(string s); void initPlayer(); void addButton(); void leftCallback(float p); void rightCallback(float p); void downCallback(float p); void upCallback(float p); void jumpCallback(float p); void jumpDownCallbcak(float p); void bulltCallback(float p); //触摸响应函数 EventListenerTouchOneByOne *listener; bool onTouchBegan(Touch *touch, Event *unused_event); void onTouchMoved(Touch *touch, Event *unused_event); void onTouchEnded(Touch *touch, Event *unused_event); bool onContactBegin(PhysicsContact& contact);//回调函数 }; #include "GameLayer.h" bool GameLayer::init() { Layer::init(); visibleSize = Director::getInstance()->getVisibleSize(); initWithFile(MAP_NAME_1); addChild(tmxMap); addButton();//添加按钮 initPlayer();//初始化主角 player->setTag(TAG_PLAYER); addChild(player); //开启触摸,固定代码 listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); listener->setSwallowTouches(true); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); //开启碰撞检测 auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameLayer::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); return true; } bool GameLayer::onContactBegin(PhysicsContact& contact)//回调函数 { CCLOG("contacting..."); Sprite *sa = (Sprite *)contact.getShapeA()->getBody()->getNode(); Sprite *sb = (Sprite *)contact.getShapeB()->getBody()->getNode(); int tagA = sa->getTag(); int tagB = sb->getTag(); if ((tagA == TAG_PLAYER && tagB == TAG_ENEMY) || (tagA == TAG_ENEMY && tagB == TAG_PLAYER))//主角与敌人碰撞则死亡 { CCLOG("player is die"); player->setSpriteFrame(SpriteFrame::create("die.png", Rect(0, 0, 32, 18))); Director::getInstance()->getEventDispatcher()->removeEventListener(listener); this->unscheduleAllSelectors(); } if ((tagA == TAG_BULLT && tagB == TAG_ENEMY))//子弹与敌人碰撞(分开写是方便移除) { removeChild(sa, true); tmxMap->removeChild(sb, true); } else if ((tagA == TAG_ENEMY && tagB == TAG_BULLT)) { removeChild(sb, true); tmxMap->removeChild(sa, true); } return true; } void GameLayer::initPlayer()//主角的初始化 { player = Player::getInstance(); player->setPosition(ccp(100, 200));//给个初始位置,会自己下落 auto body = PhysicsBody::create(); body->addShape(PhysicsShapeBox::create(player->getContentSize())); body->setDynamic(true); body->getShape(0)->setRestitution(0); body->addMoment(PHYSICS_INFINITY);//碰撞后是否转动 body->setCategoryBitmask(1); body->setCollisionBitmask(1); body->setContactTestBitmask(1); player->setPhysicsBody(body); bulltTime = BULLT_TIME;//子弹间隔时间 oldy = 0; } bool GameLayer::initWithFile(string s) { tmxMap = CCTMXTiledMap::create(s); tmxLayer = tmxMap->layerNamed("layer"); tmxObject = tmxMap->objectGroupNamed("object"); //获得地图中object的属性 auto objs = tmxObject->getObjects(); for (auto obj : objs) { ValueMap dict = obj.asValueMap(); string s = dict["name"].asString(); if (s == "ground") { Rect r; r.setRect(dict["x"].asInt(), dict["y"].asInt(), dict["width"].asInt(), dict["height"].asInt()); //r.setRect(32, 96, 32, 32);//用来测试物体位置不对的问题。。。。 Sprite *s = Sprite::create(); //s->setContentSize(r.size); auto body = PhysicsBody::create(); body->addShape(PhysicsShapeBox::create(r.size)); body->setDynamic(false); s->setPosition(r.origin.x + r.size.width / 2, r.origin.y);//加入物体后锚点会强制为中心点,开始不知道,各种错 s->setPhysicsBody(body); s->setAnchorPoint(ccp(0, 0));//锚点无效 tmxMap->addChild(s);//记得添加进场景啊。。。(这里直接加入map中,方便移动,子弹不好加入map) vector_Ground.push_back(s); } if (s=="enemy")//添加敌人 { auto dic = obj.asValueMap(); Enemy *s = Enemy::createWithFile("enemy1.png"); s->init(1, true, false); int x = dic["x"].asInt(); int y = dic["y"].asInt(); s->setPosition(x, y); auto body = PhysicsBody::create(); body->addShape(PhysicsShapeBox::create(s->getContentSize())); body->setDynamic(true); body->setCategoryBitmask(3); body->setCollisionBitmask(3); body->setContactTestBitmask(3); s->setPhysicsBody(body); s->setTag(TAG_ENEMY); tmxMap->addChild(s); vector_Enemy.push_back(s); } } return true; } void GameLayer::addButton()//添加按键 { //按键由菜单改为精灵,为了实现长按,且改为类成员变量 downButton = Sprite::create("downButton.png");// , "downButtonSelected.png", this, menu_selector(GameLayer::downCallback)); downButton->setAnchorPoint(ccp(0, 0)); downButton->setPosition(32, 0); leftButton = Sprite::create("leftButton.png");// , "leftButtonSelected.png", this, menu_selector(GameLayer::leftCallback)); leftButton->setAnchorPoint(ccp(0, 0)); leftButton->setPosition(10, downButton->getContentSize().height); rightButton = Sprite::create("rightButton.png");// , "rightButtonSelected.png", this, menu_selector(GameLayer::rightCallback)); rightButton->setAnchorPoint(ccp(0, 0)); rightButton->setPosition(leftButton->getPositionX() + leftButton->getContentSize().width + 10, downButton->getContentSize().height ); upButton = Sprite::create("upButton.png");// , "upButtonSelected.png", this, menu_selector(GameLayer::upCallback)); upButton->setAnchorPoint(ccp(0, 0)); upButton->setPosition(32, rightButton->getPositionY()+rightButton->getContentSize().height); jumpButton = Sprite::create("jumpButton.png");// , "jumpButtonSelected.png", this, menu_selector(GameLayer::jumpCallback)); jumpButton->setAnchorPoint(ccp(0, 0)); jumpButton->setPosition(visibleSize.width-50, 32); bulltButton = Sprite::create("bulltButton.png");// , "bulltButtonSelected.png", this, menu_selector(GameLayer::bulltCallback)); bulltButton->setAnchorPoint(ccp(0, 0)); bulltButton->setPosition(visibleSize.width-jumpButton->getContentSize().width-50-30, 32); this->addChild(leftButton); this->addChild(rightButton); this->addChild(upButton); this->addChild(downButton); this->addChild(jumpButton); this->addChild(bulltButton); /*Menu *menu = Menu::create(leftButton, rightButton,downButton,upButton,jumpButton,bulltButton, NULL); menu->setAnchorPoint(ccp(0, 0)); menu->setPosition(0, 0); this->addChild(menu);*/ } void GameLayer::leftCallback(float p)//往左行走 { if (player->getPlayerState() != STATE_PLAYER_RUNLEFT && player->getPlayerState() != STATE_PLAYER_JUMP) { player->runLeft(); } auto x1 = player->getPositionX(); auto x2 = tmxMap->getPositionX(); if (x2 >= 0 && x1>32) { player->setPositionX(x1 - SPEED_X); } else if (tmxMap->getPositionX() < 0 ) { tmxMap->setPositionX(x2 + SPEED_X); } } void GameLayer::rightCallback(float p)//往右走 { if (player->getPlayerState()!=STATE_PLAYER_RUNRIGHT && player->getPlayerState()!=STATE_PLAYER_JUMP) player->runRight(); auto x1 = player->getPositionX(); auto x2 = tmxMap->getPositionX(); if (x1 < visibleSize.width / 2)//走到最左边则人物走,地图不动 { player->setPositionX(x1 + SPEED_X); } else if (tmxMap->getContentSize().width+x2 > visibleSize.width)//x2为负数,左边为剩余的大小 { tmxMap->setPositionX(x2 - SPEED_X); /*for (auto obj : vector_Ground) { obj->setPositionX(obj->getPositionX() - SPEED_X); }*///精灵直接加入tmxmap层 } }//改写参数。。。 void GameLayer::upCallback(float p)//往上 { player->up(); } void GameLayer::downCallback(float p)//向下 { player->down(); } void GameLayer::jumpDownCallbcak(float p) { int y = player->getPositionY(); if (y==oldy) { player->setPlayerState(STATE_PLAYER_STAND); player->stand(); this->unschedule(schedule_selector(GameLayer::jumpDownCallbcak)); } else { oldy = y; } //player->setPositionY(player->getPositionY() - SPEED_Y); /*if (y==player->getPositionY()) { player->setPlayerState(STATE_PLAYER_STAND); this->unschedule(schedule_selector(GameLayer::jumpDownCallbcak)); }*/ //player->setHight(-SPEED_Y); CCLOG("y=%d", y); } void GameLayer::jumpCallback(float p)//跳 { player->setPlayerState(STATE_PLAYER_JUMP); player->jump(); //player->setRotation(60); player->setPositionY(player->getPositionY() + SPEED_Y); player->setHight(); if (player->getHight() >= HIGH_MAX) { this->unschedule(schedule_selector(GameLayer::jumpCallback)); this->schedule(schedule_selector(GameLayer::jumpDownCallbcak), 0.016); } } void GameLayer::bulltCallback(float p)//发射子弹 { bulltTime++; if (bulltTime<BULLT_TIME+10) { return; } Bullt *bullt = Bullt::createWithFile("playerbullt.png"); bullt->setPosition(player->getPosition()); bullt->setDirection(player->getPlayerState()); auto body = PhysicsBody::create(); body->addShape(PhysicsShapeBox::create(bullt->getContentSize())); body->setDynamic(true); body->setGravityEnable(false); body->setCategoryBitmask(1); body->setCollisionBitmask(2); body->setContactTestBitmask(2); bullt->setPhysicsBody(body); if (player->getPlayerState() == STATE_PLAYER_UP) { auto moveto = MoveTo::create(300 / BULLT_SPEED, ccp(player->getPositionX(),300)); bullt->runAction(moveto); } else if (player->m_direction == DIRECTION_RIGHT) { auto moveto = MoveTo::create(2000 / BULLT_SPEED, ccp(2000, player->getPositionY())); bullt->runAction(moveto); } else if (player->m_direction == DIRECTION_LEFT) { auto moveto = MoveTo::create(2000 / BULLT_SPEED, ccp(-2000, player->getPositionY())); bullt->runAction(moveto); } bullt->setTag(TAG_BULLT); this->addChild(bullt); vector_PlayerBullt.push_back(bullt); bulltTime = 0; } bool GameLayer::onTouchBegan(Touch *touch, Event *unused_event) { auto p = touch->getLocation(); if (rightButton->getBoundingBox().containsPoint(p))//右键按下。。。其他同理 { player->m_direction = DIRECTION_RIGHT; rightButton->setSpriteFrame(SpriteFrame::create("rightButtonSelected.png",Rect(0,0,48,36))); this->schedule(schedule_selector(GameLayer::rightCallback), 0.1); } if (leftButton->getBoundingBox().containsPoint(p)) { player->m_direction = DIRECTION_LEFT; leftButton->setSpriteFrame(SpriteFrame::create("leftButtonSelected.png", Rect(0, 0, 48, 36))); this->schedule(schedule_selector(GameLayer::leftCallback), 0.1); } if (upButton->getBoundingBox().containsPoint(p)) { //player->m_direction = DIRECTION_UP; upButton->setSpriteFrame(SpriteFrame::create("upButtonSelected.png", Rect(0, 0, 37, 48))); this->schedule(schedule_selector(GameLayer::upCallback), 0.1); } if (downButton->getBoundingBox().containsPoint(p)) { //player->m_direction = DIRECTION_DOWN; downButton->setSpriteFrame(SpriteFrame::create("downButtonSelected.png", Rect(0, 0, 35, 48))); this->schedule(schedule_selector(GameLayer::downCallback), 0.1); } if (jumpButton->getBoundingBox().containsPoint(p)) { jumpButton->setSpriteFrame(SpriteFrame::create("jumpButtonSelected.png", Rect(0, 0, 48, 48))); if (player->getPlayerState() != STATE_PLAYER_JUMP) { player->setHight(0); this->schedule(schedule_selector(GameLayer::jumpCallback), 0.1); } } if (bulltButton->getBoundingBox().containsPoint(p)) { bulltButton->setSpriteFrame(SpriteFrame::create("bulltButtonSelected.png", Rect(0, 0, 48, 48))); this->schedule(schedule_selector(GameLayer::bulltCallback), 0.016); } return true; } void GameLayer::onTouchMoved(Touch *touch, Event *unused_event) { auto p = touch->getLocation(); if (!rightButton->getBoundingBox().containsPoint(p))//右键按下。。。其他同理,移出按键则关闭定时器 { rightButton->setSpriteFrame(SpriteFrame::create("rightButton.png", Rect(0, 0, 32, 24))); this->unschedule(schedule_selector(GameLayer::rightCallback)); } if (!leftButton->getBoundingBox().containsPoint(p)) { leftButton->setSpriteFrame(SpriteFrame::create("leftButton.png", Rect(0, 0, 32, 24))); this->unschedule(schedule_selector(GameLayer::leftCallback)); } if (!upButton->getBoundingBox().containsPoint(p)) { upButton->setSpriteFrame(SpriteFrame::create("upButton.png", Rect(0, 0, 25, 32))); this->unschedule(schedule_selector(GameLayer::upCallback)); player->stand(); } if (!downButton->getBoundingBox().containsPoint(p)) { downButton->setSpriteFrame(SpriteFrame::create("downButton.png", Rect(0, 0, 24, 32))); this->unschedule(schedule_selector(GameLayer::downCallback)); player->stand(); } if (!jumpButton->getBoundingBox().containsPoint(p)) { jumpButton->setSpriteFrame(SpriteFrame::create("jumpButton.png", Rect(0, 0, 32, 32))); // this->unschedule(schedule_selector(GameLayer::jumpCallback)); } if (!bulltButton->getBoundingBox().containsPoint(p)) { bulltButton->setSpriteFrame(SpriteFrame::create("bulltButton.png", Rect(0, 0, 32, 32))); this->unschedule(schedule_selector(GameLayer::bulltCallback)); bulltTime = BULLT_TIME; } } void GameLayer::onTouchEnded(Touch *touch, Event *unused_event) { //this->unscheduleAllSelectors(); this->unschedule(schedule_selector(GameLayer::rightCallback)); this->unschedule(schedule_selector(GameLayer::leftCallback)); this->unschedule(schedule_selector(GameLayer::upCallback)); this->unschedule(schedule_selector(GameLayer::downCallback)); this->unschedule(schedule_selector(GameLayer::bulltCallback)); rightButton->setSpriteFrame(SpriteFrame::create("rightButton.png", Rect(0, 0, 32, 24))); leftButton->setSpriteFrame(SpriteFrame::create("leftButton.png", Rect(0, 0, 32, 24))); downButton->setSpriteFrame(SpriteFrame::create("downButton.png", Rect(0, 0, 24, 32))); upButton->setSpriteFrame(SpriteFrame::create("upButton.png", Rect(0, 0, 25, 32))); jumpButton->setSpriteFrame(SpriteFrame::create("jumpButton.png", Rect(0, 0, 32, 32))); bulltButton->setSpriteFrame(SpriteFrame::create("bulltButton.png", Rect(0, 0, 32, 32))); player->stand(); bulltTime = BULLT_TIME; //player->setPlayerState(STATE_PLAYER_STAND); }
相关文章推荐
- cocos2dx 3.3 魂斗罗初步尝试 (目前)敌人和子弹(只做了一点点)
- cocos2dx 3.3 魂斗罗初步尝试 人物类
- cocos2dx 3.3 魂斗罗初步尝试 欢迎场景和声音类
- cocos2dx 3.3 跑酷游戏的编写
- cocos2dx 3.3 跑酷游戏的编写 游戏层
- 笔记:利用Cocos2dx 3.3 lua 做一个动作类游戏(一)
- 【Cocos2dx】利用导演类、场景类完成重新开始游戏、暂停游戏、关闭游戏功能
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- 写了一段从图像中分离人物和背景的代码,本来想写成智能定位Breast的,可是时间不够,公司游戏开发任务太紧了。先发一段吧···以后有空再补上
- cocos2dx 多人小游戏时间同步问题(简单版)
- Cocos2d-x 自定义血条及其美化----之游戏开发《赵云要格斗》(4)cocos2dx 3.3移植版
- 【Cocos2dx】用当前游戏状态作为暂停背景、纯文字纯图片按钮与不间断的重复动作序列
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- cocos2dx 3.3 跑酷游戏 信息层
- Cocos2d-x怪物智能AI怪物也有智商--之游戏开发《赵云要格斗》(6) cocos2dx 3.3移植版
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- cocos2dx 3.3 跑酷游戏 人物的创建
- cocos2dx 3.3 + QT5.3制作游戏编辑器
- 关于cocos2dx-lua版本中游戏时间显示问题
- cocos2dx3.3开发FlappyBird总结十六:游戏层实现