A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem with boolean var

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    Problem with boolean var

    The code below allows me to place the object at a certain position when clicked, then click it at its new position to send it back. Then that's it, the boolean value doesn't change after that allowing me to keep clicking it back and forth. With a numerical value I could ++ for a loop, but with a boolean, I'm stumped.

    var taken:Boolean = false;

    function move(event:MouseEvent):void
    {
    if(taken == true)
    {movie_mc.x = original_mc.x;
    movie_mc.y = original_mc.y;}
    else
    {movie_mc.x = hold_mc.x;
    movie_mc.y = hold_mc.y;
    taken = true;}
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    var taken:Boolean = false;
    
    function move(event:MouseEvent):void{
      if(taken){
        movie_mc.x = original_mc.x;
        movie_mc.y = original_mc.y;
      }else{
        movie_mc.x = hold_mc.x;
        movie_mc.y = hold_mc.y;
      }
      taken = !taken;
    }
    }

  3. #3
    Born Again Flasher
    Join Date
    Sep 2000
    Location
    Oahu, Hawaii
    Posts
    58
    Code:
    /** I'm assuming this is in here somewhere: */
    this.movie_mc.EventListener(MouseEvent.CLICK, move);
    
    var taken:Boolean = false;
    
    function move(event:MouseEvent):void{
      if(taken){
        movie_mc.x = original_mc.x;
        movie_mc.y = original_mc.y;
        taken = false;
      }
      else{
        movie_mc.x = hold_mc.x;
        movie_mc.y = hold_mc.y;
        taken = true;
      }
    }
    You need something to tell "taken" to go back to false at the right time. Assuming I understand what you are trying to do correctly, then you'd want the movie_mc to not be taken if you click it while it is taken, and vice versa.

    In your code, you had "taken = true;" when they click a non-taken movie_mc. So you can just do the opposite if they click a taken movie_mc by adding "taken = false;".

    edit: Actually, re-reading your post I'm not sure I know what you wanted it to do. Do you want something to keep track of how many times something is moved? Is the taken field used for something else, and that's why you didn't set it to false? You could add another boolean to track the position separately (var originalPosition:Boolean = true;) or an int (var timesMoved:int = 0;) to track those things if you wanted.

    Had to also edit to remove smilies, haha.
    Last edited by emorphous; 08-19-2009 at 07:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center