views:

1176

answers:

11

I have the following code:

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;
}

And I get this output around 60:

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

How is this possible? I must be missing something simple.

The project I am working on is actually trying to simulate picking up a piece of paper and moving it around, then setting it back down. I am trying to modify the flash page flip code to create this effect.

+3  A: 

I'm not sure I fully understand what you're trying to accomplish because you're not explaining what you expect to happen. But don't you mean to do this?

if(pickedUp == true){

}
else
if(pickedUp == false){

}
Luke
sorry -1 because you don't need the "== true" or the " == false" or the "else if".
Iain
The OP has nothing to do with recommendations on coding style, please don't mark down people for having a different style than you.
Richard Szalay
+1 this is exactly correct, the problem in the original is the lack of the else. the == are from the original. This was the most direct and concise way to state the answer. kyle's answer is more complete, but still both are correct.
Adam Bellaire
+4  A: 

Hi Bryan,

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

kyle
There's a typo: in the first paragraph, you probably mean "pickedUp remains set to TRUE".
David Hanak
A: 

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)
    ....
}
Chris Nava
Generally best to avoid "while" in all ECMAScript languages, it can be a serious performance hit.
Christopher W. Allen-Poole
The while loop has no noticeable performance difference than any other type of loop in an ECMAScript-based language. A while loop is basically a for loop without the initializer or counting expression. It only has the conditional loop test.
joshtynjala
A: 

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.

Mauricio
+2  A: 

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;
    }
Iain
I could of course down vote you for rehashing what's already explained, but that of course would be childish...
Luke
A: 

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..

Bryan
I think the problem is similar to what Michael is pointing out; if the bool is true and the int is > 60, you'll have this problem every time. I'd agree with the diagnosis that you're setting the bool at the head of the loop.
kyle
+2  A: 

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.

Michael Todd
A: 

Thanks Micheal. That has to be it.

Bryan
A: 

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.

Andrew
A: 

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

Shvilam
A: 

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.

Richard Szalay