views:

3775

answers:

5

I can not control the scrolling of a scrollpane from within my actionscript (I am using actionscript 3.0)

Here is my Definition:

var scrollPane:ScrollPane = new ScrollPane();
scrollPane.verticalScrollPolicy = "false";
scrollPane.move(374, 0);
scrollPane.setSize(476, 370);

When I try to set it - scrollPane.horizontalScrollPosition = 500; I get the following error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at fl.containers::ScrollPane/setScrollDrag()
    at fl.containers::ScrollPane/draw()
    at fl.core::UIComponent/drawNow()
    at fl.containers::BaseScrollPane/set horizontalScrollPosition()
    at Program()
    at Preloader/main()
    at Preloader/enterFrameHandler()

Even scrollPane.horizontalScrollPosition = scrollPane.horizontalScrollPosition; throws the same error.

However trace(scrollPane.horizontalScrollPosition); works just fine.

I have tried hPosition also and it does not work at all. Any clues as to how I can control the scroll position would be greatly appreciated.

Below is the entire code

package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.containers.ScrollPane;


    public class Program extends Sprite {
     public function Program() {
      //define variable
      var startX:Number;
      var startY:Number;
      var counter:Number = 0;
      const LIMIT:Number = 100;
      var intMessages:Number = 80;
      var intBoardSize:Number = 476*(Math.round(intMessages/15)+1);
      var intMessageMinX:Number = 35;
      var intMessageMaxX:Number = intBoardSize-99;
      var intMessageMinY:Number = 35;
      var intMessageMaxY:Number = 310;
      var activeGloveName:String = ""
      var intDegreeHigh:Number = 45;
      var intDegreeLow:Number = -45;

      //create objects
      var mcglove:mcGlove = new mcGlove();
      var mcglovetarget:mcGloveTarget = new mcGloveTarget();
      var myDropShadowFilterSmall = new DropShadowFilter (2,45,0x000000,0.65,3,3,2,3,false,false,false);
      var myDropShadowFilterDown = new DropShadowFilter (3,45,0x000000,0.65,5,5,2,3,false,false,false);
      var myDropShadowFilterUp = new DropShadowFilter (5,45,0x000000,0.65,7,7,2,3,false,false,false);
      var topShadow = new DropShadowFilter (3,90,0x000000,0.35,8,8,2,3,false,false,false);
      var holder:MovieClip = new MovieClip();
      var pinkline:Sprite = new Sprite();
      var searchBox:Sprite = new Sprite();
      var topShadowBox:Sprite = new Sprite();
      var aMessages:Array = new Array();


      //Create a glove for each message
      for (var x = 0; x < intMessages; x++){
       aMessages[x] = new mcGlove();
       aMessages[x].scaleX = .28;
       aMessages[x].scaleY = .28;
       counter = 0;
       position(aMessages[x]);
       aMessages[x].rotation = (0, 0, 0, Math.round(Math.random() * (intDegreeHigh - intDegreeLow)) + intDegreeLow);
       aMessages[x].filters = [myDropShadowFilterSmall];
       holder.addChild(aMessages[x]);
       aMessages[x].name = "glove" + x;
       aMessages[x].addEventListener(MouseEvent.MOUSE_DOWN, selectMessage);
       aMessages[x].addEventListener(MouseEvent.MOUSE_UP, releaseMessage);
       aMessages[x].buttonMode = true;
      }

      var scrollPane:ScrollPane = new ScrollPane();
      scrollPane.verticalScrollPolicy = "false";
            scrollPane.move(374, 0);
            scrollPane.setSize(476, 370);
      scrollPane.horizontalLineScrollSize = 120;


      topShadowBox.graphics.beginFill(0x333333);
      topShadowBox.graphics.drawRect(0,0,870,5);
      topShadowBox.x = -10;
      topShadowBox.y = -5;
      topShadowBox.filters = [topShadow];

      pinkline.graphics.beginFill(0xDB9195);
      pinkline.graphics.drawRect(0,0,476,2);
      pinkline.x = 374;
      pinkline.y = 353;

      searchBox.graphics.beginFill(0xDB9195);
      searchBox.graphics.drawRect(0,0,476,25);
      searchBox.x = 374;
      searchBox.y = 370;

      //add to frame
      sortObjects();

      scrollPane.source = holder;

      scrollPane.horizontalScrollPosition = 5;

      //create instance names for referancing/compairing objects
      mcglove.name = "mcglove";
      mcglovetarget.name = "mcglovetarget";

      //position the glove and modify apperiance
      mcglove.x = 163;
      mcglove.y = 211;
      mcglove.filters = [myDropShadowFilterDown];
      mcglovetarget.x = 615;
      mcglovetarget.y = 211;
      mcglovetarget.alpha = 0

      //action listeners
      mcglove.addEventListener(MouseEvent.MOUSE_DOWN, selectGlove);
      mcglove.addEventListener(MouseEvent.MOUSE_UP, releaseGlove);

      function position(target) {
       target.x = Math.round(Math.random() * (intMessageMaxX - intMessageMinX)) + intMessageMinX;
       target.y = Math.round(Math.random() * (intMessageMaxY - intMessageMinY)) + intMessageMinY;
       for (var i:uint=0; i<aMessages.length -1 ; i++) {
        if(target.hitTestObject(aMessages[i]) && counter < LIMIT){
         counter++;
         position(target);
         return false;
        };
       }
      } 

      function selectGlove(event:MouseEvent):void {
       event.target.startDrag(true);
       var myTargetName:String = event.target.name + "target";
       var myTarget:DisplayObject = getChildByName(myTargetName);
       mcglove.filters = [myDropShadowFilterUp];
       addChild(myTarget);
       event.target.parent.addChild(event.target);
       addChild(topShadowBox);
       myTarget.alpha = .5;
       startX = event.target.x;
       startY = event.target.y;
      }

      function releaseGlove(event:MouseEvent):void {
       event.target.stopDrag();
       var myTargetName:String = event.target.name + "target";
       var myTarget:DisplayObject = getChildByName(myTargetName);
       event.target.filters = [myDropShadowFilterDown];
       myTarget.alpha = 0;

       if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, selectGlove);
        event.target.removeEventListener(MouseEvent.MOUSE_UP, releaseGlove);
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;
        var myTween:Tween = new Tween(getChildByName(event.target.name), "scaleX",Strong.easeOut,getChildByName(event.target.name).scaleX,.28,2,true);
        var myTween2:Tween = new Tween(getChildByName(event.target.name), "scaleY",Strong.easeOut,getChildByName(event.target.name).scaleY,.28,2,true);
        holder.addChild(getChildByName(event.target.name));
        scrollPane.source = holder;
        holder.getChildByName(event.target.name).x=Math.round(Math.random() * (470+Math.round(scrollPane.horizontalScrollPosition) - Math.round(scrollPane.horizontalScrollPosition))) + Math.round(scrollPane.horizontalScrollPosition);
        holder.getChildByName(event.target.name).y=Math.round(Math.random() * (intMessageMaxY - intMessageMinY)) + intMessageMinY;
        holder.getChildByName(event.target.name).addEventListener(MouseEvent.MOUSE_DOWN, selectMessage);
        holder.getChildByName(event.target.name).addEventListener(MouseEvent.MOUSE_UP, releaseMessage);
       } else {
        event.target.x = startX;
        event.target.y = startY;
       }

              //sortObjects();
       addChild(scrollPane);
       addChild(pinkline);
       addChild(topShadowBox);
      }

      function selectMessage(event:MouseEvent):void{
       if (activeGloveName != ""){
        var activeGlove:DisplayObject = holder.getChildByName(activeGloveName);
        activeGlove.filters = [myDropShadowFilterSmall];
        activeGlove.scaleX = .28;
        activeGlove.scaleY = .28;


       holder.addChild(activeGlove);
       }
       activeGloveName = event.target.name;
       //event.target.dragIt();
       holder.addChild(holder.getChildByName(event.target.name));
       event.target.filters = [new GlowFilter(0xABEEFC, .65, 15, 15, 1, 1, false, false)];
       event.target.scaleX = .35;
       event.target.scaleY = .35;
      }

      function releaseMessage(event:MouseEvent):void{
       event.target.dropIt();
      }

      function sortObjects():void{
       addChild(mcglovetarget);
       addChild(scrollPane);
       addChild(pinkline);
       addChild(searchBox);
       addChild(mcglove);
       addChild(topShadowBox);
      }

      mcglove.buttonMode = true;

     }
    }
}
+1  A: 

