Cocos2d-x 如何实现不同高低台阶的跳跃
本文主要讲解,如何实现角色从一个台阶跳向另一个台阶。两个台阶高低是不一样的,目的是让角色停在另一个台阶上而不掉下来。
具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void MapLayer::mapCreate()
{
this
->_allMap=CCTMXTiledMap::create(
"map/rosemap.tmx"
);
this
->_allMap->setScale(0.5);
this
->addChild(
this
->_allMap);
this
->_allMap->layerNamed(
"fangluoxia"
)->setVisible(
true
);
//让防落下不可见
CCTMXObjectGroup* objGroup =
this
->_allMap->objectGroupNamed(
"pengzhuang"
);
CCArray* objArray = objGroup->getObjects();
if
( objArray&& objArray->count()>0)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(objArray, pObj)
{
CCDictionary* playerPointDic= (CCDictionary*)pObj;
//将对象转化成字典,便于获取键值
float x = playerPointDic->valueForKey(
"x"
)->floatValue();
this
->_y = playerPointDic->valueForKey(
"y"
)->floatValue();
}
}
}
void MapLayer::jlmInRoad(float dt)
{
if
(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_isJlmCreate)
{
CCSize mapTiledNum =
this
->_allMap->getMapSize();
CCSize tiledSize =
this
->_allMap->getTileSize();
CCPoint point1 =((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition();
int x =point1.x *2 / tiledSize.width;
int y =point1.y *2 / tiledSize.height;
/* Cocos2d-x的默认Y坐标是由下至上的,所以要做一个相减操作 */
y = mapTiledNum.height - y;
int tiledGid=_allMap->layerNamed(
"fangluoxia"
)->tileGIDAt(ccp(x,y));
if
(tiledGid!=0)
{
CCDictionary* propertiesDict = _allMap->propertiesForGID(tiledGid);
/* 取得格子的zhezhao属性值 */
const CCString* prop = propertiesDict->valueForKey(
"flx"
);
/* 判断zhezhao属性是否为true,是的话,不让玩家移动 */
if
(prop->m_sString.compare(
"true"
) == 0) {
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->getPosition().x,80));
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->_jumpDownHigh=90;
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->getPosition().x+0.1*dt,100));
((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().x+0.1*dt,0.5*
this
->_y));
}
}
else
{
((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().x,((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().y-100.0*dt));
}
}
}
具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
void MapLayer::mapCreate()
{
this
->_allMap=CCTMXTiledMap::create(
"map/rosemap.tmx"
);
this
->_allMap->setScale(0.5);
this
->addChild(
this
->_allMap);
this
->_allMap->layerNamed(
"fangluoxia"
)->setVisible(
true
);
//让防落下不可见
CCTMXObjectGroup* objGroup =
this
->_allMap->objectGroupNamed(
"pengzhuang"
);
CCArray* objArray = objGroup->getObjects();
if
( objArray&& objArray->count()>0)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(objArray, pObj)
{
CCDictionary* playerPointDic= (CCDictionary*)pObj;
//将对象转化成字典,便于获取键值
float x = playerPointDic->valueForKey(
"x"
)->floatValue();
this
->_y = playerPointDic->valueForKey(
"y"
)->floatValue();
}
}
}
void MapLayer::jlmInRoad(float dt)
{
if
(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_isJlmCreate)
{
CCSize mapTiledNum =
this
->_allMap->getMapSize();
CCSize tiledSize =
this
->_allMap->getTileSize();
CCPoint point1 =((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition();
int x =point1.x *2 / tiledSize.width;
int y =point1.y *2 / tiledSize.height;
/* Cocos2d-x的默认Y坐标是由下至上的,所以要做一个相减操作 */
y = mapTiledNum.height - y;
int tiledGid=_allMap->layerNamed(
"fangluoxia"
)->tileGIDAt(ccp(x,y));
if
(tiledGid!=0)
{
CCDictionary* propertiesDict = _allMap->propertiesForGID(tiledGid);
/* 取得格子的zhezhao属性值 */
const CCString* prop = propertiesDict->valueForKey(
"flx"
);
/* 判断zhezhao属性是否为true,是的话,不让玩家移动 */
if
(prop->m_sString.compare(
"true"
) == 0) {
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->getPosition().x,80));
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->_jumpDownHigh=90;
//((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)this->getParent())->_jlmLayer->_jlmSprite->getPosition().x+0.1*dt,100));
((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().x+0.1*dt,0.5*
this
->_y));
}
}
else
{
((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->setPosition(ccp(((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().x,((PmdInAllLayer*)
this
->getParent())->_jlmLayer->_jlmSprite->getPosition().y-100.0*dt));
}
}
}
|