我无法从我的动作脚本中控制滚动窗格的滚动(我使用的是动作脚本3.0)
这是我的定义:
var scrollPane:ScrollPane = new ScrollPane();
scrollPane.verticalScrollPolicy = "false";
scrollPane.move(374, 0);
scrollPane.setSize(476, 370);
当我尝试设置它时 - scrollPane.horizontalScrollPosition = 500;我收到以下错误
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()
甚至scrollPane.horizontalScrollPosition = scrollPane.horizontalScrollPosition;抛出同样的错误。
但是跟踪(scrollPane.horizontalScrollPosition);工作得很好。
我也尝试了hPosition,它根本不起作用。任何关于如何控制滚动位置的线索都将非常感激。
以下是整个代码
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;
}
}
}
答案 0 :(得分:2)
我遇到了同样的问题并找出了导致它的原因。至少在我的情况下,问题是滚动窗格还不是舞台的孩子。
这会导致您描述的错误:
sp.verticalScrollPosition = 0;
this.addChild(sp)
虽然这很好用:
this.addChild(sp)
sp.verticalScrollPosition = 0;
希望有所帮助:)
答案 1 :(得分:1)
horizontalScrollPosition是ScrollPane的一个属性,因此您可以通过
进行设置scrollPane.horizontalScrollPosition = 500;
而不是方法调用。
答案 2 :(得分:1)
如果查看错误跟踪,则错误发生在
内fl.containers::ScrollPane/setScrollDrag().
如果你看{4},你会发现那个scrollDrag ......
获取或设置一个值,该值指示当用户拖动滚动窗格中的内容时是否发生滚动。
您永远不会将内容放入滚动窗格
scrollPane.source = "...";
我相信这就是错误发生的原因,在设置horizontalScrollPosition之前尝试将一些内容放入滚动窗格。
答案 3 :(得分:0)
您应首先将ScrollPane
添加到stage
。如果scrollPane.stage == null
您将出现此类错误。