A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: to mutch code?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3

    to mutch code?

    Can anyone help to get this code a little bit smaller?
    I have to put in this code 100 times and maybe it can be mutch more compact
    if (Number(_root.fillred) == 0)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(40);
    }
    else if (Number(_root.fillred) == 1)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(43);
    }
    else if (Number(_root.fillred) == 2)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(46);
    }
    else if (Number(_root.fillred) == 3)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(49);
    }
    else if (Number(_root.fillred) == 4)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(52);
    }
    else if (Number(_root.fillred) == 5)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(55);
    }
    else if (Number(_root.fillred) == 6)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(58);
    }
    else if (Number(_root.fillred) == 7)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(61);
    }
    else if (Number(_root.fillred) == 8)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(64);
    }
    else if (Number(_root.fillred) == 9)
    {
    _root.herhaalkans.gotoAndPlay(2);
    _root.tabel20.gotoAndPlay(67);
    }
    and this go on till _root.redfill=100

    Thanx

  2. #2
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    Try this:

    PHP Code:
    var i:Number = (fillred) * 40;

    if (
    Number(_root.fillred)==0) {
        
    _root.herhaalkans.gotoAndPlay(2);
        
    _root.tabel20.gotoAndPlay(i);

    EDIT - Soz just seen you using _root so I'm guessing this is for AS1 or 2. The above may only work in AS3. Maybe change first line to i=fillred * 3 + 40; ???
    Last edited by CVO Chris; 05-27-2010 at 03:54 PM.

  3. #3
    Uses MX 2004 Pro Quixx's Avatar
    Join Date
    Nov 2004
    Location
    U.S.
    Posts
    877
    You can use a for loop to go through the variables and then break away once a match has been made.


    Code:
    var startNum:Number = 40;
    var limit:Number = 100;
    var frameDiff:Number = 3;
    var checkFun:Function = function () {
    	for (i=0; i<=limit; i++) {
    		if (fillred == i) {
    			// trace("fillred= "+fillred+", gotoAndPlay= "+(startNum+(i*frameDiff)));
    			_root.herhaalkans.gotoAndPlay(2);
    			_root.tabel20.gotoAndPlay(startNum+(i*frameDiff));
    			break;
    		}
    	}
    };
    So to check fillred against all the possible variables (0 - 100), you just set up a way to call the checkFun function. For example, if you create a button on the stage called, "my_btn", you could use the following code to check the fillred variable when the button is clicked:

    Code:
    my_btn.onRelease = function() {
    	checkFun();
    };
    Last edited by Quixx; 05-28-2010 at 01:11 AM. Reason: typo fix

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanx both of you, it works great and mutch less code

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