一、加载瓦片地图以及确定对象精灵添加监听
map = TMXTiledMap::create("Map\\map1.tmx");
this->addChild(map);
map->setPosition(0, 0);
player = Sprite::create("Map\\player.png");
this->addChild(player, 2);
TMXObjectGroup* objLayer = map->getObjectGroup("PlayerObject");
ValueMap obj = objLayer->getObject("hero");
float x = obj["x"].asFloat();
float y = obj["y"].asFloat();
player->setPosition(x, y);
player->setAnchorPoint(Size::ZERO);
EventListenerTouchOneByOne* myTouchOneByOne = EventListenerTouchOneByOne::create();
myTouchOneByOne->onTouchBegan = CC_CALLBACK_2(Scene_14::touchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(myTouchOneByOne, this);
二、控制角色在地图中移动
bool Scene_14::touchBegan(Touch* touch, Event* event)
{
Vec2 playerPos = player->getPosition();
Vec2 nextPlayer(playerPos.x + 70, playerPos.y);
Point playerPoint = Point((int)nextPlayer.x / 70, 9 - (int)nextPlayer.y / 70);
TMXLayer *pWall = map->layerNamed("wall");
int id = pWall->tileGIDAt(playerPoint);
if (58 != id)
player->runAction(MoveBy::create(1.0f, Vec2(70, 0)));
return true;
}
|