简体   繁体   English

C ++做while循环

[英]C++ do while loop

I have a vector holding 10 items (all of the same class for simplicity call it 'a'). 我有一个包含10个项目的向量(为简单起见所有相同的类称为'a')。 What I want to do is to check that 'A' isn't either a) hiding the walls or b) hiding another 'A'. 我想要做的是检查'A'不是a)隐藏墙壁或b)隐藏另一个'A'。 I have a collisions function that does this. 我有一个碰撞功能,可以做到这一点。

The idea is simply to have this looping class go though and move 'A' to the next position, if that potion is causing a collision then it needs to give itself a new random position on the screen. 这个想法只是让这个循环类通过并将'A'移动到下一个位置,如果该药水引起碰撞,那么它需要在屏幕上给自己一个新的随机位置。 Because the screen is small, there is a good chance that the element will be put onto of another one (or on top of the wall etc). 由于屏幕较小,因此很有可能将元素放在另一个(或墙顶等)上。 The logic of the code works well in my head - but debugging the code the object just gets stuck in the loop, and stay in the same position. 代码的逻辑在我的脑海中运行良好 - 但调试代码时对象只是卡在循环中,并保持在相同的位置。 'A' is supposed to move about the screen, but it stays still! 'A'应该在屏幕上移动,但它保持静止!

When I comment out the Do while loop, and move the 'MoveObject()' Function up the code works perfectly the 'A's are moving about the screen. 当我注释掉Do while循环,并移动'MoveObject()'函数时,代码完美地运行'A'正在屏幕上移动。 It is just when I try and add the extra functionality to it is when it doesn't work. 只是当我尝试添加额外的功能时它就不起作用了。

    void Board::Loop(void){


        //Display the postion of that Element. 
        for (unsigned int i = 0; i <= 10; ++i){


            do {

                if (checkCollisions(i)==true){
                moveObject(i); 
                }
                else{
                    objects[i]->ResetPostion();

                }

            }
            while (checkCollisions(i) == false);
            objects[i]->SetPosition(objects[i]->getXDir(),objects[i]->getYDir());
        }

}

The class below is the collision detection. 下面的类是碰撞检测。 This I will expand later. 我稍后会扩展。

    bool Board::checkCollisions(int index){

    char boundry = map[objects[index]->getXDir()][objects[index]->getYDir()];

    //There has been no collisions - therefore don't change anything 
    if(boundry == SYMBOL_EMPTY){
        return false;
    }
    else{
        return true;

    }

}

Any help would be much appreciated. 任何帮助将非常感激。 I will buy you a virtual beer :-) 我会给你买一个虚拟啤酒:-)

Thanks 谢谢

Edit: 编辑:

ResetPostion -> this will give the element A a random position on the screen moveObject -> this will look at the direction of the object and adjust the x and Y cord's appropriately. ResetPostion - >这将使元素A在屏幕上的随机位置moveObject - >这将查看对象的方向并适当调整x和Y线。

I guess you need: 我想你需要:

do { ...
... } while (checkCollisions(i)); 

Also, if you have 10 elements, then i = 0; i < 10; i++ 另外,如果你有10个元素,那么i = 0; i < 10; i++ i = 0; i < 10; i++

And btw. 顺便说一下。 don't write if (something == true) , simply if (something) or if (!something) 不要写if (something == true) ,只是if (something)if (!something)

for (unsigned int i = 0; i <= 10; ++i){

is wrong because that's a loop for eleven items, use 这是错误的,因为这是11个项目的循环,使用

for (unsigned int i = 0; i < 10; ++i){

instead. 代替。

You don't define what 'doesn't work' means, so that's all the help I can give for now. 你没有定义“不起作用”的意思,所以这就是我现在可以提供的所有帮助。

There seems to be a lot of confusion here over basic language structure and logic flow. 这里似乎有很多关于基本语言结构和逻辑流程的混淆。 Writing a few very simple test apps that exercise different language features will probably help you a lot. 编写一些运行不同语言功能的非常简单的测试应用程序可能会对您有所帮助。 (So will a step-thru debugger, if you have one) (如果你有一个step-thru调试器,那么也是如此)

do/while() is a fairly advanced feature that some people spend whole careers never using, see: do...while vs while do/while()是一个相当高级的功能,有些人花费了整个职业从未使用过,请参阅: do ... while vs while

I recommend getting a solid foundation with while and if/else before even using for . 我建议有越来越坚实的基础whileif/else之前,即使使用for Your first look at do should be when you've just finished a while or for loop and realize you could save a mountain of duplicate initialization code if you just changed the order of execution a bit. 你先看看do应该是当你刚刚完成了whilefor循环,实现你可以节省重复的初始化代码一座山,如果你只是改变了执行的顺序位。 (Personally I don't even use do for that any more, I just use an iterator with while(true)/break since it lets me pre and post code all within a single loop) (我个人甚至不使用do了任何更多的,我只是用同一个迭代器while(true)/break ,因为它让我前置后置码内的所有单回路)

I think this simplifies what you're trying to accomplish: 我认为这简化了您要完成的任务:

void Board::Loop(void) {
    //Display the postion of that Element. 
    for (unsigned int i = 0; i < 10; ++i) {
        while(IsGoingToCollide(i))  //check is first, do while doesn't make sense
            objects[i]->ResetPosition();
        moveObject(i);   //same as ->SetPosition(XDir, YDir)?
                         //either explain difference or remove one or the other
    }
}

This function name seems ambiguous to me: 这个函数名对我来说似乎不明确:

bool Board::checkCollisions(int index) {

I'd recommend changing it to: 我建议将其更改为:

// returns true if moving to next position (based on inertia) will 
// cause overlap with any other object's or structure's current location
bool Board::IsGoingToCollide(int index) {

In contrast checkCollisions() could also mean: 相比之下, checkCollisions()也可能意味着:

// returns true if there is no overlap between this object's
// current location and any other object's or structure's current location
bool Board::DidntCollide(int index) {

Final note: Double check that ->ResetPosition() puts things inside the boundaries. 最后注意事项:仔细检查->ResetPosition()将事物置于边界内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM