简体   繁体   English

如何在as3中访问按钮对象内的影片剪辑对象(flash cs4)

[英]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). 我想动态地将按钮的图形加载到按钮的每个框架内的mc(向上和向上)。 Inside each frame I have a movie clip (canvas and canvas_over) 在每个帧内我都有一个影片剪辑(canvas和canvas_over)

The green box is the button object (header_btn): 绿色框是按钮对象(header_btn):

绿色框是按钮对象(header_btn)

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 object 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_canvasheader_canvas_over被放置在舞台上。 header_canvas_over is placed over header_canvas . header_canvas_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. 如果as3允许按钮包含对象,那将是很好的。 :/ :/

The SimpleButton class is extended from DisplayObject, not DisplayObjectContainer. SimpleButton类是从DisplayObject扩展而不是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. 对于你想做的事情,可能值得创建一个扩展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. 您必须自己添加代码才能在用户交互状态之间移动,但您可以更好地控制状态之间的视觉转换以及它们的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM