AKever

导航

Cocos2dx(1)-摇杆(Joystick)

摇杆作为游戏的控制,已是作为学做游戏的基础部分!实现部分,应该将摇杆是现在一个层(cocos2dx-layer),这个层为控制层!一下是实现代码!简单,不一一介绍... ...

JoystickLayer.h

#ifndef __JOYSTICKLAYER_H__
#define __JOYSTICKLAYER_H__

#include "cocos2d.h"
USING_NS_CC;

class JoystickLayer : public cocos2d::CCLayer
{
public:
    virtual bool init();
    CREATE_FUNC(JoystickLayer);

    //get the direction and velocity of joystick and make it public
    CCPoint getDirection();
    float getVelocity();
    
private:
    JoystickLayer(void);
    ~JoystickLayer(void);

    CCSprite* joyStick;
    CCSprite* joyPlate;
    CCPoint centerPoint;    //the center of joystick
    CCPoint currentPoint;    //the current position of joystick
    float radius;            //the radius of joystick

    void updateJoystick(float dt);

    //Touch callback
    virtual void registerWithTouchDispatcher();
    virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent);
    virtual void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent);
    virtual void ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent);
};

#endif

JoystickLayer.cpp

#include "JoystickLayer.h"

USING_NS_CC;

JoystickLayer::JoystickLayer(void)
{
}


JoystickLayer::~JoystickLayer(void)
{
}

bool JoystickLayer::init()
{   
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    joyPlate = CCSprite::create("joystick_plate.png");
    joyStick = CCSprite::create("joystick.png");
    this->addChild(joyPlate);
    this->addChild(joyStick);

    radius = joyPlate->getContentSize().width*0.5f;
    centerPoint = ccp(origin.x+winSize.width*0.01f+radius, origin.y+winSize.height*0.01f+radius);
    currentPoint =centerPoint;

    joyPlate->setPosition(centerPoint);
    joyStick->setPosition(centerPoint);

    this->setTouchEnabled(true);
    this->schedule(schedule_selector(JoystickLayer::updateJoystick));

    return true;
}


//获取摇杆的方向
CCPoint JoystickLayer::getDirection()
{
    return ccpNormalize(ccpSub(centerPoint, currentPoint));
}

//获取摇杆的力度,摇杆点与中心的距离
float JoystickLayer::getVelocity()
{
    return ccpDistance(centerPoint, currentPoint);
}

void JoystickLayer::registerWithTouchDispatcher()
{
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

bool JoystickLayer::ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
{
    CCPoint touchPoint = pTouch->getLocationInView();
    touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
    if(ccpDistance(touchPoint, centerPoint) > radius) 
        return false;
    currentPoint = touchPoint;
    return true;
}

void JoystickLayer::ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent)
{
    CCPoint touchPoint = pTouch->getLocationInView();
    touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
    if(ccpDistance(touchPoint, centerPoint) > radius)
    {
        currentPoint = ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
    }
    else
    {
        currentPoint = touchPoint;
    }
}

void JoystickLayer::ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent)
{
    currentPoint = centerPoint;
}

//更新摇杆的走动,ccpAdd
void JoystickLayer::updateJoystick(float dt) { joyStick->setPosition(ccpAdd(joyStick->getPosition(), ccpMult(ccpSub(currentPoint, joyStick->getPosition()), 0.5f))); }

在需要添加摇杆的场景(scene),只需要添加以下代码,就是把layer层添加进scene

JoystickLayer* joystickLayer = JoystickLayer::create();
scene->addChild(joystickLayer);

摇杆已实现!

转载:http://www.cnblogs.com/anndaming/archive/2012/02/13/2349017.html

posted on 2013-12-27 09:36  AKever  阅读(893)  评论(0)    收藏  举报