A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: How can I use a variable to change a movieclips frame number?

  1. #1

    How can I use a variable to change a movieclips frame number?

    I have a library of sorts with 6 pictures (called select 1-6) per page contained in its own movieclip.

    For the first page I have

    Code:
    select1.gotoAndStop(1);
    select2.gotoAndStop(2);
    select3.gotoAndStop(3);
    select4.gotoAndStop(4);
    select5.gotoAndStop(5);
    select6.gotoAndStop(6);
    button1.onPress = function () {
    	_root.head.eyes.gotoAndStop(1)
    };
    button2.onPress = function () {
    	_root.head.eyes.gotoAndStop(2)
    };
    button3.onPress = function () {
    	_root.head.eyes.gotoAndStop(3)
    };
    button4.onPress = function () {
    	_root.head.eyes.gotoAndStop(4)
    };
    button5.onPress = function () {
    	_root.head.eyes.gotoAndStop(5)
    };
    button6.onPress = function () {
    	_root.head.eyes.gotoAndStop(6)
    };
    For the 2nd page I have:

    Code:
    select1.gotoAndStop(7);
    select2.gotoAndStop(8);
    select3.gotoAndStop(9);
    select4.gotoAndStop(10);
    select5.gotoAndStop(11);
    select6.gotoAndStop(12);
    button1.onPress = function () {
    	_root.head.eyes.gotoAndStop(7)
    };
    button2.onPress = function () {
    	_root.head.eyes.gotoAndStop(8)
    };
    button3.onPress = function () {
    	_root.head.eyes.gotoAndStop(9)
    };
    button4.onPress = function () {
    	_root.head.eyes.gotoAndStop(10)
    };
    button5.onPress = function () {
    	_root.head.eyes.gotoAndStop(11)
    };
    button6.onPress = function () {
    	_root.head.eyes.gotoAndStop(12)
    };
    ...which works, but it seems rather long winded to have to change the numbers each time when all they'll be doing is going up by 6 on each page.
    I was thinking something along the lines of this would be better (don't laugh...)

    Code:
    select1.gotoAndStop(i);
    select2.gotoAndStop(i+);
    select3.gotoAndStop(i+2);
    select4.gotoAndStop(i+3);
    select5.gotoAndStop(i+4);
    select6.gotoAndStop(i+5);
    button1.onPress = function () {
    	_root.head.eyes.gotoAndStop(i)
    };
    button2.onPress = function () {
    	_root.head.eyes.gotoAndStop(i+)
    };
    button3.onPress = function () {
    	_root.head.eyes.gotoAndStop(i+2)
    };
    button4.onPress = function () {
    	_root.head.eyes.gotoAndStop(i+3)
    };
    button5.onPress = function () {
    	_root.head.eyes.gotoAndStop(i+4)
    };
    button6.onPress = function () {
    	_root.head.eyes.gotoAndStop(i+5)
    };
    var i:Number = 1;
    Then for each page I could just change 'i' to 6, 12, 18, 24 and so on.
    As I have a lot of pages to do this could really be a time saver. Does anyone know how to get this code working? I'm hoping I'm not too far off, just missing some inverted commas here and a bracket there.

    Any help would be much appreciated. I've been scouring the net but its quite a specific problem and I must not be using the right key words in my searches.

    Thanks!
    Fran

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    hmm not sure the whole way your going about your application is the right way, but to shorten your code:

    in your first frame put:


    PHP Code:
    for(var i:Number=1i<=6i++){
        
        
    this["select"+i].gotoAndStop(i);    
        
        
    this["button"+i].id i;
        
        
    this["button"+i].onRelease = function():Void{
            
    _root.head.eyes.gotoAndStop(this.id);
        }

    in your second frame put:


    PHP Code:
    for(var i:Number=1i<=6i++){
        
        
    this["select"+i].gotoAndStop(i+6);    
        
        
    this["button"+i].id i+6;
        
        
    this["button"+i].onRelease = function():Void{
            
    _root.head.eyes.gotoAndStop(this.id+6);
        }

    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Wowy that's the stuff! Thankyou so much! That will save me so much time!

    Hmmm is there a way to be lazier still and get flash to automatically add the 6's each frame? (I have a taste for shortcuts now hehe)

  4. #4
    Crikey I just looked at the projects in your sig...wow do I feel small bothering you with my terrible coding!

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