前言:
我们在做经典的格斗类的游戏的时候,场景常常用的是45°斜地图来创建的。下面我就来实现一个简单的Demo来展现一下斜地图的使用。
功能实现:
1.倾斜地图的加载;
2.点击地图居中;
3.主角只能在一定的范围内移动;
4.鼠标点击屏幕,主角移动一格,如果连续点击则主句不断的移动;
代码实现:
图层要设置z轴属性,方便可以隐藏主角:
在图层的属性中加上 cc_vertexz -400
如果前面的图层那就设置为automatic
在AppDelegate添加代码:
[plain]
//深度测试,方便实现遮盖效果
CCDirector::sharedDirector()->setDepthTest(true);
//Opengl渲染设置,如果地图有背景图层的话就需要加这句
CCDirector::sharedDirector()->setProjection(kCCDirectorProjection2D);
Player类:
[plain]
#ifndef ___5tilemap__Player__
#define ___5tilemap__Player__
#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class Player : public CCSprite
{
public:
static Player * create();
virtual bool initPlayer();
void updateVertextZ(CCPoint tilePos,CCTMXTiledMap * tileMap);
};
#endif /* defined(___5tilemap__Player__) */
[plain]
#include "Player.h"
static Player *s;
Player* Player::create()
{
s = new Player();
if (s&&s->initPlayer()) {
s->autorelease();
return s;
}
else
{
delete s;
s = NULL;
return NULL;
}
}
bool Player::initPlayer()
{
if (!CCSprite::initWithFile("ninja.png")) {
return false;
}
return true;
}
void Player::updateVertextZ(cocos2d::CCPoint tilePos, cocos2d::CCTMXTiledMap *tileMap)
{
float lowestZ = -(tileMap->getMapSize().width + tileMap->getMapSize().height);
float currentZ = tilePos.x + tilePos.y;
this->setVertexZ(lowestZ+currentZ - 1);
}
HelloWorld.h:
[plain]
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Player.h"
using namespace cocos2d;
typedef enum {
MoveDirectionNone = 0,
MoveDirectionUpperLeft,
MoveDirectionLowerLeft,
MoveDirectionUpperRight,
MoveDirectionLowerRight,
MAX_MoveDirections
}EMoveDirection;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
CCPoint locationFromTouches(CCSet *touches);
Player * player;
CCPoint playableAreaMin,playableAreaMax;
//获取瓷砖块的坐标
CCPoint tilePosFromLocation(CCPoint location,CCTMXTiledMap * tilemap);
//图层居中
void centerTileMapOnTileCoord(CCPoint tilePos,CCTMXTiledMap *tileMap);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
CCPoint screenCenter;
CCTMXTiledMap * tileMap;
CCRect upperLeft,lowerLeft,upperRight,lowerRight;
CCPoint moveOffsets[MAX_MoveDirections];
EMoveDirection currentMoveDirection;
void update(float delta);
CCPoint ensureTilePosIsWithinBounds(CCPoint tilePos);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
[plain]
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "Player.h"
using namespace cocos2d;
using namespace CocosDenshion;
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);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
//添加一个地图
<