1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//GameScene.h
#include "cocos2d.h"
USING_NS_CC;
class
GameScene :
public
cocos2d::Layer
{
public
:
static
cocos2d::Scene* createScene();
virtual
bool
init();
void
menuCallback(Ref* pSender);
//重写重力感应函数
virtual
void
didAccelerate(Acceleration* acc, Event* event);
CREATE_FUNC(GameScene);
private
:
//注意不能用auto关键字
Size size;
Sprite *sprite;
};
|
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
61
|
//GameScene.cpp
// Created by Jacedy on 14-8-11.
#include "GameScene.h"
USING_NS_CC;
cocos2d::Scene* GameScene::createScene()
{
auto
scene = Scene::create();
//创建一个场景
auto
layer = GameScene::create();
//创建一个图层
scene->addChild(layer);
return
scene;
}
//初始化当前的图层
bool
GameScene::init()
{
if
(!Layer::init())
//初始化父类
return
false
;
//获取屏幕大小
size = Director::getInstance()->getVisibleSize();
//auto size = Director::getInstance()->getWinSize();
//添加一个图片精灵
sprite = Sprite::create(
"OnePiece_1.png"
);
sprite->setPosition(Vec2(size.width/2, size.height/2));
sprite->setTag(12);
this
->addChild(sprite);
//开启重力感应
Device::setAccelerometerEnabled(
true
);
//创建事件监听对象
auto
listener = EventListenerAcceleration::create(CC_CALLBACK_2(GameScene::didAccelerate,
this
));
//在事件监听器中注册
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this
);
return
true
;
}
void
GameScene::didAccelerate(Acceleration* acc, Event* event)
{
//使精灵随重力感应移动
auto
sprite =
this
->getChildByTag(12);
//通过标签获取精灵
auto
pos = sprite->getPosition();
pos.x += acc->x;
pos.y += acc->y;
//pos.y += acc->y * 10; //调整敏感度
sprite->setPosition(pos);
}
void
GameScene::menuCallback(Ref* pSender)
{
}
|