在init里面:
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
SimpleAudioEngine::getInstance()->preloadEffect("pickup.mp3");
SimpleAudioEngine::getInstance()->preloadEffect("hit.mp3");
SimpleAudioEngine::getInstance()->preloadEffect("move.mp3");
SimpleAudioEngine::getInstance()->playBackgroundMusic("TileMap.mp3");
auto winSize=Director::getInstance()->getWinSize();
std::string file = "TileMap.tmx";
auto str = String::createWithContentsOfFile(FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str());
_tileMap = TMXTiledMap::createWithXML(str->getCString(),"");
_background = _tileMap->getLayer("Background");
_foreground = _tileMap->getLayer("Foreground");
_meta = _tileMap->getLayer("Meta");
_meta->setVisible(false);
TMXObjectGroup *objects = _tileMap->getObjectGroup("Objects");
CCASSERT(NULL != objects, "'Objects' object group not found");
auto spawnPoint = objects->getObject("SpawnPoint");
CCASSERT(!spawnPoint.empty(), "SpawnPoint object not found");
int x = spawnPoint"x"].asInt();
int y = spawnPoint"y"].asInt();
_player = Sprite::create("Player.png");
_player->setPosition(x, y);
addChild(_player);
setViewPointCenter(_player->getPosition());
addChild(_tileMap, -1);
for (auto& eSpawnPoint : objects->getObjects()) {
ValueMap& dict = eSpawnPoint.asValueMap();
if (dict"Enemy"].asInt() == 1) {
x = dict"x"].asInt();
y = dict"y"].asInt();
this->addEnemyAtPos(Point(x,y));
}
}
auto listener = EventListenerTouchOneByOne::create();
//lambda expression: advanced feature in C++ 11
listener->onTouchBegan = &](Touch *touch, Event *unused_event)->bool { return true; };
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
_numCollected = 0;
_mode = 0;
this->schedule(schedule_selector(HelloWorld::testCollisions));
return true;
}
下面是ontouchend函数
void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event)
{
if (_mode == 0) {
// old contents of onTouchEnded
auto touchLocation = touch->getLocation();
touchLocation = this->convertToNodeSpace(touchLocation);
auto playerPos = _player->getPosition();
auto diff = 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().height;
}
else {
playerPos.y -= _tileMap->getTileSize().height;
}
}
if (playerPos.x <= (_tileMap->getMapSize().width * _tileMap->getMapSize().width) &&
playerPos.y <= (_tileMap->getMapSize().height * _tileMap->getMapSize().height) &&
playerPos.y >= 0 &&
playerPos.x >= 0)
{
this->setPlayerPosition(playerPos);
}
this->setViewPointCenter(_player->getPosition());
} else {
// code to throw ninja stars will go here
// Find where the touch is
auto touchLocation = touch->getLocation();
touchLocation = this->convertToNodeSpace(touchLocation);
// Create a projectile and put it at the player's location
auto projectile = Sprite::create("Projectile.png");
projectile->setPosition(_player->getPosition());
this->addChild(projectile);
// Determine where we wish to shoot the projectile to
int realX;
// Are we shooting to the left or right?
auto diff = touchLocation - _player->getPosition();
if (diff.x > 0)
{
realX = (_tileMap->getMapSize().width * _tileMap->getTileSize().width) +
(projectile->getContentSize().width / 2);
}
else {
realX = -(_tileMap->getMapSize().width * _tileMap->getTileSize().width) -
(projectile->getContentSize().width / 2);
}
float ratio = (float)diff.y / (float)diff.x;
int realY = ((realX - projectile->getPosition().x) * ratio) + projectile->getPosition().y;
auto realDest = Point(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile->getPosition().x;
int offRealY = realY - projectile->getPosition().y;
float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
float velocity = 480 / 1; // 480pixels/1sec
float realMoveDuration = length / velocity;
// Move projectile to actual endpoint
auto actionMoveDone = CallFuncN::create(CC_CALLBACK_1(HelloWorld::projectileMoveFinished, this));
projectile->runAction(Sequence::create(MoveTo::create(realMoveDuration, realDest), actionMoveDone, NULL));
_projectiles.pushBack(projectile);
}
}
这里面的this指针的调用对象是什么,为什么地图图层会偏移呢,__tileMap在那里调用setviewpoint了?