开发者

Trying to generate Buttons with a loop, but Icon property causes problem

开发者 https://www.devze.com 2023-02-20 10:05 出处:网络
I\'m trying to generate a button with the loop of an array, but I can\'t make the icon style and the stylename work :(

I'm trying to generate a button with the loop of an array, but I can't make the icon style and the stylename work :(

        for (var x:int = 0; x < smileys.length; x++ ) {
            var emoticon:Butt开发者_如何学JAVAon = new Button();
            var label:String = smileys[x][0];
            emoticon.width = 24; emoticon.height = 24;
            emoticon.x = positionX; emoticon.y = 0;
            emoticon.styleName('buttonImg'); // doesn't work...
            emoticon.setStyle("icon", "@Embed(source='smileys/"+smileys[x][0]+".png')"); // doesn't work
            emoticonsGroup.addChild(emoticon);
            positionX+= 24;
        }

I also tried to insert this in the loop but it doesn't work neither :

        [Embed(source="smileys/"+smileys[x][0]+".png")] // fb doesn't like that
        var buttonIcon:Class;
        emoticon.setStyle("icon", buttonIcon);


You can't put variables in an embedding since it's an instruction for the compiler, not a runtime resolution. Now, you can either

  • create as many variables as smileys you have and then have N static embeddings
  • create your own skin for a button which accepts an Image or a Loader and load dynamically the image inside the button
  • preload an swf with all your images and use emoticon.setStyle("icon", getDefinitionByName(smileys[x][0])) where smileys[x][0] is the linkage name of the smiley in the Flash library.
0

精彩评论

暂无评论...
验证码 换一张
取 消