//GameScene.cpp
//可参考MutiTouchTest.cpp
#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();
auto sprite = Sprite::create( "Icon.png" );
sprite->setPosition(Vec2(size.width/2, size.height/2));
sprite->setTag(12);
this ->addChild(sprite);
//1.注册监听事件对象
auto listener = EventListenerTouchAllAtOnce::create(); //多点触摸
//2.定义监听对象的回调方法
listener->onTouchesBegan = CC_CALLBACK_2(GameScene::onTouchesBegan, this );
listener->onTouchesMoved = CC_CALLBACK_2(GameScene::onTouchesMoved, this );
listener->onTouchesEnded = CC_CALLBACK_2(GameScene::onTouchesEnded, this );
listener->onTouchesCancelled = CC_CALLBACK_2(GameScene::onTouchesCancelled, this );
//3.在事件监听器中注册(事件监听器包含:触摸事件、键盘相应事件、加速记录事件、鼠标响应事件、自定义事件)
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this );
return true ;
}
void GameScene::menuCallback(Ref* pSender)
{
}
//触摸开始时调用
void GameScene::onTouchesBegan( const std::vector<Touch*>& touches, Event *unused_event)
{
for ( auto &item: touches) //遍历容器中的各个成员!!!多点触摸时将显示同时显示多个精灵
{
auto touch = item;
auto location = touch->getLocation();
//加载一个精灵
auto sprite = Sprite::create( "Icon.png" );
sprite->setPosition(location);
addChild(sprite);
}
}
//触摸移动时调用
void GameScene::onTouchesMoved( const std::vector<Touch*>& touches, Event *unused_event)
{
}
//触摸结束时调用
void GameScene::onTouchesEnded( const std::vector<Touch*>& touches, Event *unused_event)
{
}
//取消触摸时调用
void GameScene::onTouchesCancelled( const std::vector<Touch*>&touches, Event *unused_event)
{
}
|