以下为player移动的控制代码
在此之前还要添加windos的键盘识别代码
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) // #include <windows.h>
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0)
#define KEY_UP(vk_code) (GetAsyncKeyState(vk_code) & 0x8000 ? 0 : 1)
#endif
//添加到头文件中就可以了,接下来让player动起来
void HelloWorld::move()
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
if(KEY_DOWN(VK_UP)&&jump) //jump
{
yVel=4;
xVel=2;
}
if(KEY_DOWN(VK_RIGHT)) //right
{
xVel=2;
}
else if(KEY_DOWN(VK_LEFT)) //left
{
xVel=-2;
}
else if(KEY_UP(VK_LEFT)||KEY_UP(VK_RIGHT))
{
xVel=0;
}
#endif
}
现在player可以窗口内快乐的运动了,但是空间还是不够广阔,对于横版游戏,player需要在地图中不断的穿行,由于手机内存的关系,不可能做一张很大的地图,如此需要小地图来进行拼接,合理利用资源,如此设计了如下函数:
void HelloWorld::MapControl()
{
//地Ì?图ª?处ä|理¤¨ª算?法¤¡§
CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
int mapWidth=map->getMapSize().width*map->getTileSize().width;
int mapHeight=map->getMapSize().height*map->getTileSize().height;
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
float deltax=player->getPosition().x-winSize.width/2; //
float deltay=player->getPosition().y-32; //
if(deltax<0)
{
deltax=0;
}
if(deltax>mapWidth-winSize.width)
{
deltax=mapWidth-winSize.width;
}
if(-deltay>0)
{
deltay=0;
}
if(deltay>mapHeight-winSize.height)
{
deltay=mapHeight-winSize.height;
}
map->setPosition(ccp(-deltax,-deltay)); //
}
void HelloWorld::gameLoop(ccTime dt)
{
if(player->getPosition().x>970)
{//我的地图是1024x1024
CCUserDefault::sharedUserDefault()->setIntegerForKey("lvl",
CCUserDefault::sharedUserDefault()->getIntegerForKey("lvl") + 1);
CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::transitionWithDuration(0.01,HelloWorld::scene()));//scene切换部分
}//将此段代码和地图初始化的代码配合就可以实现地图的切换了