开发者

How to access a movie clip object inside a button object in as3 (flash cs4)

I want to dynamically load the graphic of the button into an mc inside each frame of the button (up and over). Inside each frame I have a movie clip (canvas and canvas_over)

The green box is the button object (header_btn):

How to access a movie clip object inside a button object in as3 (flash cs4)

This is my code:

var hLoader:Loader = new Loader();
hLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, hLoaded);
hLoader.load(new URLRequest("http://django.liveproject.is/misc/current_flash_header/image.png"));

function hLoaded(event:Event):void {
    var image:Bitmap = new Bitmap(event.target.content.bitmapData);
    header_btn.canvas.addChild(image);
}

var hoLoader:Loader = new Loader();
hoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, hoLoaded);
hoLoader.load(new URLRequest("http://django.liveproject.is/misc/current_flash_header_over/image.png"));

function hoLoaded(event:Event):void {
    var image:Bitmap = new Bitmap(event.target.content.bitmapData);
    header_btn.over_canvas.addChild(image);
}

The error I get is:

TypeError: Error #1009: Cannot access a property or method of a null objec开发者_如何学Pythont reference.
    at MethodInfo-78()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at MethodInfo-77()

Edit - Solved:

header_canvas and header_canvas_over are placed on stage. header_canvas_over is placed over header_canvas.

Code:

var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;

var hLoader:Loader = new Loader();
hLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, hLoaded);
hLoader.load(new URLRequest("http://django.liveproject.is/misc/current_flash_header/image.png"), context);

function hLoaded(event:Event):void {
    var image:Bitmap = new Bitmap(event.target.content.bitmapData);
    header_canvas_up.addChild(image);
}

var hoLoader:Loader = new Loader();
hoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, hoLoaded);
hoLoader.load(new URLRequest("http://django.liveproject.is/misc/current_flash_header_over/image.png"), context);

function hoLoaded(event:Event):void {
    var image:Bitmap = new Bitmap(event.target.content.bitmapData);
    header_canvas_over.addChild(image);
    header_canvas_over.visible = false;

    header_btn.addEventListener(MouseEvent.MOUSE_OVER, onHover);
    function onHover(event:Event):void {
        header_canvas_over.visible = true;
    }

    header_btn.addEventListener(MouseEvent.MOUSE_OUT, onOut);
    function onOut(event:Event):void {
        header_canvas_over.visible = false;
    }
}

It would be nice though if as3 allowed for buttons to contain objects. :/


The SimpleButton class is extended from DisplayObject, not DisplayObjectContainer. That means that you can't access objects on a button's timeline in that way, nor can you add or remove children.

For what you're wanting do do it might be worth creating a custom button class that extends MovieClip. You'd have to add code yourself for moving between the states on user interaction, but you would get a lot more control over the visual transition between states as well as the content of them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