实现仿Box2D弹球效果,看上去像是自由落体运动和自由弹起效果
实现思路是使每帧在方向上的位移增量递增
步骤:
1.新建一个Cocos2d-x工程
2.在HelloWorldScene.h文件中增加一个函数,一个精灵和一个变量
3.在HelloWorldScene.cpp文件中实例化精灵,开启scheduleUpdate()
实现:
1.新建一个Cocos2d-x工程
2.在HelloWorldScene.h文件中增加一个函数,一个精灵和一个变量
using namespace cocos2d;
在public下加入
virtual void update(float dt);
CCSprite *test;
float currentY;
3.在HelloWorldScene.cpp文件中实例化精灵,开启scheduleUpdate()
在init()函数中加入:
test = CCSprite::create("CloseNormal.png");
CC_BREAK_IF(! test);
test->setPosition(ccp(size.width / 2, size.height / 2));
this->addChild(test);
currentY = 0;
this->scheduleUpdate();
添加函数:
void HelloWorld::update(float dt)
{
currentY += -450 * dt;
test->setPositionY(test->getPositionY() + currentY * dt);
if(test->getPositionY() < 5) currentY = 350;
}