Saturday, November 23, 2024
HomeGame DevelopmentCocos2d-x virtualJoystick - C++ - Cocos Boards

Cocos2d-x virtualJoystick – C++ – Cocos Boards


Hello Cocos2d-x Builders,
I need assistance on tips on how to transfer a picture/sprite by the joystick.

These are the codes, i didn’t write this.

Joystick.cpp

#embrace "Joystick.h"

#outline PI 3.1415926

const std::string JoystickEvent::EVENT_JOYSTICK = "event_of_joystick";

JoystickEvent::JoystickEvent() {
	log(" JoystickEvent(), %p", this);
}

JoystickEvent::~JoystickEvent() {
	log("~JoystickEvent(), %p", this);
}

bool JoystickEvent::init() {
	return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------
Joystick::Joystick() {
}

Joystick::~Joystick() {
	// 取消事件监听
	_eventDispatcher->removeEventListenersForTarget(this);
}

bool Joystick::init() {
	bool outcome = false;
	do {
		// 父类初始化
		if (!Layer::init()) {
			break;
		}
		// joystick的背景
		mJsBg = Sprite::create("joystick_bg.png");
		if (nullptr == mJsBg) {
			break;
		}
		mJsBg->setPosition(mJsPos);
		addChild(mJsBg);

		// joystick的中心点
		mJsCenter = Sprite::create("joystick_center.png");
		if (nullptr == mJsCenter) {
			break;
		}
		mJsCenter->setPosition(mJsPos);
		addChild(mJsCenter);

		// contact event监听
		auto touchListener = EventListenerTouchOneByOne::create();
		if (nullptr == touchListener) {
			break;
		}
		touchListener->setSwallowTouches(true);
		touchListener->onTouchBegan =
				CC_CALLBACK_2(Joystick::onTouchBegan, this);
		touchListener->onTouchMoved =
				CC_CALLBACK_2(Joystick::onTouchMoved, this);
		touchListener->onTouchEnded =
				CC_CALLBACK_2(Joystick::onTouchEnded, this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener,
				this);

		outcome = true;
	} whereas (0);

	return outcome;
}

bool Joystick::onTouchBegan(Contact *contact, Occasion *unused_event) {
	log("onTouchBegan");
	auto level = touch->getLocation();
	if (mJsCenter->getBoundingBox().containsPoint(level)) {
		// 若触摸点在joystick的中心点,则继续接受事件
		return true;
	}
	// 否则不接受后续事件
	return false;
}

void Joystick::onTouchMoved(Contact *contact, Occasion *unused_event) {
	log("onTouchMoved");

	// 1. 获得角度,
	//第一象限是0,90度
	//第二象限是90,180度
	//第三象限是-90,-180度
	//第四象限是-90,0度
	Vec2 level = touch->getLocation();
	double y = level.y - mJsPos.y;
	double x = level.x - mJsPos.x;
	double angle = atan2(y, x) * 180 / PI;
	int j, o;
	double xpos, ypos;
	j = (int)x;
	o = (int)y;
	xpos = x - j;
	ypos = y - o;
	log("---------------------------------          %f", xpos);
	log("------------------------------------          %f", ypos);
	// 2. 更新joystick中心点位置,目的是想让中心点始终在它的背景图范围
	// joystick背景图半径
	double jsBgRadis = mJsBg->getContentSize().width * 0.5;
	//触摸点到中心点的实际距离
	double distanceOfTouchPointToCenter = sqrt(
			pow(mJsPos.x - level.x, 2) + pow(mJsPos.y - level.y, 2));
	if (distanceOfTouchPointToCenter >= jsBgRadis) {
		//利用等比关系计算delta x y
		double deltX = x * (jsBgRadis / distanceOfTouchPointToCenter);
		double deltY = y * (jsBgRadis / distanceOfTouchPointToCenter);
		mJsCenter->setPosition(Vec2(mJsPos.x + deltX, mJsPos.y + deltY));
	} else {
		mJsCenter->setPosition(level);
	}

	//	 3. 分发joystick occasion
	JoystickEvent* jsEvent = JoystickEvent::create();
	jsEvent->mAnagle = angle;
	Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(
			JoystickEvent::EVENT_JOYSTICK, jsEvent);
}

void Joystick::onTouchEnded(Contact *contact, Occasion *unused_event) {
	log("onTouchEnded");
	// 事件结束,还原joystick中心点位置
	mJsCenter->setPosition(mJsPos);
}

Joystick.h

#ifndef JOYSTICK_H_
#outline JOYSTICK_H_

#embrace "cocos2d.h"
utilizing namespace cocos2d;

class JoystickEvent: public Ref {
non-public:
	JoystickEvent();

public:
	digital ~JoystickEvent();
	digital bool init();CREATE_FUNC(JoystickEvent)
	;

public:
	static const std::string EVENT_JOYSTICK;
	double mAnagle;
//	double mX;
//	double mY;
};

class Joystick: public Layer {
non-public:
	Joystick();

protected:
	bool onTouchBegan(Contact *contact, Occasion *unused_event);
	void onTouchMoved(Contact *contact, Occasion *unused_event);
	void onTouchEnded(Contact *contact, Occasion *unused_event);

public:
	digital ~Joystick();
	digital bool init();CREATE_FUNC(Joystick)
	;

non-public:
	Sprite* mJsBg;
	Sprite* mJsCenter;
	Vec2 mJsPos = Vec2(100, 100);
};

#endif /* JOYSTICK_H_ */

Please assist me.


Ohhh thanks,
However how are you going to transfer the sprite?, are you able to train me as a result of i don’t know the way and im new in c++ programming.

Or how do you progress the sprite/picture by the ange.(To not rotate)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments