1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//GameScene.h
#include"cocos2d.h"
USING_NS_CC;
class
GameScene:
public
cocos2d::Layer
{
:
static
cocos2d::Scene*createScene();
virtual
bool
init();
onTouchBegan(Touch*touch,Event*unused_event);
virtual
void
onTouchMoved(Touch*touch,Event*unused_event);
onTouchEnded(Touch*touch,Event*unused_event);
menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};
|
cocos2d::Scene*GameScene::createScene()
{
auto
scene=Scene::create();
//创建一个场景
layer=GameScene::create();
//创建一个图层
scene->addChild(layer);
return
scene;
}
//初始化当前的图层
GameScene::init()
{
if
(!Layer::init())
//初始化父类
return
false
;
//获取屏幕大小
SizevisibleSize=Director::getInstance()->getVisibleSize();
sprite=Sprite::create(
"OnePiece_1.png"
);
sprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this
->addChild(sprite);
sprite1=Sprite::create(
"Flag_1.png"
);
sprite1->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
sprite1->setTag(12);
@H_35_404@->addChild(sprite1);
//1.注册监听事件对象
listener=EventListenerTouchOneByOne::create();
//单点触摸
//autolistener=EventListenerTouchAllAtOnce::create();//多点触摸
//setTouchEnabled(true);//3.0版本已经被弃用
//2.定义监听对象的回调方法
listener->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan,
);
listener->onTouchMoved=CC_CALLBACK_2(GameScene::onTouchMoved,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">);
listener->onTouchEnded=CC_CALLBACK_2(GameScene::onTouchEnded,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">);
//3.在事件分发器中注册
_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">);
true
@H_404_458@}
;
//触摸事件函数(内部已创建,这里重新编写)
GameScene::onTouchBegan(Touch*touch,Event*unused_event)
{
//touch->getLocation();//获得的是OpenGL坐标
//autop1=touch->getLocationInView();//获得的是UI坐标
//autop2=Dirctor::getInstance()->convertToGL(p1);//转换成OpenGL坐标
CCLOG(
"TouchBeganx:%lf,y:%lf"
,touch->getLocation().x,touch->getLocation().y);
sprite=
->getChildByTag(12);
//通过标识获取精灵
//sprite->setPosition(Vec2(touch->getLocation().x,touch->getLocation().y));
sprite->runAction(MoveTo::create(0.5f,Point(touch->getLocation().x,touch->getLocation().y)));
;
}
GameScene::onTouchMoved(Touch*touch,Event*unused_event)
{
//图标随鼠标一起移动
//autosprite=this->getChildByTag(12);//通过标识获取精灵
}
GameScene::onTouchEnded(Touch*touch,Event*unused_event)
{
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//GameScene.h
#include"cocos2d.h"
USING_NS_CC;
class
GameScene:
public
cocos2d::Layer
{
:
static
cocos2d::Scene*createScene();
virtual
bool
init();
menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};
|
cocos2d::Scene*GameScene::createScene()
{
auto
scene=Scene::create();
//创建一个场景
layer=GameScene::create();
//创建一个图层
scene->addChild(layer);
return
scene;
}
//初始化当前的图层
GameScene::init()
{
if
(!Layer::init())
//初始化父类
return
false
;
//获取屏幕大小
SizevisibleSize=Director::getInstance()->getVisibleSize();
sprite=Sprite::create(
"OnePiece_1.png"
);
this
->addChild(sprite);
sprite1=Sprite::create(
"Flag_1.png"
);
sprite1->setTag(12);
@H_35_404@->addChild(sprite1);
//1.注册监听事件对象
listener=EventListenerTouchOneByOne::create();
//单点触摸
//autolistener=EventListenerTouchAllAtOnce::create();//多点触摸
//setTouchEnabled(true);//3.0版本已经被弃用
//2.定义监听对象的回调方法
);
);
);
//3.在事件分发器中注册
);
true
@H_404_458@}
;
//触摸事件函数(内部已创建,这里重新编写)
{
//touch->getLocation();//获得的是OpenGL坐标
//autop1=touch->getLocationInView();//获得的是UI坐标
//autop2=Dirctor::getInstance()->convertToGL(p1);//转换成OpenGL坐标
CCLOG(
sprite=
->getChildByTag(12);
//通过标识获取精灵
;
}
{
//图标随鼠标一起移动
//autosprite=this->getChildByTag(12);//通过标识获取精灵
}
{
|