horizontalScrollPosition is a property of ScrollPane so you would set it through

scrollPane.horizontalScrollPosition = 500;

instead of a method call.

James Hay
Alright, well narrows it down... thanks, I do see how you are getting that now.
Patcouch22
+1  A: 

If you look at the error trace, the error is occuring inside of

fl.containers::ScrollPane/setScrollDrag().

If you look here you find out that scrollDrag ...

Gets or sets a value that indicates whether scrolling occurs when a user drags on content within the scroll pane.

You never put content into the scroll pane

scrollPane.source = "...";

I believe this is why the error is occuring, try putting some content into the scrollpane before you set the horizontalScrollPosition.

ForYourOwnGood
I posted the full code above. I do have content I just did not show it. You could still be right though if the content is not populating before that call... I will look into that more, but I don't think its the issue. Thanks!
Patcouch22
you should also look at scrollpane.validateNow() to make sure the scrollpane is rendered before the call.
ForYourOwnGood
validateNow() didn't work:Error #1009: Cannot access a property or method of a null object reference at fl.containers::ScrollPane/setScrollDrag() at fl.containers::ScrollPane/draw() at fl.core::UIComponent/validateNow() at Program() at Preloader/main() at Preloader/enterFrameHandler()
Patcouch22
Ok... idk why but for some reason it is not rendering... just like u said... so while I can't get it to start in any particular position, it works within the event listener. Thanks!
Patcouch22
A: 

You should add ScrollPane to stage firstly. If scrollPane.stage == null you will have such error.

Roman
A: 

I had the same problem and figured out what caused it. At least in my case, the problem was that the scrollpane was not yet a child of stage.

This causes the error you describe:

sp.verticalScrollPosition = 0;    
this.addChild(sp)

While this works fine:

this.addChild(sp)
sp.verticalScrollPosition = 0;

Hope it helps :)

SuperIRis
A: 

Hi this has been of great help to me as the scrollPane hasn't been added to the stage and there was a call to scroll it. I put a check to return the control as long as the scrollPane.stage returns null.

anonymous