//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));
this ->addChild(sprite);
//设定滑动距离
moveDistance = 30;
//创建监听事件对象
auto listener = EventListenerTouchOneByOne::create();
//定义监听事件的回调函数
listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this );
listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this );
listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this );
//在事件分发器中注册
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this );
return true ;
}
void GameScene::funCallback( float dt)
{
auto sub = Vec2(prePos, curPos);
//判断水平和垂直方向上哪个偏移量大
//水平移动
if ( fabs (sub.x) > fabs (sub.y))
{
//右滑
if (sub.x > moveDistance)
log ( "right" );
else
//左滑
if (sub.x < -moveDistance)
log ( "left" );
}
else
{
//上滑
if (sub.y > moveDistance)
log ( "up" );
else
//下滑
if (sub.y < -moveDistance)
log ( "down" );
}
}
bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)
{
auto pos = touch->getLocation();
//保存起始点
prePos = pos;
scheduleOnce(schedule_selector(GameScene::funCallback), 0.3f);
return true ;
}
void GameScene::onTouchMoved(Touch *touch, Event *unused_event)
{
auto pos = touch->getLocation();
//随时获取当前移动点的位置
curPos = pos;
}
void GameScene::onTouchEnded(Touch *touch, Event *unused_event)
{
auto pos = touch->getLocation();
}
|