0

I am attempting to display a class derived from SimpleButton on a Flash screen. This button is supposed to load an external JPG file and use it as the display.

The code for the custom button is shown below:

package  {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;

public class CustomState extends Sprite {

    public function CustomState()
    {
        var loader:Loader = new Loader();
        loader.load(new URLRequest("file.jpg"));

        this.width = 70;
        this.height = 100;

        addChild(loader);
        trace("State Width: "+this.width);
    }

   }

}

The button state code is shown below:

package  {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;

public class CustomState extends Sprite {

    public function CustomState()
    {
        var loader:Loader = new Loader();
        loader.load(new URLRequest("file.jpg"));

        this.width = 70;
        this.height = 100;

        addChild(loader);
        trace("State Width: "+this.width);
    }

}

}

The code used to test the button is below:

package  {
import flash.display.MovieClip;

public class TestButton extends MovieClip{

    public function TestButton()
    {
        var button:CustomButton = new CustomButton();

        button.x = 10;
        button.y = 30;

        addChild(button);

    }

}

}

For reasons that are not clear, the button does not appear at all.

Can someone tell me how to make this button appear? As can be seen from the test class, I am adding the button to its display list. I also note that despite the fact that I am setting width and height for the sprite, its width and height apparently aren't being set. Something funky is going on, but my attempts to find a piece of working code that does what I am trying to do have failed.

Someone please advise...

Factor Three
  • 2,094
  • 5
  • 35
  • 51
  • 1
    Going off the top of my head here: calling load starts the load, and fires an event when it's completely loaded. You are setting the height & width without listening for that event, so it's still empty, which messes things up. Similar situation http://stackoverflow.com/questions/5134347/as3-setting-width-and-height-of-a-sprite-container – VBCPP Apr 10 '14 at 21:55
  • Well, you've put the first piece of code down twice. Did you mean to add the code for the CustomButton class? Is that a linked symbol that you made in Flash? Or is it a pure Actionscript class that, itself, instantiates a button. Clearer questions get better answers! – Craig Apr 11 '14 at 05:53

0 Answers0