cocos::触摸跟随移动

前端之家收集整理的这篇文章主要介绍了cocos::触摸跟随移动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#pragma once
#ifndef __CONTROLL_MOVE_H_
#define __CONTROLL_MOVE_H_

#include "cocos2d.h"
USING_NS_CC;
class ControllMoveLayer :public Layer
{
public :
	CREATE_FUNC(ControllMoveLayer);
	bool init();
	void setPlayer(Node *player);
private:
	void update(float dt);
	bool onTouchBegan(Touch *touch,Event *unused_event);
	void onTouchCancelled(Touch *touch,Event *unused_event);
	void onTouchMoved(Touch *touch,Event *unused_event);
	void onTouchEnded(Touch *touch,Event *unused_event);
	float mDeltaX;
	float mDeltaY;
	Node *mPlayer;
	bool isControlled;
};
#endif



#include "ControllMoveLayer.h"
bool ControllMoveLayer::init() {
	if (!Layer::init()) {
		return false;
	}
	mPlayer = NULL;
	mDeltaX = 0;
	mDeltaY = 0;
	isControlled = false;
	scheduleUpdate();
	return true;
}

void  ControllMoveLayer::setPlayer(Node *player) {

	if (mPlayer != NULL) {
		removeChild(player);
		CC_SAFE_DELETE(player);
	}

	mPlayer = player;
	addChild(player);

	auto listeScene = EventListenerTouchOneByOne::create();
	listeScene->onTouchBegan = CC_CALLBACK_2(
		ControllMoveLayer::onTouchBegan,this);

	listeScene->onTouchCancelled = CC_CALLBACK_2
		(ControllMoveLayer::onTouchCancelled,this);

	listeScene->onTouchMoved = CC_CALLBACK_2(
		ControllMoveLayer::onTouchMoved,this);

	listeScene->onTouchEnded = CC_CALLBACK_2(
		ControllMoveLayer::onTouchEnded,this);

	Director::getInstance()->getEventDispatcher()
		->addEventListenerWithSceneGraPHPriority(listeScene,this);
}

void  ControllMoveLayer::update(float dt) {
	if (mPlayer == NULL) {
		return;
	}
}
bool ControllMoveLayer::onTouchBegan(Touch *touch,Event *unused_event) {
	auto playerBox = mPlayer->getBoundingBox();
	auto touchPos = Director::getInstance()
		->convertToGL(touch->getLocationInView());
	if (playerBox.containsPoint(touchPos)) {
		isControlled = true;
		auto playerPos = mPlayer->getPosition();
		mDeltaX = touchPos.x - playerPos.x;
		mDeltaY = touchPos.y - playerPos.y;
		
		return true;
	}
	return true;
}

void  ControllMoveLayer::onTouchCancelled(Touch *touch,Event *unused_event) {
	isControlled = false;
}
void  ControllMoveLayer::onTouchMoved(Touch *touch,Event *unused_event) {

	if (isControlled) {

		Point mMovedPos = touch->getLocationInView();
		mMovedPos = Director::getInstance()->convertToGL(mMovedPos);

		Size visibleSize = Director::getInstance()->getVisibleSize();
		Point origin = Director::getInstance()->getVisibleOrigin();

		//記得減去偏移量  
		float x = mMovedPos.x - mDeltaX;
		float y = mMovedPos.y - mDeltaY;

		//x到达屏幕左边界  
		if (x <= mPlayer->getContentSize().width / 2 + origin.x)
			x = mPlayer->getContentSize().width / 2 + origin.x;
		//x到达屏幕右边界  
		else if (x >= visibleSize.width - mPlayer->getContentSize().width / 2)
			x = visibleSize.width - mPlayer->getContentSize().width / 2;

		//y到达屏幕下边界  
		if (y <= mPlayer->getContentSize().height / 2 + origin.y)
			y = mPlayer->getContentSize().height / 2 + origin.y;
		//x到达屏幕上边界
		else if (y >= visibleSize.height - mPlayer->getContentSize().height / 2)
			y = visibleSize.height - mPlayer->getContentSize().height / 2;

		//飞机跟随手指移动  
		mPlayer->setPosition(Point(x,y));
	}
}

void  ControllMoveLayer::onTouchEnded(Touch *touch,Event *unused_event) {
	isControlled = false;

}
原文链接:https://www.f2er.com/cocos2dx/339926.html

猜你在找的Cocos2d-x相关文章