从今天起寒風的旅途由cocos2d-x变为Cocos了。因为时代在进步。。。好了废话不多说,下面进入正题。
我们在做飞机大战等一系列小游戏中,会遇到最高分问题。那么这时候我们有两种选择,一种是连数据库,一种是用UserDefault。
在这里,我用我的代码为大家说明:
//分数 auto labelScore=Label::createWithSystemFont("分数:0","DFGirl",40); labelScore->setName("labelScore"); labelScore->setPosition(Vec2(size.width/2-20,size.height/2+115)); labelScore->setColor(Color3B::BLACK); //labelScore->setOpacity(0); this->addChild(labelScore,6); auto scoreLabel=(Label *)this->getChildByName("labelScore"); scoreLabel->setString(String::createWithFormat("分数:%d",score)->_string); Label * highScoreLabel=nullptr; if (score>_highScore) { UserDefault::getInstance()->setIntegerForKey("HighScore",score); _highScore=score; highScoreLabel=Label::createWithSystemFont("恭喜您突破自我!\n 最高分",20); highScoreLabel->setColor(Color3B::RED); highScoreLabel->setHorizontalAlignment(TextHAlignment::CENTER); highScoreLabel->setPosition(Vec2(size.width/2-20,size.height/2+145)); } else { highScoreLabel=Label::createWithSystemFont(StringUtils::format("你的最高分:%d",_highScore),"DFgirl",20); highScoreLabel->setColor(Color3B::RED); highScoreLabel->setPosition(Vec2(size.width/2-20,size.height/2+145)); } this->addChild(highScoreLabel,6); }
首先定义一个分数来记录,你的最终得分,然后定义一个最高分。然后判断如果当前分数大于最高分,那么就让当前分数给最高分赋值。否则,显示您的最高记录是:XXX。
void GameScene::onEnter() { Layer::onEnter(); _highScore = UserDefault::getInstance()->getFloatForKey("HighScore",0); SimpleAudioEngine::getInstance()->playBackgroundMusic("stones_v.4_loop.mp3",true); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。