所需的函数及资源文件:http://download.csdn.net/detail/sharkmarine/6800323
函数的具体使用:
1、添加头文件:#include "Joystick.h"
2、声明对应变量:HRocker *joyStick;
3、初始化
CCSprite *testPointL=CCSprite::create("t_u_0.png");//摇杆
CCSprite *testBGL=CCSprite::create("t_d.png");//摇杆背景
//最后面的参数为false则摇杆会在固定位置,为true则会跟着点击位置移动,固定的坐标课通过参数ccp(80.0f,80.0f)设置
joyStick=HRocker::HRockerWithCenter(ccp(80.0f,80.0f),60.0f ,testPointL ,testBGL,false);
addChild(joyStick, 1 );
4、用摇杆控制物体移动
需在update计时器函数中如此使用:
float s =GetinkAngle(joystick->centerPoint, joystick->currentPoint);
CCPoint pos=GetAngleMove(s,fish->moveStep);
fishPos.x+=pos.x;
fishPos.y+=pos.y;
fish->setPosition(fishPos);
fish是要随摇杆移动的物体
下面是对应的两个函数:
//得到两点角度
static inline float GetinkAngle(CCPoint piDot1, CCPoint piDot2)
{
return atan2(piDot2.x - piDot1.x, piDot2.y - piDot1.y);
}
static inline CCPoint GetAngleMove(float fAngle, float fLong = 1)
{
return ccp(sin(fAngle)*fLong, cos(fAngle)*fLong);
}