前言:
我们在做经典的格斗类的游戏的时候,场景常常用的是45°斜地图来创建的。下面我就来实现一个简单的Demo来展现一下斜地图的使用。
功能实现:
1.倾斜地图的加载;
2.点击地图居中;
3.主角的移动;
代码实现:
图层要设置z轴属性,方便可以隐藏主角:
在图层的属性中加上 cc_vertexz -400
如果前面的图层那就设置为automatic
在AppDelegate添加代码:
//深度测试,方便实现遮盖效果
CCDirector::sharedDirector()->setDepthTest(true);
//Opengl渲染设置,如果地图有背景图层的话就需要加这句
CCDirector::sharedDirector()->setProjection(kCCDirectorProjection2D);
Player类:
#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__) */
#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:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Player.h"
using namespace cocos2d;
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);
};
HelloWorldScene.cpp
#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();
//添加一个地图
CCTMXTiledMap * tileMap = CCTMXTiledMap::create("huohuo.tmx");
// CCTMXTiledMap * tileMap = CCTMXTiledMap::create("isometric.tmx");
// tileMap->setAnchorPoint(CCPointMake(size.width/2, size.height/2));
// tileMap->setPosition(CCPointMake(size.width/2, size.height/2));
CCSize s = tileMap->getContentSize();
CCLog("width:%f",-s.width/2);
tileMap->setPosition(ccp(-s.width/2,0));
this->addChild(tileMap,-1,1);
this->setTouchEnabled(true);
//添加主角精灵
player = Player::create();
player->setPosition(CCPointMake(size.width / 2, size.height / 2));
player->setAnchorPoint(ccp(0.3f,0.1));
this->addChild(player);
const int borderSize = 10;
playableAreaMin = CCPointMake(borderSize, borderSize);
playableAreaMax = CCPointMake(tileMap->getMapSize().width - 1 - borderSize, tileMap->getMapSize().height - 1 - borderSize);
return true;
}
//返回点击的坐标点
CCPoint HelloWorld::locationFromTouches(cocos2d::CCSet *touches)
{
CCTouch *touch = (CCTouch *)touches->anyObject();
return touch->getLocation();
}
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
CCNode * node = this->getChildByTag(1);
CCTMXTiledMap *tileMap = (CCTMXTiledMap *)node;
CCPoint touchLocation = this->locationFromTouches(pTouches);
CCPoint tilepos = this->tilePosFromLocation(touchLocation, tileMap);
CCLog("%f,%f",tilepos.x,tilepos.y);
this->centerTileMapOnTileCoord(tilepos, tileMap);
player->updateVertextZ(tilepos, tileMap);
}
//获取瓷砖块的坐标
CCPoint HelloWorld::tilePosFromLocation(cocos2d::CCPoint location, cocos2d::CCTMXTiledMap *tilemap)
{
CCPoint pos = ccpSub(location, tilemap->getPosition());
float halfMapWidth = tilemap->getMapSize().width * 0.5f;
float mapHeight = tilemap->getMapSize().height;
float tileWidth = tilemap->getTileSize().width;
float tileHeight = tilemap->getTileSize().height;
CCPoint tilePasDiv = ccp(pos.x / tileWidth, pos.y / tileHeight);
float inverseTileY = mapHeight - tilePasDiv.y;
float posX = (int)(inverseTileY + tilePasDiv.x - halfMapWidth);
float posY = (int)(inverseTileY - tilePasDiv.x + halfMapWidth);
// posX = MAX(0, posX);
// posX = MIN(tilemap->getMapSize().width - 1, posX);
// posY = MAX(0, posY);
// posY = MIN(tilemap->getMapSize().height - 1, posY);
posX = MAX(playableAreaMin.x,posX);
posX = MIN(playableAreaMax.x, posX);
posY = MAX(playableAreaMin.y, posY);
posY = MIN(playableAreaMax.y, posY);
pos = CCPointMake(posX, posY);
return pos;
}
//将地图居中
void HelloWorld::centerTileMapOnTileCoord(cocos2d::CCPoint tilePos, cocos2d::CCTMXTiledMap *tileMap)
{
//获取屏幕大小和屏幕中心点
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCPoint screenCenter = CCPointMake(size.width/2, size.height/2);
//获取地板层
CCTMXLayer * layer = tileMap->layerNamed("Ground");
//CCTMXLayer * layer = tileMap->layerNamed("GroundLayer1");
//仅仅在内部使用;瓷砖的Y坐标减去1
tilePos.y -=1;
//获取瓷砖块坐标
CCPoint scrollPosition = layer->positionAt(tilePos);
//考虑到地图移动的情况,我将像素坐标信息乘以-1,从而得到负值
scrollPosition = ccpMult(scrollPosition, -1);
//为屏幕中央坐标添加位移值
scrollPosition = ccpAdd(scrollPosition, screenCenter);
CCMoveTo * move = CCMoveTo::create(.2f, scrollPosition);
tileMap->stopAllActions();
tileMap->runAction(move);
}
实现效果:
源码下载:
http://download.csdn.net/detail/s10141303/6302839
分享到:
相关推荐
JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台发布、程序代码管理、两大应用商店发布产品。...
第1章 Cocos2D-X引擎的介绍 1 1.1 何为游戏引擎 1 1.1.1 游戏的核心—引擎 1 1.1.2 引擎的特点 2 1.1.3 知名的引擎介绍 4 1.1.4 引擎的分类 5 1.2 Cocos2D-X引擎的来历 8 1.3 引擎的版本 9 1.4 下载与安装 10 1.5 ...
介绍Cocos2d-x更高阶的内容,包括渲染效率的提高、动画、TexturePacker图片打包、Tiled地图游戏实例、定时器、函数回调、内存管理、数据保存、Csv文件读取。介绍Lua、有限状态机启蒙知识和应用。分享基于Cocos2d-X的...
全书内容涵盖了Cocos2d-x的核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、数据交换格式、内存管理、性能优化、平台移植、程序代码管理、三大应用商店发布产品等。本书共29章,按内容结构可分为六篇...
突破(Cocos2D HTML 2.x) 突破教程游戏的Cocos2d-html实现。 玩游戏 所有其他实现均已存档: 概述 这是用Cocos2D HTML编写的示例突破游戏。 最新版本是2.2.2,并且将在发布3.0时更新或分支。 此版本可在台式机...
此外,还介绍了如何进行应用的测试与发布,以及Cocos3D、Cocos2D-x、Cocos2D-HTML5、Cocos2D-Python等衍生技术。 《Cocos2D权威指南》全书共19章,分为三个部分:基础篇(第1~9章)系统讲解了Cocos2D v2.0的功能...
黄隽实编著的《Android和PHP开发*实践(第2版移动开发)》以一个完整的微博应用项目实例为...此外,书中还介绍了AndroidNDK的开发以及Android游戏开发的相关内容,包括OpenGL的使用、流行游戏引擎Cocos2d-x和Unity3D。
此外,本书还介绍了Android NDK的开发以及Android游戏开发的相关内容,包括OpenGL的使用、流行游戏引擎Cocos2d-x和Unity 3D。 本书适合于所有对Android和PHP技术有兴趣的读者。不管是客户端还是服务端的开发者,都...
Go是基于Cocos2d-x游戏引擎(版本3.10),主要使用C++语言编写的休闲益智类安卓手机游戏。Ball Go于2016-11-02上线腾讯应用宝。 项目团队 导师:李兰 队长:张明睿 队员:施文强、秦凯、王森、徐依依 成员分工 张...
游戏引擎类--基于cocos2d的连连看游戏 游戏引擎类--简单炸弹人小游戏源码 游戏引擎类--切水果游戏 游戏引擎类幸运大转盘的抽奖游戏 游戏引擎-推箱子游戏 游戏引擎之雷电游戏的激光子弹 指示器类 指示器(HUD)之...
(4)Cocos2D-X for XNA游戏开发指南(下)—《TweeJump》项目实战 (5)iOS即时语音聊天技术实践 本文讲解了如何在iOS设备上实现语音聊天应用开发,主要介绍了语音应用开发中的语音录制、播放、编解码等技术。 ...