Flash AS3 - AddChild 超出精灵范围

发布于 2024-11-26 16:14:00 字数 2393 浏览 0 评论 0原文

我正在尝试在 Flash AS3 中创建动态滚动列表。当我创建列表时,我将其设置为初始长度。后来,我向列表中添加了超出列表原始大小的更多对象。我希望能够向下滚动列表并查看所有对象。

我的滚动条只是移动列表的 y 位置。但是,当我运行程序并滚动时,不会显示在原始列表大小之外绘制的每个对象。我该如何解决这个问题?

我有一个列表对象,当我需要将内容添加到列表时,它具有以下功能。当我初始化列表时,我给它一个初始高度和宽度,我用它来创建一个蒙版。

public function AddPlayers(pPlayers:Array, pScrollBar:ScrollBar):void
{
        var player:Player;

        trace("happened again");
        for (var i:int = 0; i < pPlayers.length; i++)
        {               
            player = pPlayers[i];                               
            this.addChild(player);
            player.x = 0;
            player.y = player.height * i;
        }

        pScrollBar.InitializeScrollBar(this);       
}

InitializeScrollBar 函数根据添加内容的大小更新滚动条的属性。它主要是根据列表内容的大小来改变滚动面的大小。

public function InitializeScrollBar(pList:List)
{
        this.list = pList;

        // Size and place scroll track
        scrollTrack.height = scrollHeight - 2 * scrollUp.height;
        scrollTrack.y = scrollUp.height;            
        addChild(scrollTrack);          

        // Place scroll face
        scrollFace.y = scrollUp.height;
        scrollFace.height = scrollTrack.height * (scrollHeight / list.height);          
        addChild(scrollFace);

        // Place scroll buttons
        addChild(scrollUp);
        scrollDown.y = scrollDown.height + scrollTrack.y + scrollTrack.height;
        addChild(scrollDown);
        scrollDown.scaleY = -1;


        scrollFace.addEventListener(MouseEvent.MOUSE_DOWN, MoveScrollFace);

        scrollDown.addEventListener(MouseEvent.CLICK, MoveDown)
        scrollUp.addEventListener(MouseEvent.CLICK, MoveUp)
}

我还在滚动条中有一个更新函数,当拖动滚动面时,它使用 ENTER FRAME 事件。

private function Update(evt:Event)
{
        trace(mouseY);
        scrollFace.y = mouseY;

        if (scrollFace.y <= scrollTrack.y)
        {
            scrollFace.y = scrollTrack.y;
        }
        else if (scrollFace.y + scrollFace.height >= scrollTrack.y + scrollTrack.height)
        {
            scrollFace.y = scrollTrack.y + scrollTrack.height - scrollFace.height;
        }

        var scrollChange:Number = (list.height - scrollHeight)/(scrollTrack.height - scrollFace.height);
        list.y = -1 * scrollChange * (scrollFace.y - scrollUp.height);          
        list.y = list.y + list.y % 25;

}   

I am trying to create a dynamic scrolling list in Flash AS3. When I create a list, I set it to be an initial length. Later, I add more objects to the list which is outside of the original size of the list. I want to be able to scroll down the list and see all the objects.

My scrollbar just moves the list's y position. However, every object that is drawn outside the original list size is not shown when I run the program and scroll. How can I fix this?

I have a list object which has the function below when I need to add content to the list. When I initialize the list, I give it an initial height and width which I use to create a mask.

public function AddPlayers(pPlayers:Array, pScrollBar:ScrollBar):void
{
        var player:Player;

        trace("happened again");
        for (var i:int = 0; i < pPlayers.length; i++)
        {               
            player = pPlayers[i];                               
            this.addChild(player);
            player.x = 0;
            player.y = player.height * i;
        }

        pScrollBar.InitializeScrollBar(this);       
}

The InitializeScrollBar function updates the properties of the scrollbar based on the size of the content added. It's mainly changing the size of the scroll face depending on the size of the list content.

public function InitializeScrollBar(pList:List)
{
        this.list = pList;

        // Size and place scroll track
        scrollTrack.height = scrollHeight - 2 * scrollUp.height;
        scrollTrack.y = scrollUp.height;            
        addChild(scrollTrack);          

        // Place scroll face
        scrollFace.y = scrollUp.height;
        scrollFace.height = scrollTrack.height * (scrollHeight / list.height);          
        addChild(scrollFace);

        // Place scroll buttons
        addChild(scrollUp);
        scrollDown.y = scrollDown.height + scrollTrack.y + scrollTrack.height;
        addChild(scrollDown);
        scrollDown.scaleY = -1;


        scrollFace.addEventListener(MouseEvent.MOUSE_DOWN, MoveScrollFace);

        scrollDown.addEventListener(MouseEvent.CLICK, MoveDown)
        scrollUp.addEventListener(MouseEvent.CLICK, MoveUp)
}

I also have an Update function in the scrollbar which uses an ENTER FRAME event when the scrollface is dragged.

private function Update(evt:Event)
{
        trace(mouseY);
        scrollFace.y = mouseY;

        if (scrollFace.y <= scrollTrack.y)
        {
            scrollFace.y = scrollTrack.y;
        }
        else if (scrollFace.y + scrollFace.height >= scrollTrack.y + scrollTrack.height)
        {
            scrollFace.y = scrollTrack.y + scrollTrack.height - scrollFace.height;
        }

        var scrollChange:Number = (list.height - scrollHeight)/(scrollTrack.height - scrollFace.height);
        list.y = -1 * scrollChange * (scrollFace.y - scrollUp.height);          
        list.y = list.y + list.y % 25;

}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-12-03 16:14:00

如果您使用标准 UIScrollbar 组件,它有一个内置函数 scrollBar.update(); 您应该在更改内容大小后使用此函数。

If you use standard UIScrollbar component it has a build in function scrollBar.update(); You should use this function after changing content size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文