A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: 1 line of code - which loop to use?

  1. #1
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    1 line of code - which loop to use?

    Hi Guys,

    Anyone know how say this in actionscript? -

    //determine if any 'food' is left in the game. If not display flag to next level.
    When randomNumberVariable % (modulus) timesTableVariable stops equaling 0 for any of the 20 spiders in the game set flagNextLevel._visible = true

    I can't figure out which loop to use as a for loop hasn't worked so far... Maybe a do while ?

    Any help greatly appreciated...

    Mark

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Not a lot of info.. anyway, i can always suggest a for loop.

    a for loop should work if they're sequential (spiders0 - spiders1, etc.), a do while would crash flash. Each spider has a randomNumberVariable, named the same? Then use something in these lines:

    Code:
    for (i=0; i<spidersNumber; i++) {
    this["spiders"+i].randomNumberVariable%path.timesTableVariable!=0?flagNextLevel._visible=true:flagNextLevel._visible=false;
    }

  3. #3
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    Cheers for the prompt reply...

    Right then a little more detail first. This is what I have already that branches depending on whether the number is a multiple of ran or not.

    //still within Ladybird_mc.onEnterFrame determine if any 'food' is left in the game. If not display flag to go to next game level.
    for (i=1; i<21; i++) {

    if (_root['spider'+i].ran%tT==0){

    trace('multiple =' + _root['spider'+i].ran)

    }else{
    trace ('not multiple');
    }

    What you've just posted looks like it might work but I don't quite understand the following syntax:

    this["spiders"+i].randomNumberVariable%path.timesTableVariable!=0?f lagNextLevel._visible=true:flagNextLevel._visible= false;

    Can you please clarify this for me as I've been looking in books all day

    Thanks mate - brilliant for help so far...

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    ternary code is shorthand for an if/else:

    statement1?statement2:statement3;
    is like saying :
    if (statement1) {
    statement2;
    } else {
    statement3;
    }

    gparis

  5. #5
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    WOW thanks...

    Cheers for this... actionscript has moved on a little since I first started using flash in version 4 but never had time to get proficient...

    BTW - I like your tree menu... gonna have fun pulling that fla apart...

    All the best...

  6. #6
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    flag displays when eat first spider

    Hi again,

    What's happening now is that the flag is displaying when you eat the first spider containing a random number that is a multiple of the times table selected. It needs to display when you eat the last one in the game - not the first. The problem being that I don't know how many there are.

    Here is the code:

    for (i=1; i<21; i++) {
    _root['spider'+i].ran%tT!=0?flag._visible = true:flag._visible = false;
    }

    Thanks for your explanation of ternary code tho - will use this in future...

  7. #7
    Senior Member
    Join Date
    Oct 2000
    Posts
    184

    My Mistake - it kind of works...

    My mistake... it sort of works although the flag still displays when there are multiples left to be eaten...

    It is the closest code I've had so far so cheers.

    Needs more testing but cheers...

    Thanks

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