Level select

Subscribe to Level select 4 posts, 3 voices

 
avatar for Baturinsky Baturinsky 7 posts

I got a problem with AS 2.0 I want to have a page with links to levels. I place buttons on screen in a cycle, but can’t make each button to link to different level.

I do it that way.

var number = number of levels;

for(var i=1;i<=number;i++){ var b = button creation code; bb.onRelease = function(){ gotoLevel(i); } }

but that’s not working. Dam Flash uses last value of i, instead of value iat the moment of function creation.

 
avatar for BigCheese BigCheese 363 posts

Are you trying to loop through buttons on the stage?

P.S. You can also use the <...pre><.../pre> tags (Without the dots) to show code in the forums.

 
avatar for Baturinsky Baturinsky 7 posts

Oh, thanks.

I want to generate buttons with similar functionality (launch level with specific number) in a cycle.

 
avatar for Phantasmagoria Phantasmagoria 236 posts

Dam Flash uses last value of i, instead of value iat the moment of function creation.

This is correct behavior on Flash’s part. Research closures if you want to understand what’s going on.

There are various ways to get the behavior you want. I’m not sure what the easiest way to do it in AS2 is, given the lack of block level scope, but here’s one that should work:

function makeButtonFunction (i) {
    return function(){ gotoLevel(i); }
}

for(var i=1;i<=number;i++){
    var b = button creation code;
    bb.onRelease = makeButtonFunction(i);
}