Skip to main content
Participant
October 22, 2009
Answered

Repeating Functions, variable won't change. Reeeallly confusing

  • October 22, 2009
  • 1 reply
  • 601 views

Okay.. so yea, I'm trying to repeat a function that basically will attachmovie on a "tileset", an image depending on the parameter.

It's called AddID(i), and currently there's only 2 valid options, 0 and 1, images showing, X and O.

There's a Button, and theres an array and a function that shows an image of the tile depending on its ID value loaded from the array.

My problem is:

Everything works just great as if you repeat the function by clicking again, but...

When repeating the function by code, all the tiles will have the Image of the last tile added.

This is my timeline actionscript code.

IDonTile = new Array();

IDonTile = [0,0,1,0,0,1];

_global.Tile=0;         

And this is my buttoncode.

on(release){
            AddID(IDonTile[_global.Tile]);                 // This function Adds the Tile to my screen
            Tilelist.text+=TileSlot[_global.Tile];         // This is actually a debug textfield to see what value is supposed to be loaded

            _global.Tile+=1;                                    // This is so the next time a tile is added, next array value will be used as ID.

}

This, works just fine!!

If i click this button 6 times, i will have in the following order : X,X,O,X,X,O

The debug text field will show : 0,0,1,0,0,1

BUT

I want to add all 6 tiles instantly, I would just use the function in a for loop, or repeat the code 6 times, Like this:

on(release){

     for(i=0;i<5;i++){
             AddID(IDonTile[_global.Tile]);                 // This function Adds the Tile to my screen
             Tilelist.text+=TileSlot[_global.Tile];         // This is actually a debug textfield to see what value is supposed to be loaded

            _global.Tile+=1;                                    // This is so the next time a tile is added, next array value will be used as ID.

       }

}

The Debug Text field will Show : 0,0,1,0,0,1

Which is actually correct

BUT ALL 6 TILES WILL BE : O,O,O,O,O,O

That's 6 O's.

The AddID(i) Function is only 10 lines long.

I don't want to manually Add my 6 tiles, what if i had 100 tiles

I was wondering, is there something wrong on Actionscript 2?

Should I move on to Actionscript 3 maybe it will work??

Because, if I can manually click the button, and it works, it should be the exact same thing if i code it by a foor loop, Correct???

I am reaaaallly confuseddd T,T it shouldn't beee like thisss

Please help !!

I thanks in advance for any replies..

Cause last time I tried asking for help, I got none...

This topic has been closed for replies.
Correct answer Ned Murphy

Here's one solution... If I rewrite the function and eliminate the use of the Tile linkage it works.

function AddID(iID){
if(tiles<tilesets){
  switch(iID){
   case 0 :
   attachMovie("X","Tile", tilelayer, {_x:kx+(iconwidth*tX), _y:ky+(iconheight*tY)});;
   break;
   case 1 :   
   attachMovie("O","Tile", tilelayer, {_x:kx+(iconwidth*tX), _y:ky+(iconheight*tY)});
   break;
  }
  addTile();
}
if(tiles==tilesets){
  MsgBox.text="You have nowhere to store."
}
}

If for some reason that isn't desirable, then here is an alternative.  I think I have narrowed down the problem to be one of timing... something in the loop cannot react fast enough to the changing _global.Tile value, and I suspect it is the attachMovie activity.  This solution involves slowing down the rate at which the file tries to attach the movies...  On the button that initiates adding the tiles place the following...

on(release){
myInterval = setInterval(fillTiles, 10);
}

And in the timeline code add the following...

var myInterval;

function fillTiles(){
if(_global.Tile < 6){
  AddID(IDonTile[_global.Tile]);
  Tilelist.text+=IDonTile[_global.Tile]
  _global.Tile+=1;
} else {
  clearInterval(myInterval);
}
}

1 reply

Ned Murphy
Legend
October 22, 2009

AS2 vs AS3 will not make any difference for what you appear to be trying to do.  You should show all the code you are trying to use.  You have another array "TileSlot" that isn't explained anywhere that I can find, and the one function that seems most important, AddID,  isn't shown at all.  Have you checked the symbols in the library to confirm that they are prepared properly with their linkages?

Participant
October 22, 2009

Oh, sorry, "TileSlot" was just an error from my part, it's actually just the IDonTile array.

Yes I have checked all the linkage, if there was something wrong with that, the manual button wouldnt have been working as it does.

What i don't get is that, the repeating of the function won't use the proper variables it is supposed to use.

It's as if the for loop will only read it one time.

I've uploaded the timelinecode in .txt file if you want to have a closer look.

I feel like this is a problem impossible to solve, and if it is, then it's all over for my little game

Maybe I should use other methods of looping the function, or even other methods of the function itself..

Any ideas, suggestion?

(EDIT: I Hope i can upload the .fla file here too?? *cough*, oh yea, by the way, i think it will be easier to understand it all if you can see all the code in the fla file)

I am really thankful for anyone that takes a look on it though..

Ned Murphy
Ned MurphyCorrect answer
Legend
October 22, 2009

Here's one solution... If I rewrite the function and eliminate the use of the Tile linkage it works.

function AddID(iID){
if(tiles<tilesets){
  switch(iID){
   case 0 :
   attachMovie("X","Tile", tilelayer, {_x:kx+(iconwidth*tX), _y:ky+(iconheight*tY)});;
   break;
   case 1 :   
   attachMovie("O","Tile", tilelayer, {_x:kx+(iconwidth*tX), _y:ky+(iconheight*tY)});
   break;
  }
  addTile();
}
if(tiles==tilesets){
  MsgBox.text="You have nowhere to store."
}
}

If for some reason that isn't desirable, then here is an alternative.  I think I have narrowed down the problem to be one of timing... something in the loop cannot react fast enough to the changing _global.Tile value, and I suspect it is the attachMovie activity.  This solution involves slowing down the rate at which the file tries to attach the movies...  On the button that initiates adding the tiles place the following...

on(release){
myInterval = setInterval(fillTiles, 10);
}

And in the timeline code add the following...

var myInterval;

function fillTiles(){
if(_global.Tile < 6){
  AddID(IDonTile[_global.Tile]);
  Tilelist.text+=IDonTile[_global.Tile]
  _global.Tile+=1;
} else {
  clearInterval(myInterval);
}
}