Loading and unloading content from library in AS3

霸气de小男生 提交于 2019-12-20 05:12:30

问题


I'm doing a flash project but I'm new to actionscript. I have a menu in the main page and I want to make other pages appear when I click on menu items.

I know how to load movieclips from library but I don't know how to specify their position on the screen and how to make them appear in a specific layer.

Is it necessary to unload previous content when I click on another item of menu? will they stack on each other and make the program heavy if I don't unload them?!

I've added a layer for each menu item and added the following code in each of them (For every item there is a different class):

import flash.display.MovieClip;

btn_msg.addEventListener(MouseEvent.CLICK, ShowMessegePage);

function ShowMessegePage(event:MouseEvent):void
{
var msg_page:MessegePage = new MessegePage();
    addChild(msg_page);

msg_page.x = 495;
msg_page.y = 323;

}

I have placed an image in the first frame as the main page. Then I placed some buttons each on separate layers. In order to load a page while clicking on each button, I have added the above code.

When I click on button A, Page A is loaded, when I click on button B, page B is loaded and so on. How can I see the main page again? I have added a button for the main page but for some reason I do not what to load it from library, it is in the first frame and under loaded contents. My question is how can I remove loaded contents so that I can see the main page again? Should I remove every previously loaded content when I click on every button and then load new contents?


回答1:


Loading MovieClip from library:

var mc:MovieClip = new MyClipID();
parentLayer.addChild(mc); // parentLayer can be any DisplayObjectContainer, even the stage

Positioning a loaded clip:

mc.x = 15;
mc.y = 30;

Removing a loaded clip:

if(mc.parent)
{
    mc.parent.removeChild(mc);
}

Placing a clip at a specific "layer" (index):

parentLayer.addChildAt(mc, 0); // this will place it behind everything on parentLayer

UPDATE: To answer your latest question, yes, you should remove the previously loaded clip before adding a new one. Then, when you want to display the main image again, you can just remove the currently loaded clip.

UPDATE2: An example of keeping track which page is currently loaded and removing it before displaying a new page:

import flash.display.MovieClip;
import flash.display.DisplayObject;

var currentPage:DisplayObject;

btn_msg.addEventListener(MouseEvent.CLICK, ShowMessagePage);
btn_msg2.addEventListener(MouseEvent.CLICK, ShowMesssagePage2);

function ShowMessagePage(event:MouseEvent):void
{
    removeCurrentPage(); // remove the old page
    var msg_page:MessagePage = new MessagePage();
        addChild(msg_page);

    msg_page.x = 495;
    msg_page.y = 323;
    currentPage = msg_page; // keep track of the current page
}

function ShowMessagePage2(event:MouseEvent):void 
{
    removeCurrentPage(); // remove the old page
    var msg_page2:MessagePage2 = new MessagePage2();
        addChild(msg_page2);

    msg_page2.x = 495;
    msg_page2.y = 323;
    currentPage = msg_page2; // keep track of the current page
}

function removeCurrentPage():void
{
    if(currentPage && currenPage.parent)
    {
        currentPage.parent.removeChild(currentPage);
    }
}


来源:https://stackoverflow.com/questions/22940461/loading-and-unloading-content-from-library-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!