Search code examples
actionscript-3scrolladdchildscrollpane

ScrollPane doesn't work with dynamic content in as3


scrollPane.setSize(400,400);
scrollPane.source=emptyc;

Where emptyc is a container in which I add content dynamically (i.e. by addChild method) doesn't work. It simply doesn't scroll at all.

Neither does work if I add content using scrollPane as a container itself (i.e.:

scrollPane.addChild(myChild);

Solution

  • The problem is the ScollPane instance has no clue you've updated it's content (added a child to emptyc/etc.) so you need to tell it to update().

    Here's a basic example:

    var b:BitmapData = new BitmapData(2,2,false,0xFFFFFF);
    b.setPixel(0,0,0);b.setPixel(1,1,0);
    var s:Shape = new Shape();
    
    var sp:ScrollPane = new ScrollPane();
    sp.scrollDrag = true;
    sp.source = s;
    addChild(sp);
    
    
    s.graphics.beginBitmapFill(b);
    s.graphics.drawRect(0,0,1000,1000);
    s.graphics.endFill();
    sp.update();
    

    Notice that you get the same behaviour you mention if you comment out sp.update();. Also, there's an example in the documentation.