Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

create dynamic button with AS2

Participant ,
May 26, 2013 May 26, 2013

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);

}

TOPICS
ActionScript
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 27, 2013 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;

    //-------

...
Translate
Participant ,
May 27, 2013 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2013 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2013 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2013 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);

    };

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2013 May 27, 2013

sigh, somehow I'm now feeling that my only option is to add all the jpegs one by one manually, and asign each instance name manually... and thats near 200 jpegs there..  thats a bye bye bedtime

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2013 May 27, 2013

if you want to retrieve the value of i when a movieclip was created, you can apply the string methods to the movieclip's _name property or, even easier, just assign the value of i to a movieclip property:

this["mc"+i].ivar=i;  // stores the value of i (when the movieclip was created) in the movieclip's ivar property

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

     trace (arrayBg[this.ivar]);

// or, arrayBg[this._name.substr(2)], but using ivar is easier and less prone to errors.

        mc.loadMovie(arrayBg);

    };

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 27, 2013 May 27, 2013

thanks a lot for reply , i got it work now.

it all work out now, but as everything does work, it comes a little issue ( not really little )

the level, I used 200+i, 100+i, 400+i on different elements on stage, when there r more than 30 or so jpeg sent to stage, with all the texts and stuffs,  somehow it become unstablized and affect the other components on the main movie.

I just realized when you have 1 element on 1 level and it stack up to 500+ or so it really isn't a good idea.

but if i don't do 200+i, just 200, for example. the next jpeg stay where the last one was.

is there way get around it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2013 May 28, 2013

there are 2 ways to ensure there are no depth collisions:

1. control your depths using a variable (eg, dep).  initialize dep and then increment everytime it's used:

var dep:Number=0;

whatever.attachMovie("id","name",dep++);

something.createEmptyMovieClip("name",dep++);

etc

2.  let flash control your depths (fp 8+) and use getNextHighestDepth():

whatever.attachMovie("id","name",whatever.getNextHighestDepth());

something.createEmptyMovieClip("name",something.getNextHighestDepth());

etc

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 08, 2013 Jun 08, 2013

I tried to fix all the scripts I had so far to use getNextHighestDepth().. took a while

thanks a lot.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 08, 2013 Jun 08, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines