Cocos2d-X 摇杆的使用(Joystick)

所需的函数及资源文件: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);
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值