Cocos2d-x--伪物理世界,实现Box2D弹球效果

实现仿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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值