dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: nested if statement

  1. #1
    Junior Member
    Join Date
    Oct 2003
    Posts
    11

    nested if statement

    Ok guys, I've spend hours on this and I cant figure it out ... it's driving me insane .... I would really appreciate some help

    Basically I have 9 boxes
    when you click on a box it gets alpha 50
    then if you click again the tile[i + 1] goes to frame 2
    ONLY I can't get the [ i + 1 ] to work ! .... I tried putting in a [4] etc and it works fine ... am I writing the expression wrong ?

    thanks for any help

    function game() {

    mx = _root._xmouse;
    my = _root._ymouse;

    for (i=0; i<tiles.length; i++) {
    if (tiles[i].hittest(mx,my)) {

    tiles[i]._alpha = 50;

    if ((tiles[i+1].hittest(mx,my)) and (tiles[i+1]._currentframe == 3)) {
    tiles[i+1].gotoAndStop(2);
    }

    for (j=0; j<i; j++) {
    tiles[j]._alpha = 100;
    }
    for (j=8; j>i; j--) {
    tiles[j]._alpha = 100;
    }

    } else {
    tiles[i]._alpha = 100;
    }
    }
    }

  2. #2
    Junior Member
    Join Date
    May 2004
    Location
    Milton Keynes
    Posts
    18
    Have you tried putting i+1 into a variable first?

    also I would do some kind of check to ensure that i+1 is less than the amount of tiles...IE your for/next loop checks all tiles so what happens if it occours on the last tile, where is the next one?

    IE if you have 10 tiles tile[0]-tile[9];
    and your fornext loop goes 0-9;
    your hittest would be trying to test tile[10], which doesnt exist, probably causing the problem.


    However, I'm soooo new at this I could be talking crap though
    Last edited by jamiefuller; 05-20-2004 at 08:48 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2003
    Posts
    11

    variable

    ok , I just made a variable next

    next = i + 1;
    trace(next);

    next shows up as 4 (which is correct)

    when I put next in here

    if ((tiles[next].hittest(mx,my)) && (tiles[next]._currentframe == 3)) {
    tiles[next].gotoAndStop(2);
    }

    it doesnt work !? ... but when I put the number 4 it does ?

    I am confused

  4. #4
    Junior Member
    Join Date
    May 2004
    Location
    Milton Keynes
    Posts
    18
    Is that code still contained within the for next loop of i though?
    if it is then it will be checking too many tiles and may cause the whole function to fail.

    how about this change?

    Code:
    for (i=0; i<(tiles.length-1); i++) {
    Once again I apologise if im talking rubbish.

  5. #5
    Junior Member
    Join Date
    Oct 2003
    Posts
    11

    i increment

    I think the problem is ...
    (the function is executed onClipEvent(mouseDown)

    When I click the mouse button on the box i gets incremented and so it is already i+1

    I need it to only change the fram of the next tile if you click number 3 1st ( if 3 is selected )

    then if you click on 4 it should change it to frame 2;

    I am lost

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    but it cant work like you have written:
    code:

    if (tiles[i].hittest(mx, my)) {
    ...
    if ((tiles[i+1].hittest(mx, my)) and (tiles[i+1]._currentframe == 3)) {
    ...
    }



    if the mouse is on tiles[i] meaning hittest with tiles[i] is true, then it cant be on tiles[i+1] same time so the hittest on tiles[i+1] is false.

  7. #7
    Senior Member
    Join Date
    Feb 2001
    Location
    England
    Posts
    114
    Hi, i'm a bit rusty on this, but how did you make this array of tiles?

    Did you do this:
    Code:
    //method 1
    
    tiles = new Array();
    
    tiles[0] = attachMovie("tile", "somename", 123);
    tiles[1] = attachMovie("tile", "somename", 124);
    tiles[2] = attachMovie("tile", "somename", 125);
    
    ....etc
    or did you do:

    Code:
    //method 2
    
    attachMovie("tile", "tiles["+0+"]", 123);
    attachMovie("tile", "tiles["+1+"]", 124);
    attachMovie("tile", "tiles["+2+"]", 125);
    
    ...etc
    The difference being the second method doesn't actually make an array, and would not work unless you addressed you movieclips like:
    Code:
    eval("tiles["+i+"]")._alpha = 50;
    
    //or like
    
    tiles[4]._alpha = 50;
    the first method should create an array and fill it with your attached movies, so you can call them like you are currently trying, and would probably be preferable to the second method. Sorry if this is a silly suggestion, but it is the only thing I can think of
    Tomorrow's today, and today is yesterday.

  8. #8
    Flash hates me. crashlanding's Avatar
    Join Date
    Nov 2003
    Location
    UK
    Posts
    439
    Don't you need to put ///
    code:
    hitTest

    instead of
    code:
    hittest

    ???
    "wen i found my gerbil dead my other gerbil was eating it i just cried and screamed"
    http://www.livescripts.net

    --------------------------------------------------------------------------------
    Last edited by some moderator : Today at 9:01 PM.

  9. #9
    Junior Member
    Join Date
    Oct 2003
    Posts
    11

    array

    This is how I made my array

    I also tried the hitTest but it still doesnt work\

    ... I think I might try the long roundabout way of doing a lot of if statements .. that should work ... I think hehheh

    function startup() {

    tiles = new Array();
    for(i=0;i<9;i++) {
    tiles.push(_root["tile"+i]);
    }

    selected = new Array();
    for(i=0;i<9;i++) {
    selected.push("selected"+i);
    }

    for(i=0;i<tiles.length;i++) {
    if (i < 4) {
    tiles[i].gotoAndStop(1);
    } else if (i > 4) {
    tiles[i].gotoAndStop(2);
    } else tiles[i].gotoAndStop(3);
    }
    }

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