【问题标题】:Cocos2d-x: Create simple progress barCocos2d-x:创建简单的进度条
【发布时间】:2013-07-18 02:52:16
【问题描述】:

我是 Cocos2d-x 的新手。

我想为我的游戏创建简单的进度/更新栏。

当这个进度条已满时,我们将进入下一个级别。

我怎样才能创建那个栏。

感谢您的所有帮助。

【问题讨论】:

    标签: android cocos2d-x


    【解决方案1】:

    看到这个 - How to use a progress bar in cocos2d-x and C++

    基本上,创建两个精灵,一个用于进度条的边框,一个用于加载栏本身。

    CCPointer  fuelBarBorder;
    
    fuelBarBorder = 
         CCSprite::createWithSpriteFrameName ("bt_progressbarborder.png" );
    fuelBarBorder->setPosition(ccp(100,100));
    this->addChild(fuelBarBorder,1);
    
    
    
    
    // CCProgresstimer object (smart pointer) 
    CCPointer  fuelBar; 
    fuelBar = CCProgressTimer::create(
         CCSprite::createWithSpriteFrameName ("bt_progressbar.png" ));
    

    将加载条精灵的类型设置为CCProgressTimerType

    // Set this progress bar object as kCCProgressTimerTypeBar (%)
    fuelBar->setType(CCProgressTimerType::kCCProgressTimerTypeBar);
    
    // Set anchor point in 0,0 and add it as a child to our border sprite
    fuelBar->setAnchorPoint(ccp(0,0));
    
    fuelBar->setBarChangeRate(ccp(1,0)); // To make width 100% always
    fuelBar->setTag(1);                  // Tag our object for easy access
    
    fuelBarBorder->addChild(fuelBar,50); // Add it inside the border sprite
    

    在您的更新方法中,更改其percentage 以反映加载百分比。

    fuelBar->setPercentage(80); // Value between 0-100
    

    【讨论】:

    • 我尝试了,但是 CCPointer 不存在,我不知道为什么。
    • 我有最新版本的Cocos2d-x
    • @Tom 只需使用 CCSprite 代替 CCPointer ,它应该可以工作
    • 谢谢,我会在我的项目中尝试
    【解决方案2】:

    我写了原始帖子(上面的链接)。

    感谢这篇文章,我发现 WordPress 在保存代码时玩了我...... :(。

    有一些更正:

    CCPointer <CCSprite> fuelBarBorder; 
    
    fuelBarBorder = 
      CCSprite::createWithSpriteFrameName ("bt_progressbarborder.png" ); 
    
    fuelBarBorder->setPosition(ccp(100,100)); 
    
    this->addChild(fuelBarBorder,1);
    

    这是第一组,你可以看到唯一的变化是在第一行:

    CCPointer <CCSprite> fuelBarBorder;
    

    如果你没有这个 cocos2d-x 扩展,就用下面这个:

    CCSprite * fuelBarBorder;
    

    第二组代码也是这样,正确的是:

    CCPointer <CCProgressTimer> fuelBar;
    
    fuelBar = CCProgressTimer::create(
     CCSprite::createWithSpriteFrameName ("bt_progressbar.png" ));
    // Set this progress bar object as kCCProgressTimerTypeBar (%)
    fuelBar->setType(CCProgressTimerType::kCCProgressTimerTypeBar);
    
    // Set anchor point in 0,0 and add it as a child to our border sprite
    fuelBar->setAnchorPoint(ccp(0,0));
    
    fuelBar->setBarChangeRate(ccp(1,0)); // To make width 100% always
    fuelBar->setTag(1);                  // Tag our object for easy access
    
    fuelBarBorder->addChild(fuelBar,50); // Add it inside the border sprite
    

    同样的事情是使用 CCPointer(智能指针实现),如果您的项目中没有它,只需更改以下行:

    CCPointer <CCProgressTimer> fuelBar;
    

    这个:

    CCProgressTimer fuelBar;
    

    这应该可以使代码正常工作,我希望这会有所帮助!!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      相关资源
      最近更新 更多