cocos2d-x(2.1.4)之虚拟摇杆SneakyInput

这篇文章中,我们将用另一个虚拟摇杆类,SneakyInput,这个是开源的,已经有c++版,SneakInput c++的源码下载地址为:https://github.com/Ntran013/SneakyInput

注意,这个下载下来需要修改,因为那是基于老版本的cocos2dx的。已修改后的源码下载地址为:http://download.csdn.net/detail/my183100521/6620631

使用SneakyInput示例如下:

HelloWorldScene.h

    void update(float t);
    
    cocos2d::CCSprite *iconSprite;
    
    SneakyJoystick * joystick;

HelloWorldScene.cpp

bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1);


    CCSize wsize = CCDirector::sharedDirector()->getVisibleSize();
    
    iconSprite=CCSprite::create("Icon-72.png");
    
    iconSprite->setPosition(ccp(wsize.width/2, wsize.height/2));
    iconSprite->setTag(123);
    
    this->addChild(iconSprite, 1);
    
    float joystickRadius = 220;
    
    joystick = new SneakyJoystick();
    joystick->autorelease();
    joystick->initWithRect(CCRectZero);
    //是否自动回到中心 
    joystick->setAutoCenter(true);
    //是否支持死亡区域,该区域不会触发
    joystick->setHasDeadzone(true);
    //死亡区域半径
    joystick->setDeadRadius(10);
    
    SneakyJoystickSkinnedBase *joystickSkin = new SneakyJoystickSkinnedBase();
    joystickSkin->autorelease();
    joystickSkin->init();
    //背景
    joystickSkin->setBackgroundSprite(CCSprite::create("joystick1.png"));
    //中心点
    joystickSkin->setThumbSprite(CCSprite::create("joystick2.png"));
    joystickSkin->getThumbSprite()->setScale(1.0f);
    joystickSkin->setPosition(CCPointMake(joystickRadius,joystickRadius));
    joystickSkin->setJoystick(joystick);
    
    this->addChild(joystickSkin);
    
    this->scheduleUpdate();
    
    return true;
}

更新代码:

void HelloWorld::update(float t)
{
    //  getVelocity()到的数值很小 需要放大  
    CCPoint poi = ccpMult(joystick->getVelocity(), 100);
    
    //right
    if ((poi.x  >  0)  && (poi.x - poi.y) >0 && (poi.x + poi.y) > 0)
    {

        iconSprite->setPosition(ccp(iconSprite->getPosition().x+1, iconSprite->getPosition().y));

    }
    //left
    else if ( (poi.x < 0)  && (poi.x + poi.y) < 0 &&(poi.x - poi.y) < 0)
    {

        iconSprite->setPosition(ccp(iconSprite->getPosition().x-1, iconSprite->getPosition().y));
    }
    //up
    else if ((poi.y > 0) &&(poi.y - poi.x) > 0 &&(poi.y + poi.x) >0 )
    {

        iconSprite->setPosition(ccp(iconSprite->getPosition().x, iconSprite->getPosition().y+1));
    }
    //down
    else if ((poi.y < 0) &&(poi.y - poi.x) < 0 && (poi.y + poi.x) < 0)
    {
        iconSprite->setPosition(ccp(iconSprite->getPosition().x, iconSprite->getPosition().y-1));
    }
}

在上面中,我们初始化了一个精灵,并且初始化了 SneakyInput,然后再刷新函数里,我们去判断摇杆方向,然后移动精灵

效果图:




参考博文:http://blog.csdn.net/kuloveyouwei/article/details/9102623


转载请注明出处:http://blog.csdn.net/rexuefengye/article/details/16855971

麻烦了。呵呵〜〜〜

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值