其中一些代码不会运行

时间:2017-02-07 14:32:04

标签: c++

我是C ++的初学者,我试图为Trinity Core World of Warcraft私人服务器创建一个脚本,但似乎有些代码成功运行而其他代码因未知原因而被转义..

这里是代码

#include "ScriptPCH.h"
#include "DisableMgr.h"

class LearnSpellsOnLevelUp : public PlayerScript 
{
public:
    LearnSpellsOnLevelUp()
        : PlayerScript("LearnSpellsOnLevelUp"){};

    void OnLevelChanged(Player* player, uint8 oldLevel)
    {
        if (player->getClass() == 3 && player->getLevel() >= 12) // ran
        {
            if (player->HasSpell(23356)) // ran
                return; // ran
            else // ran
                player->LearnSpell(23356, false); // ran
            player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Taming Lesson]!|r"); // ran
        }

        if (player->getClass() == 9) // Warlock  // ran
        {
            if (player->getLevel() >= 2) // ran
            {
                if (player->HasSpell(44163)) // ran
                    return;
                else
                    player->LearnSpell(44163, false); // ran
                player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Imp]!|r"); // ran
            }
            else if (player->getLevel() >= 10) 
            {
                if (player->HasSpell(25112))
                    return;
                else
                    player->LearnSpell(25112, false);
                player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Voidwalker]!|r");
            }
            else if (player->getLevel() >= 20) 
            {
                if (player->HasSpell(712))
                    return;
                else
                    player->LearnSpell(712, false);
                player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Succubus]!|r");
            }
            else if (player->getLevel() >= 30)
            {
                if (player->HasSpell(691))
                    return;
                else
                    player->LearnSpell(691, false);
                player->GetSession()->SendAreaTriggerMessage("|cff00FF00You Learnt new Spell [Summon Felhunter]!|r");
            }
            else
                return;
        }
    }
};

void AddSC_LearnSpellsOnLevelUp()
{
    new LearnSpellsOnLevelUp();
}

我在成功运行的代码旁边写了// ran

2 个答案:

答案 0 :(得分:1)

密切关注getLevel条件的顺序。

if (player->getLevel() >= 2)
{
}
// Here, it's certain that the level is < 2, since it wasn't >= 2.
// Thus, none of these following tests will be true.
else if (player->getLevel() >= 10)
{
}
else if (player->getLevel() >= 20)
{
}
else if (player->getLevel() >= 30)
{
}
else
    return;

你应该从最大的水平开始测试水平,然后按照自己的方式进行测量。

if (player->getLevel() >= 30)
{
}
// The level is less than 30. Is it greater than 20?
else if (player->getLevel() >= 20)
{
}
// The level is less than 20. Is it greater than 10?
else if (player->getLevel() >= 10)
{
}
// The level is less than 10. Is it greater than 2?
else if (player->getLevel() >= 2)
{
}
else
    return;

答案 1 :(得分:0)

看起来只是一个逻辑错误。让我们来看看这个陈述:

if (player->getLevel() >= 2) // ran - 目前为止确定

现在很多这个:

else if (player->getLevel() >= 10) - 和其他人(与20,30比较)

现在假设你的玩家&#39;有15级 - 看起来意图是运行第二个if区块(例如,与>= 10进行比较的位置)

但是有一个问题:如果级别是15,那么每次只执行第一个 if - 块(因为显然15&gt; = 2 )。

解决方案:与范围进行比较,如下所示:

if ((player->getLevel() >= 2) && (player->getLevel() < 10))
...
else if ((player->getLevel() >= 10) && (player->getLevel() < 20))
...

等等。