cocos2d-x摇杆(一)

cocos2d-x摇杆(一),第1张

概述先看效果图: 具体实现: Control.h #ifndef _CONTROL_H_#define _CONTROL_H_#include "cocos2d.h"USING_NS_CC;class Control : public cocos2d::CCLayer{public: virtual bool init(); CREATE_FUNC(Control); virtual
先看效果图:  具体实现: Control.h   
#ifndef _CONTRol_H_#define _CONTRol_H_#include "cocos2d.h"USING_NS_CC;class Control : public cocos2d::cclayer{public:	virtual bool init();	CREATE_FUNC(Control);	virtual voID onEnter();	virtual voID onExit();	virtual bool cctouchBegan(CCtouch* touch,CCEvent* event);    virtual voID cctouchmoved(CCtouch* touch,CCEvent* event);    virtual voID cctouchended(CCtouch* touch,CCEvent* event);private:	CCSize visibleSize; //可视窗口大小	CCSprite* control_bg;	CCSprite* cen;	voID initData();	bool isOnControl;	CCPoint origin; //摇杆中心点	float r_control;};#endif

Control.cpp
#include"Control.h"USING_NS_CC;bool Control::init(){	if (!cclayer::init())	{		return false;	}	initData();	return true;}voID Control::initData(){	origin = ccp(150,150);  //摇杆原点		control_bg = CCSprite::create("dpadUp.png");	control_bg->setposition(origin);	control_bg->setScale(2);	this->addChild(control_bg);	cen = CCSprite::create("joystickUp.png");	cen->setposition(origin);	this->addChild(cen);	CCRect r = control_bg->boundingBox();	r_control = (r.getMaxX()-r.getMinX())/2; //摇杆半径}voID Control::onEnter(){	cclayer::onEnter();	CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,false);}bool Control::cctouchBegan(CCtouch* touch,CCEvent* event){	if (cen->boundingBox().containsPoint(touch->getLocation()))	{		isOnControl = true;		cen->setScale(1.1);	}	return true;}voID Control::cctouchmoved(CCtouch* touch,CCEvent* event){	if (isOnControl)  	{		CCPoint p_touch = touch->getLocation();		float distance = sqrt((p_touch.x - origin.x)*(p_touch.x - origin.x)+(p_touch.y - origin.y)*(p_touch.y - origin.y)); //触摸点和摇杆起点的距离		if (distance <= r_control)		{			cen->setposition(touch->getLocation());			cen->setScale(1.1);		}		else		{			float x = r_control/distance * (p_touch.x - origin.x)+origin.x;			float y = r_control/distance * (p_touch.y - origin.y)+origin.y;			cen->setposition(ccp(x,y));			cen->setScale(1.1);		}	}	else	{		//do nothing	}}voID Control::cctouchended(CCtouch* touch,CCEvent* event){	isOnControl = false;	cen->setposition(origin);	cen->setScale(1);}voID Control::onExit(){	CCDirector::sharedDirector()->gettouchdispatcher()->removeAllDelegates();	cclayer::onExit();}

在场景中使用:
control = Control::create();	this->addChild(control,1);


代码和素材下载地址: 总结

以上是内存溢出为你收集整理的cocos2d-x摇杆(一)全部内容,希望文章能够帮你解决cocos2d-x摇杆(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1061700.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存