Cocos2d game example tiledmap

Source: Internet
Author: User

Original article: http://www.raywenderlich.com/39113/cocos2d-x-tile-map-tutorial-part-1

The original Article was originally written in C ++, and the code has almost never been changed.

Package APK + resources: http://download.csdn.net/detail/u010639508/5875945

Code: https://github.com/serika00/android/tree/master/cocos2d-x/TileMapExample

Cocos2d-x-2.14 + vs2010

Run:

Helloworldscene. h

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "HudLayer.h"USING_NS_CC;class HelloWorld : public cocos2d::CCLayer{public:    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();      // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);        // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);void setViewPointCenter(CCPoint position);void registerWithTouchDispatcher();void setPlayerPosition(CCPoint position);bool ccTouchBegan(CCTouch* touch, CCEvent* event);void ccTouchEnded(CCTouch* touch, CCEvent* event);CCPoint tileCoordForPosition(CCPoint position);private:CCTMXTiledMap* _tileMap;CCTMXLayer* _background;CCSprite* _player;CCTMXLayer* _meta;CCTMXLayer* _foreground;HudLayer* _hud;int _numCollected;};#endif // __HELLOWORLD_SCENE_H__

Helloworldscene. cpp

#include "HelloWorldScene.h"//#include "SimpleAudioEngine.h"USING_NS_CC;CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);HudLayer* hud = new HudLayer();hud->init();scene->addChild(hud);layer->_hud = hud;    // return the scenereturn scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("pickup.caf");//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("hit.caf");//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("move.caf");//CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("TileMap.caf");this->setTouchEnabled(true);_numCollected = 0;_tileMap = new CCTMXTiledMap();_tileMap->initWithTMXFile("TileMap.tmx");_background = _tileMap->layerNamed("Background");this->addChild(_tileMap);_foreground = _tileMap->layerNamed("Foreground");_meta = _tileMap->layerNamed("Meta");_meta->setVisible(false);CCTMXObjectGroup* objectGroup = _tileMap->objectGroupNamed("Objects");if (objectGroup == NULL) {CCLog("tile map has no objects object layer.");return false;}CCDictionary* spawnPoint = objectGroup->objectNamed("SpawnPoint");int x = ((CCString)*spawnPoint->valueForKey("x")).intValue();int y = ((CCString)*spawnPoint->valueForKey("y")).intValue();_player = new CCSprite();_player->initWithFile("Player.png");_player->setPosition(ccp(x, y));this->addChild(_player);this->setViewPointCenter(_player->getPosition());return true;}void HelloWorld::menuCloseCallback(CCObject* pSender){    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}void HelloWorld::setViewPointCenter(CCPoint position) {CCSize winSize = CCDirector::sharedDirector()->getWinSize();int x = MAX(position.x, winSize.width/2);int y = MAX(position.y, winSize.height/2);x = MIN(x, (_tileMap->getMapSize().width*this->_tileMap->getTileSize().width) - winSize.width/2);y = MIN(y, (_tileMap->getMapSize().height*this->_tileMap->getTileSize().height) - winSize.height/2);CCPoint actualPosition = ccp(x, y);CCPoint centerOfView = ccp(winSize.width/2, winSize.height/2);CCPoint viewPoint = ccpSub(centerOfView, actualPosition);this->setPosition(viewPoint);}void HelloWorld::registerWithTouchDispatcher() {CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);}bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event) {return true;}void HelloWorld::setPlayerPosition(CCPoint position) {CCPoint tileCoord = this->tileCoordForPosition(position);int tileGid = _meta->tileGIDAt(tileCoord);if (tileGid) {CCDictionary* properties = _tileMap->propertiesForGID(tileGid);if (properties) {CCString* collision = new CCString();*collision = *properties->valueForKey("Collidable");if (collision && (collision->compare("True") == 0)) {//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("hit.caf");return;}CCString *collectible = new CCString();*collectible = *properties->valueForKey("Collectable");if (collectible && (collectible->compare("True") == 0)) {_meta->removeTileAt(tileCoord);_foreground->removeTileAt(tileCoord);_numCollected++;_hud->numCollectedChanged(_numCollected);//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.caf");}}}//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.caf");_player->setPosition(position);}void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event) {CCPoint touchLocation = touch->getLocationInView();touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);touchLocation = this->convertToNodeSpace(touchLocation);CCPoint playerPos = _player->getPosition();CCPoint diff = ccpSub(touchLocation, playerPos);if (abs(diff.x) > abs(diff.y)) {if (diff.x > 0) {playerPos.x += _tileMap->getTileSize().width;} else {playerPos.x -= _tileMap->getTileSize().width;}} else {if (diff.y > 0) {playerPos.y += _tileMap->getTileSize().width;} else {playerPos.y -= _tileMap->getTileSize().height;}}if (playerPos.x <= (_tileMap->getMapSize().width*_tileMap->getTileSize().width)&& playerPos.y <= (_tileMap->getMapSize().height*_tileMap->getTileSize().height)&& playerPos.y >= 0&& playerPos.x >= 0){this->setPlayerPosition(playerPos);}this->setViewPointCenter(_player->getPosition());}CCPoint HelloWorld::tileCoordForPosition(CCPoint position) {int x = position.x / _tileMap->getTileSize().width;int y = ((_tileMap->getMapSize().height*_tileMap->getTileSize().height) - position.y)/_tileMap->getTileSize().height;return ccp(x, y);}

Hudlayer. h

#ifndef __HUDLAYER_H__#define __HUDLAYER_H__#include "cocos2d.h"USING_NS_CC;class HudLayer: public CCLayer {private:CCLabelTTF* _label;public:virtual bool init();static CCScene* scene();void menuCloseCallBack(CCObject* pSender);CREATE_FUNC(HudLayer);void numCollectedChanged(int numCollected);};#endif

Hudlayer. cpp

#include "HudLayer.h"USING_NS_CC;bool HudLayer::init() {if (CCLayer::init()) {CCSize winSize = CCDirector::sharedDirector()->getWinSize();_label = new CCLabelTTF();_label->initWithString("0", "Verdana-Bold", 18.0);_label->setColor(ccc3(0, 0, 0));int margin = 10;_label->setPosition(ccp(winSize.width - (_label->getContentSize().width/2)- margin, _label->getContentSize().height/2 + margin));this->addChild(_label);}return true;}void HudLayer::numCollectedChanged(int numCollected) {CCString* labelCollected = new CCString();labelCollected->initWithFormat("%d", numCollected);_label->setString(labelCollected->getCString());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.