Cocos2d-x 如何实现不同高低台阶的跳跃

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));
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值