忍者实例地图(TODO用这个精灵替换图中的)
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { cocos2d::TMXTiledMap* _tileMap; ① cocos2d::Sprite *_player; ② public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); _tileMap = TMXTiledMap::create("map/MiddleMap.tmx"); ① addChild(tileMap,0,100); ② TMXObjectGroup* group = _tileMap ->getObjectGroup("objects"); ③ ValueMap spawnPoint = group->getObject("ninja"); ④ float x = spawnPoint["x"].asFloat(); ⑤ float y = spawnPoint["y"].asFloat(); ⑥ _player = Sprite::create("ninja.png"); ⑦ _player ->setPosition(Point(x,y)); ⑧ addChild(_player, 2,200); return true; }
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" class HelloWorld : public cocos2d::Layer { cocos2d::TMXTiledMap* _tileMap; cocos2d::Sprite *_player; public: static cocos2d::Scene* createScene(); virtual bool init(); virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); ① virtual void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event); virtual void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event); ② CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
bool HelloWorld::init() { … … setTouchEnabled(true); //设置为单点触摸 setTouchMode(Touch::DispatchMode::ONE_BY_ONE); return true; }
bool HelloWorld::onTouchBegan(Touch* touch, Event* event) { log("onTouchBegan"); return true; } void HelloWorld::onTouchMoved(Touch *touch, Event *event) { log("onTouchMoved"); } void HelloWorld::onTouchEnded(Touch *touch, Event *event) { log("onTouchEnded"); Point touchLocation = touch->getLocation(); ① Point playerPos = _player->getPosition(); ② Point diff = touchLocation - playerPos; ③ if (abs(diff.x) > abs(diff.y)) { ④ if (diff.x > 0) { ⑤ playerPos.x += _tileMap->getTileSize().width; _player->runAction(FlipX::create(false)); ⑥ } else { playerPos.x -= _tileMap->getTileSize().width; _player->runAction(FlipX::create(true)); ⑦ } } else { if (diff.y > 0) { ⑧ playerPos.y += _tileMap->getTileSize().height; } else { playerPos.y -= _tileMap->getTileSize().height; } } _player->setPosition(playerPos); ⑨ }
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "SimpleAudioEngine.h" class HelloWorld : public cocos2d::Layer { cocos2d::TMXTiledMap* _tileMap; cocos2d::TMXLayer* _collidable; ① cocos2d::Sprite *_player; public: static cocos2d::Scene* createScene(); virtual bool init(); virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); virtual void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event); virtual void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event); void setPlayerPosition(cocos2d::Point position); ② cocos2d::Point tileCoordFromPosition(cocos2d::Point position); ③ CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); _tileMap = TMXTiledMap::create("map/MiddleMap.tmx"); addChild(_tileMap,0,100); TMXObjectGroup* group = _tileMap->getObjectGroup("objects"); ValueMap spawnPoint = group->getObject("ninja"); float x = spawnPoint["x"].asFloat(); float y = spawnPoint["y"].asFloat(); _player = Sprite::create("ninja.png"); _player->setPosition(Point(x,y)); addChild(_player, 2, 200); _collidable = _tileMap->getLayer("collidable"); ① _collidable->setVisible(false); ② setTouchEnabled(true); //设置为单点触摸 setTouchMode(Touch::DispatchMode::ONE_BY_ONE); return true; }
HelloWorld::onTouchEnded代码如下: void HelloWorld::onTouchEnded(Touch *touch, Event *event) { log("onTouchEnded"); //获得在OpenGL坐标 Point touchLocation = touch->getLocation(); Point playerPos = _player->getPosition(); Point diff = touchLocation - playerPos; if (abs(diff.x) > abs(diff.y)) { if (diff.x > 0) { playerPos.x += _tileMap->getTileSize().width; _player->runAction(FlipX::create(false)); } else { playerPos.x -= _tileMap->getTileSize().width; _player->runAction(FlipX::create(true)); } } else { if (diff.y > 0) { playerPos.y += _tileMap->getTileSize().height; } else { playerPos.y -= _tileMap->getTileSize().height; } } this->setPlayerPosition(playerPos); ① }
void HelloWorld::setPlayerPosition(Point position) { //从像素点坐标转化为瓦片坐标 Point tileCoord = this->tileCoordFromPosition(position); ① //获得瓦片的GID int tileGid = _collidable->getTileGIDAt(tileCoord); ② if (tileGid > 0) { ③ Value prop = _tileMap->getPropertiesForGID(tileGid); ④ ValueMap propValueMap = prop.asValueMap(); ⑤ std::string collision = propValueMap["Collidable"].asString(); ⑥ if (collision == "true") { //碰撞检测成功 ⑦ CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("empty.wav"); ⑧ return ; } } _player->setPosition(position); }
Point HelloWorld::tileCoordFromPosition(Point pos) { int x = pos.x / _tileMap->getTileSize().width; ① int y = ((_tileMap->getMapSize().height * _tileMap->getTileSize().height) - pos.y) / _tileMap->getTileSize().height; ② return Point(x,y); }
void HelloWorld::setViewpointCenter(Point position) { Size visibleSize = Director::getInstance()->getVisibleSize(); int x = MAX(position.x, visibleSize.width / 2); ① int y = MAX(position.y, visibleSize.height / 2); ② x = MIN(x, (_tileMap->getMapSize().width * _tileMap->getTileSize().width) - visibleSize.width / 2); ③ y = MIN(y, (_tileMap->getMapSize().height * _tileMap->getTileSize().height) - visibleSize.height/2); ④ //屏幕中心点 Point pointA = Point(visibleSize.width/2, visibleSize.height/2); ⑤ //使精灵处于屏幕中心,移动地图目标位置 Point pointB = Point(x, y); ⑥ log("目标位置 (%f ,%f) ",pointB.x,pointB.y); //地图移动偏移量 Point offset =pointA - pointB; ⑦ log("offset (%f ,%f) ",offset.x, offset.y); this->setPosition(offset); ⑧ }
移动地图
欢迎关注智捷iOS课堂微信公共平台
原文地址:http://blog.csdn.net/tonny_guan/article/details/39375383