0

我有以下代码:

if(pickedUp == true){
    trace("released and picked up" + pickedUpNum);
    this.storedClick = false;
    this.onMouseMove = null;
    this.onMouseDown = null;
    this.onMouseUp = null;
    this.onEnterFrame = this.refresh;
    pickedUpNum++;
    if( pickedUpNum > 60) pickedUp = false;
}

if(pickedUp == false){
    trace("released and not picked up");
    this.storedClick = false;
    this.onEnterFrame = this.refresh;
    this.onMouseDown = this.onStoreDrag;
    this.onMouseUp = this.onClearStoreDrag;
    this.onMouseMove = null;
}

我得到这个输出大约 60:

released and picked up59
released and picked up60
released and not picked up
released and picked up61
released and not picked up  

这怎么可能?我一定错过了一些简单的东西。

我正在进行的项目实际上是在尝试模拟拿起一张纸并移动它,然后将其放回原处。我正在尝试修改 Flash 页面翻转代码以创建此效果。

4

10 回答 10

4

Take a look at a trace for the initial conditions (pickedUpNum, pickedUp) = (59, true). We'll execute the first conditional, and add 1 to pickedUpNum. After adding 1, pickedUpNum = 60, so we don't execute the internal conditional and pickedUp remains set to true. With this value, we skip the second conditional.

So we're at (pickedUpNum,pickedUp) = (60,true). We again execute the first conditional, and add 1 to pickedUpNum. Now, pickedUpNum = 61, so we execute the internal conditional and set pickedUp = false. With this new value, the second conditional will execute.

(sorry if I'm being pedantic, sometimes it's best to be explicit :) )

edit: thanks, David, for catching my logical typo

于 2009-01-22T02:23:08.877 回答
3

我不确定我是否完全理解您要完成的工作,因为您没有解释您期望发生的事情。但你不是打算这样做吗?

if(pickedUp == true){

}
else
if(pickedUp == false){

}
于 2009-01-22T02:19:58.523 回答
2

Ok, couple of things. Firstly, you don't need to say "== true" for booleans. Secondly, in general don't use "==false" use "!" (the NOT symbol), but in your case just use an "else"

    if(pickedUp){
                trace("released and picked up" + pickedUpNum);
                this.storedClick = false;
                this.onMouseMove = null;
                this.onMouseDown = null;
                this.onMouseUp = null;
                this.onEnterFrame = this.refresh;
                pickedUpNum++;
                if( pickedUpNum > 60) pickedUp = false;
        }  else {
            trace("released and not picked up");
            this.storedClick = false;
            this.onEnterFrame = this.refresh;
            this.onMouseDown = this.onStoreDrag;
            this.onMouseUp = this.onClearStoreDrag;
            this.onMouseMove = null;
    }
于 2009-01-22T09:47:38.013 回答
2

If the code you wrote is in a loop, somehow pickedUp is being set to true AFTER the check for (pickedUp == false). So, what would happen is the next time through the loop (say when pickedUpNum == 61), pickedUp would be true, the true statements would run, and then pickedUp would be set to false again (at the end of the "true" check) allowing the false check statements to run.

Make sure you're not resetting pickedUp to true after the false check (and when pickedUpNum > 60, per your code) and it should work as you intend.

于 2009-01-22T18:16:44.240 回答
0

I suspect your code is resetting the flag each time through the loop.

wile (true)
{
    boolean pickedUp = true // Move this outside the loop ^^

    if (pickedUp == true)
    ....
}
于 2009-01-22T05:21:16.020 回答
0

What's it supposed to do when it reaches 61 and executes the second conditional?

If your code is not in a loop, check if pickedUp and pickedUpNum are being changed somewhere else than in the first conditional.

于 2009-01-22T08:37:36.563 回答
0

To answer all of your questions

  • Yes, the code is in a loop. (onEnterFrame)

  • I know I can use "else if", but if I did use else the second conditional would never execute.

  • I know I don't need to use == and can use not operator (!), but since I was having problems I decided to be explicit

  • I don't understand how Kyle explained the solution in his code and why other people believe it is a solution (+2).

The question remains how are both conditionals being satisfied ( pickedUp is true and false) once pickedUpNum reaches 61 and pickedUp is set to false..

于 2009-01-22T18:09:34.660 回答
0

Bryan - I stared at this for some time wondering why you couldn't accept Kyle's answer, then it hit me that you actually did want it to do this bit:

released and picked up60
released and not picked up

And the question was about what happened after. At first glance your question was being misinterpreted, as a fairly Textbook Example of a Mistake Someone Might Make With Conditionals. Micheal seems to have the right idea, as there is nothing in the code given that should explain it, but I also just wanted to add that the debugger in the flash IDE, while not perfect, is awesome for tracking down this kind of thing.

于 2009-01-27T07:25:21.173 回答
0

way not use debugger ? build your code with flex builder and use a friendly programing environment (eclipse) that have an easy way to debug the code

于 2009-01-27T09:12:33.173 回答
0

I agree with Shvilam. Learning how to use a step-through debugger is such a valuable skill and it makes bugs significantly easier to diagnose. I'd recommend Flex's debugger over Flash's, but either is better than none.

于 2009-01-27T10:27:00.853 回答