dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Expert - Changing movie frame (gotoAndPlay) in a for loop using a counter

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6

    Expert - Changing movie frame (gotoAndPlay) in a for loop using a counter

    I'm having some difficulties using counters. Specifically related to using the gotoAndPlay (or Stop) command within a 'for' loop. This does not relate to the layers of attached movie clips but the counter of the for loop 'i'.

    Below is an example of code that works. Imagine a movie clip called 'Card' that is added to the main Movie using a for loop with instance names Card0, Card1, Card2, ... etc. Within the movie clip 'Card' there are variables which can be set using a pre-defined array. The variable is called Var1 and is accessed using the code "Card0.Var1" in the case that i=0.


    for(i=0;i<NCards;i++){

    this.attachMovie( "Card", "Card"+i, Layer );
    Layer++;
    set ("Card"+i+".Var1",Value[i]);

    }

    Now imaging there's a movie clip within 'Card' with the instance name "Image". To change the frame of that movie clip you would simply use the following code:

    Card0.Image.gotoAndStop(3);

    The code that I cannot make work looks a little like either of the following 2 codes below:

    for(i=0;i<NCards;i++){
    "Card"+i+".Image".gotoAndStop(3);
    }

    or

    setProperty("Card"+i+".Image",_currentframe,3);

    Basically it seems that the gotoAndStop function doesnt get on with concatenated text (added together text). Any help would be much appreciated!

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    try it like this
    PHP Code:
    var NCards:Number 12;

    for (var 
    i:Number 0NCardsi++)
    {
        var 
    Card:MovieClip this.attachMovie("Card""Card" ii);
        
    Card._x 40;
        
    Card._y 25;
        
    Card.Var = Value[i];
        
    Card.Image.gotoAndStop(3);


  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Hi fruitbeard,

    Thanks for your reply. I like the idea of using a variable to carry the information about the value of i within the for loop. Sadly I cant get the code working. I've tried the simplest position of _x or _y and cant get the value passed. I've notice that when I define a variable type it usually turns blue. This is the case with Number, String, Object, etc. But this does not happen with MovieClip or any variation with capitals (movieclip, movieClip, Movieclip). Perhaps my version of ActionScipt or Flash is not the same as yours?

    Just to check that we're both on the same page. This code is going on the main timeline. Not sure if that would change anything.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,784
    Hi,

    yes it would help if we knew what version we are using,

    Here is a file CS5, AS2, see if it helps at all,

    here is the code if you are unable to open it,

    with a MovieClip with AS linkage of "Card" and a MovieClip inside called "Image", with three stop frames.
    Like you already have I imagine.
    PHP Code:
    var nCards:Number 12;
    var 
    cardArray:Array = new Array();

    for (var 
    i:Number 0nCardsi++)
    {
        var 
    card:Object this.attachMovie("Card""card" ii);
        
    card._x 40 + (* (card._width 2));
        
    card._y 10;
        
    card.vars i;
        
    card.Image.gotoAndStop(3);
        
    card.onPress turnCard;
        
    card.onRelease reTurnCard;
        
    cardArray.push(card);
    }

    function 
    turnCard()
    {
        
    trace(this._name " \t| _x = " this._x " \t| _y = " this._y " \t| var = " this.vars);
        
    this.Image.gotoAndStop(2);
    }

    function 
    reTurnCard()
    {
        
    this.Image.gotoAndStop(3);
    }

    // *** Not used here, but can be used to turn all, etc etc.....
    function allCards(arg:Number)
    {
        for (var 
    j:Number 0cardArray.lengthj++)
        {
            
    cardArray[j].Image.gotoAndStop(arg);
        }

    obviously I am guessing at how you get your nCards number and things as we can not see your *.fla, which makes things a lot easier.

    i notice the title now, Flash5!!
    Last edited by fruitbeard; 10-03-2014 at 08:24 AM.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    So I've actually found a bit of a work around. Without over complicating my explanation with the specific details of my Movie, it goes along the following lines:

    The code for changing the 'Image' movie clip was placed in the Card movie timeline rather than on the Main timeline. This means I could use Temp3 = targetPath (this).charAt(13) to identify the local value of the card

    if (_root.CardA[Temp3][5]=="E"){
    this.Gems.gotoAndStop(5);

    It basically means that rather than the Main timeline trying to change the frame of the 'Card' movie clip, I get the 'Card' movie clip itself to change its own frame. I still ran into some difficulties that I couldn't fully solve but its good enough for what I need. If I come up with anything extra then I'll let you know.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    I've just had a revelation! Ever heard of the 'eval' statement?

    Layer=0;
    Card = new Array();

    for(i=0;i<4;i++){

    Card[i] = new Array();

    for(j=0;j<13;j++){

    //Create an array of unique card names. Note: This only works when i is a single digit! Or you would have to think more carefully about creating the array.
    Card[i][j] = "Card"+i+j;

    this.attachMovie( "Card", Card[i][j], Layer );
    Layer++;

    //SetProperty method is fine evaluating the value of Card[i][j], but .gotoAndStop is not!
    setProperty(Card[i][j],_x,60*i+25);
    setProperty(Card[i][j],_y,80*j+37.5);

    //Here we use the eval method to obtain the value held in the array. It now works!
    eval(Card[i][j]).CardM.gotoAndStop(2);

    //Passing the values of i & j to the card itself can be very useful!
    Card[i][j].CardM.Var1=i;
    Card[i][j].CardM.Var2=j;
    }
    }

Tags for this Thread

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