//定義一個方向枚舉 (頭文件)
typedef enum JumpDir
{
DirUp,DirDown,DirStop
}
public:
int speed;//速度
JumpDir jump;
//******************Cpp文件************
1.
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin =
Director::getInstance()->getVisibleOrigin();
speed = 30;//初始化爲30
2.//添加背景層 (錨點默認在 point(0.5,0.5))
auto BG = Sprite:create("bg.jpg");
BG.setPosition(visibleSize/2);
BG.setTag(90);//設置一個標記
this->addChild(BG);
3.//加載地圖層,把製作好的地圖添加到遊戲中(錨點默認在 point(0,0))
auto map01 = TMXTiledMap::create("t01.tmx");
map1->setTag(100);
////設置一個標記
this->addChild(map1);
4.創建一個精靈動畫,讓它在地圖上跑。(可以做一個封裝)
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("run.plist","run.png");
SpriteFrame *frame;
Vector<SpriteFrame*> arrayFrame;
int index = 1;
do
{
frame = cache->spriteFrameByName(String::createWithFormat("run%d.png",index)->getCString());
if (frame == NULL)
{
break;
}
arrayFrame.pushBack(frame);
index++;
} while (1);
Animation* animation = Animation::createWithSpriteFrames(arrayFrame);
animation->setDelayPerUnit(0.1f);
animation->setLoops(-1);
Animate* act = Animate::create(animation);
sp->runAction(act);
5.在update函數中,無限滾地圖
//獲取地圖標記
auto map01 = this->getChildByTag(100);
if(map01->getPositionX() > -16000+960)//map01->getPositionX()獲取的點一直向左移動小於0
{
map01->setPositionX(map01->getPositionX()-5);
}
6.添加觸摸事件
處理人物的跳躍
7.封裝一個人物跳躍函數
void SpriteJump();
{
//人物跳躍
auto sp = this->getChildByTag(120);
if (jump == JumpDir::DirUp)
//如果方向 向上,否則方向 向下
{ //使精靈的向上運動類似於向 上拋的石頭 速度越來越多小,速度小於0 後向下落
sp->setPositionY(sp->getPositionY()+speed);
//精靈的Y軸從30開始遞減
speed = speed - 2;
//速度每向上一幀減小 2
if (speed==0)
//當上升的速度小於0時,改變運動方向 向下
{
jump=JumpDir::Dirdown;//運動方向 向下
}
log("DirUp=%d",speed);
}else if (jump==JumpDir::Dirdown)
//如果方向 向上,否則方向 向下
{
sp->setPositionY(sp->getPositionY()-speed);//精靈的Y軸從 0 開始增加
speed = speed + 2;
//速度每向下一幀增加 2
log("DirDown=%d",speed);
checkDown();
//檢測是否踩到草坪
if (speed > 30)
{
//jump = JumpDir::DirStop;
//運動方向 停止
speed =30;
checkDown();
}
}
}
-----------------------------
void HelloWorld::checkDown()
{
auto nowplayer =(Sprite*) this->getChildByTag(120);
auto nowmap =(TMXTiledMap*)this->getChildByTag(100);
// px 滾動的地圖長度加上人物在屏幕上的位置,就是人物踩在地圖是的位置
int px = nowplayer->getPositionX()+abs(0-nowmap->getPositionX());
//py 人物在地圖的位置
int py = nowplayer->getPositionY()+0;
//人物在地圖的 行 列
int nowrow = px/32;
Tiled地圖的行
int nowcol = py/32;
Tiled地圖的列
log ("nowrow=%d nowcol=%d",nowrow,nowcol);
if (nowcol < 0 || nowcol >14)
{
return ;
}
(地圖的高度/塊大小 - nowcol)
int tid = nowmap->getLayer("t01")->getTileGIDAt(Vec2(nowrow,14-nowcol));
log("tid=%d",tid);
if (tid >0)
{
jump = JumpDir::DirStop;
}else
{
log("Game Ocer");
return;
}
}
簡單跑酷
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.