cocos2d-x 공부용2014. 9. 5. 16:42

터치 관련으로 문제가 많아서ㅡㅡ; 그거 좀 해결하느라 하루종일 아무것도 못적었네요.

우선 터치 관련만 정리해둡니다.


참조

http://mightyfine.egloos.com/viewer/294494

http://plming.tistory.com/83

http://blog.naver.com/cdyho/220068792495


그리고 중간에 이상한 문제가 터져서 그걸 고쳐보겠다고 GCC바꾸고 별 난리를 치다가..포기하고 프로젝트를 새로 만들었습니다. 그리고 어느 고마우신 분이 3.2버전 수정 소스를 올리셨다고 해서 덮어씌웠고, 지금은 문제 없이 진행중입니다.

관련 주소 : http://cafe.naver.com/cocos2dxusers/20933 (로긴 및 가입 필요)


터치에 반응하는건 성공 했는데, 다른 중대한 문제가 생겼네요. 머리 터지겠습니다 아주.

아래는 TouchBegan만 적용하는 소스입니다.


1. 싱글 터치

HelloWorldScene.h

using namespace cocos2d;

class HelloWorld : public cocos2d::Layer
{ ..
virtual bool onTouchBegan(Touch* touch, Event* event);
..
};

cpp에 해당하는 함수를 빈상태라도 구현하지 않으면 위에만 써놓고 빌드했을때 에러납니다.


HelloWorldScene.cpp

bool HelloWorld::init() {
	//////////////////////////////
	// 1. super init first
	if (!Layer::init()) {
		return false;
	}

	_screenSize = Director::sharedDirector()->getWinSize();

	this->setTouchEnabled(true);

	//디스패처. 리스너와 오브젝트를 연결해주는 역할
	EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
	//터치 위치를 알려주는 리스너. 단일 터치.
	//바로 만들어쓰는 식별자는 auto를 사용한다.
	auto positionListener = EventListenerTouchOneByOne::create();
	//zOrder에 따라 밑에 깔린애도 동작할지 아닐지를 결정한다.
	positionListener->setSwallowTouches(true);
	//콜백 함수 대입
	positionListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
	//디스패처를 이용해 객체와 리스너를 이어준다. 화면 전체를 터치할 수 있게 만들어야 하므로 객체는 this
	dispatcher->addEventListenerWithSceneGraphPriority(positionListener, this);

	createGameAction();
	createGameScene();

	return true;
}

..

bool HelloWorld::onTouchBegan(Touch* touch, Event* event) {
	//getCurrentTarget은 Node를 반환한다. 이것이 터치한 오브젝트가 된다.
	auto target = event->getCurrentTarget();
	Point location = target->convertToNodeSpace(touch->getLocation());

	return true;
}

2. 멀티 터치


HelloWorldScene.h

using namespace cocos2d;
using namespace std;

class HelloWorld : public cocos2d::Layer
{
..

    virtual void onTouchesBegan(const std::vector<Touch*> &touches, Event* event);

..
};

HelloWorldScene.cpp

bool HelloWorld::init() {
	//////////////////////////////
	// 1. super init first
	if (!Layer::init()) {
		return false;
	}

	_screenSize = Director::sharedDirector()->getWinSize();

	this->setTouchEnabled(true);

	//디스패처. 리스너와 오브젝트를 연결해주는 역할
	EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher();
	//터치 위치를 알려주는 리스너. 단일 터치.
	//바로 만들어쓰는 식별자는 auto를 사용한다.
	auto positionListener = EventListenerTouchAllAtOnce::create();
	//콜백 함수 대입
	positionListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
	//디스패처를 이용해 객체와 리스너를 이어준다. 화면 전체를 터치할 수 있게 만들어야 하므로 객체는 this
	dispatcher->addEventListenerWithSceneGraphPriority(positionListener, this);

	return true;
}

void HelloWorld::onTouchesBegan(const std::vector<Touch*> &touches, Event* event) {
	for (auto iter = touches.begin(); iter != touches.end(); iter++){
	      Point location = (*iter)->getLocation();

	    }
}

멀티 터치에서는 onTouchesBegan이 void로 바뀝니다. 이거 중요합니다. 이걸 몰라서 오류랑 많이 놀았습니다..

여튼 싱글터치도 멀티 터치도 잘 됩니다. 


여튼 추석 잘 보내시구요, 추석때는 다른 할일이 있어서 추석 끝나고 다시 이어서 할 것 같습니다.

Posted by 아이시네프