Cocos2d-x加速度计实例:运动的小球
下面我们通过一个实例介绍一下如果通过层加速度计事件实现访问加速度计。该实例场景如下图所示场景中有一个小球当我们把移动设备水平放置屏幕向上然后左右晃动移动设备来改变小球的位置。
下面我们再看看具体的程序代码首先看一下HelloWorldScene.h文件它的代码如下
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#define kBall_Tag 100 ①
#define SPEED 30.0 ②
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
virtual void onEnter();
virtual void onExit();
virtual voidonAcceleration(cocos2d::Acceleration* acc, cocos2d::Event *unused_event); ③
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
上述代码第①行定义宏kBall_Tag它是小球精灵的标签Tag数值。第②行定义宏SPEED它表示小球运动的速度。第③行代码声明了onAcceleration函数。
HelloWorldScene.cpp文件它的主要代码如下
bool HelloWorld::init()
{
if( !Layer::init() )
{
returnfalse;
}
SizevisibleSize = Director::getInstance()->getVisibleSize();
Pointorigin = Director::getInstance()->getVisibleOrigin();
//贴图的纹理图片宽高必须是2的n次幂128x128
autobg = Sprite::create("BackgroundTile.png",
Rect(0,0, visibleSize.width, visibleSize.height));
//贴图的纹理参数水平重复平铺垂直重复平铺
Texture2D::TexParamstp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
bg->getTexture()->setTexParameters(tp);
bg->setPosition(origin+ Point(visibleSize.width/2, visibleSize.height/2));
addChild(bg,0);
autoball = Sprite::create("Ball.png");
ball->setPosition(origin+Point(visibleSize.width/2,visibleSize.height/2));
addChild(ball,10, kBall_Tag);
returntrue;
}
void HelloWorld::onEnter()
{
Layer::onEnter();
log("HelloWorldonEnter");
setAccelerometerEnabled(true); ①
}
void HelloWorld::onAcceleration(Acceleration*acc, Event *unused_event)
{
log("{x = %f, y = %f}", acc->x,acc->y);
Size visibleSize = Director::getInstance()->getVisibleSize();
Sprite* ball = (Sprite*)this->getChildByTag(kBall_Tag); ②
Size s = ball->getContentSize(); ③
Point p0 = ball->getPosition(); ④
float p1x = p0.x + acc->x *SPEED ; ⑤
if ((p1x - s.width/2) <0) { ⑥
p1x = s.width/2; ⑦
}
if ((p1x + s.width / 2) > visibleSize.width) { ⑧
p1x = visibleSize.width - s.width / 2; ⑨
}
float p1y = p0.y + acc->y *SPEED ;
p1y = p1y < 0 ? -p1y : p1y;
if ((p1y - s.height/2) < 0) {
p1y = s.height/2;
}
if ((p1y + s.height/2) > visibleSize.height) {
p1y = visibleSize.height - s.height/2;
}
ball->runAction(Place::create(Point( p1x, p1y))); ⑩
}
void HelloWorld::onExit()
{
Layer::onExit();
log("HelloWorldonExit");
}
上述代码①行开启加速计设备这个代码是在HelloWorld::onEnter()函数中意味着在进入层的时候就开启加速度计设备。
在第②行代码是通过标签属性获得小球精灵对象。第③行代码ball->getContentSize()获得小球尺寸大小。第④行代码ball->getPosition()是获得小球的位置。第⑤行代码是p0.x + acc->x * SPEED是获得小球的x轴方向移动的位置但是需要考虑左右超出屏幕的情况第⑥行代码是 (p1x - s.width/2) <0是判断超出左边屏幕这种情况下我们需要通过第⑦行代码p1x = s.width/2重新设置它的x轴坐标。第⑧行代码(p1x+ s.width / 2) > visibleSize.width是判断超出右边屏幕这种情况下我们需要通过第⑨行代码p1x = visibleSize.width - s.width / 2重新设置它的x轴坐标。类似的判断y轴也需要代码就不再解释了。
重新获得小球的坐标位置后通过第⑩行代码ball->runAction(Place::create(Point( p1x, p1y)))是执行一个动作使小球移动到新的位置。
《Cocos2d-x实战 C++卷》现已上线各大商店均已开售
京东https://item.jd.com/11584534.html
当当https://product.dangdang.com/23606265.html
互动出版网https://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源码及样章下载地址
源码下载地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1
最后更新:2017-04-03 05:39:33