Skip to main content
Inspiring
May 27, 2013
Answered

create dynamic button with AS2

  • May 27, 2013
  • 10 replies
  • 2332 views

Hi, this is my code, my problem is how to turn this["mc"+i] to a clickable button, see the code near the end? it doesn't do it, in fact on the screen it's not even 'button-abled'. I did some research, couldn't find anything at all, please help.

what the script does to load a series of arrayFlag (ext jpeg files) into "mc"+i, and supposingly when you click these jpeg on screen would loadMovie to another mc, right now they re not behaving like buttons

for (var i=0; i<=flagArray.length; i++) {

    arrayFlag="country/thumb/"+flagArray;

    arrayBg="country/"+flagArray;

    //--------flags-------

    mybutton = this.createEmptyMovieClip("mc"+i, i);

    this["mc"+i].loadMovie(arrayFlag);

    this["mc"+i]._x=95+distance;

    this["mc"+i]._y=0;

    //---------Name----------

    var str:String = flagArray.split(".jpg").join("");

    trace (str);

    this.createTextField("my_txt"+i, 100+i, distance, 12, 100, 100);

    this["my_txt"+i].text = str;

    this["my_txt"+i].setTextFormat(format2);

    //--------Text-----------

    this.createTextField("my_title"+i,200+i, distance, 0, 150, 100);

    this["my_title"+i].text = "Country Name:";

    this["my_title"+i].setTextFormat(format1);

   

    //-----loadMovie---------

   

    this["mc"+i].onRelease = function () {    <<-- not working

        mc.loadMovie(arrayBg);

    }

    distance+=193;

trace(flagArray);

}

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer kglad

if you're trying to load a bitmap and create a button from the loaded bitmap, you are incorrect when you say there's no way to do that.

you just need to wait until loading is complete before assigning mouse handlers to the load-target movieclip or, even easier, create a child movieclip and use that as the load-target and apply the mouse handler to the parent:

for (var i = 0; i <= flagArray.length; i++) {

    arrayFlag = "country/thumb/" + flagArray;

    arrayBg = "country/" + flagArray;

    //--------flags-------

    mybutton = this.createEmptyMovieClip("mc" + i, i);  // your code would be easier for you to read if you used mybutton instead of this["mc" + i]

    var loadTarget:MovieClip = mybutton.createEmptyMovieClip("loadTarget",0);

    loadTarget.loadMovie(arrayFlag);

    this["mc" + i]._x = 95 + distance;

    this["mc" + i]._y = 0;

    //---------Name----------

    var str:String = flagArray.split(".jpg").join("");

    trace(str);

    this.createTextField("my_txt" + i,100 + i,distance,12,100,100);

    this["my_txt" + i].text = str;

    this["my_txt" + i].setTextFormat(format2);

    //--------Text-----------

    this.createTextField("my_title" + i,200 + i,distance,0,150,100);

    this["my_title" + i].text = "Country Name:";

    this["my_title" + i].setTextFormat(format1);

    //-----loadMovie---------

    this["mc" + i].onRelease = function() {

        mc.loadMovie(arrayBg);  // mc must already exist

    };

    distance += 193;

    trace(flagArray);

}

10 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 27, 2013

if you're trying to load a bitmap and create a button from the loaded bitmap, you are incorrect when you say there's no way to do that.

you just need to wait until loading is complete before assigning mouse handlers to the load-target movieclip or, even easier, create a child movieclip and use that as the load-target and apply the mouse handler to the parent:

for (var i = 0; i <= flagArray.length; i++) {

    arrayFlag = "country/thumb/" + flagArray;

    arrayBg = "country/" + flagArray;

    //--------flags-------

    mybutton = this.createEmptyMovieClip("mc" + i, i);  // your code would be easier for you to read if you used mybutton instead of this["mc" + i]

    var loadTarget:MovieClip = mybutton.createEmptyMovieClip("loadTarget",0);

    loadTarget.loadMovie(arrayFlag);

    this["mc" + i]._x = 95 + distance;

    this["mc" + i]._y = 0;

    //---------Name----------

    var str:String = flagArray.split(".jpg").join("");

    trace(str);

    this.createTextField("my_txt" + i,100 + i,distance,12,100,100);

    this["my_txt" + i].text = str;

    this["my_txt" + i].setTextFormat(format2);

    //--------Text-----------

    this.createTextField("my_title" + i,200 + i,distance,0,150,100);

    this["my_title" + i].text = "Country Name:";

    this["my_title" + i].setTextFormat(format1);

    //-----loadMovie---------

    this["mc" + i].onRelease = function() {

        mc.loadMovie(arrayBg);  // mc must already exist

    };

    distance += 193;

    trace(flagArray);

}

Inspiring
May 27, 2013

Hi, Kglad

thank you for reply.

I'll try your method, it seems more straight forward than mine, but I have some problem to have another movie to load this movie to the stage

If I run this movie by itself everything works fine. buttons are generated, and when they click, mc will be loaded with jpeg from 'country' folder.

But if I have another swf (also written in AS2), load this movie to stage. Then these buttons work  just once and then freeze itself.

the texts are not visible too, that might has to do with this movie is loaded to a masked layer. If I use the bitmapdata.draw method, its fine. It's the freeze thats killing me.

I'm now trying to see if your method could help.

Inspiring
May 27, 2013

Hi, kglad.

I just tried your code,

this["mc" + i].onRelease = function() {

     trace (arrayBg);    <<---------      arrayBg becomes undefined inside function

        mc.loadMovie(arrayBg);

    };

Inspiring
May 27, 2013

turns out there's no way to turn import jpeg become clickable buttons,

so the alternative is create another clip of the size and place it over the jpeg, while set the alpha value to 0 so jpeg underneath can be seen.

this is code:

for (var i=0; i<=flagArray.length-1; i++) {

    var a=i;

    arrayFlag="country/thumb/"+flagArray;

    arrayBg="country/"+flagArray;

    //--------flags-------

    mybutton = this.createEmptyMovieClip("mc"+i, i);

    this["mc"+i].loadMovie(arrayFlag);

    this["mc"+i]._x=95+distance;

    this["mc"+i]._y=0;

    //---------Name----------

    var str:String = flagArray.split(".jpg").join("");

    trace (str);

    this.createTextField("my_txt"+i, 100+i, distance, 12, 100, 100);

    this["my_txt"+i].text = str;

    this["my_txt"+i].setTextFormat(format2);

    //--------Text-----------

    this.createTextField("my_title"+i,200+i, distance, 0, 150, 100);

    this["my_title"+i].text = "Country Name:";

    this["my_title"+i].setTextFormat(format1);

   

       

   

   

    //-----loadMovie--------- create alpha = 0 clip over top of import jpeg --

    attachMovie("invbtton", "cover"+i, 300+i);

    this["cover"+i]._alpha = 0;

    this["cover"+i]._x = 95+distance;

    this["cover"+i]._y = 0;

    this["cover"+i].cover_inNum = i;

   

    this["cover"+i].onRelease = function() {

        var bb = this.cover_inNum;

        mc.loadMovie(arrayBg[bb]);

    }

   

   

    distance+=193;

    //trace(this["cover"+i].bb);

    //trace(arrayBg);

}