PDA

View Full Version : Accessing functions in separate AS file



SarasotaTheBand
February 5th, 2010, 08:36 PM
I'm working on an mp3 player. It makes use of a custom scrollbar I found here: http://www.flashscaper.com/blog/?p=3.

I've gotten it to work so far, but there is one bit I would like to add. I would like it to scroll to specific y-coords when a button is clicked. The code for the scrollbar is all in an external AS file, which is referred to by an exported movieclip in the main fla.

Does anyone have any idea how I would go about referring to this file, or just some other method of doing this? I'll paste the AS code as well as the basic fla code. THANK YOU!

fla code:


var s:Sprite = new Sprite();
txt_mc.spacer.visible = false;
sb.init(txt_mc, "easeOutBack",0,true,2);
AS code:


package {

import caurina.transitions.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;



public class Scrollbar extends MovieClip {

private var target:MovieClip;
private var top:Number;
private var bottom:Number;
private var dragBot:Number;
private var range:Number;
private var ratio:Number;
private var sPos:Number;
private var sRect:Rectangle;
private var ctrl:Number;//This is to adapt to the target's position
private var trans:String;
private var timing:Number;
private var isUp:Boolean;
private var isDown:Boolean;
private var isArrow:Boolean;
private var arrowMove:Number;
private var upArrowHt:Number;
private var downArrowHt:Number;
private var sBuffer:Number;

public function Scrollbar():void {
scroller.addEventListener(MouseEvent.MOUSE_DOWN, dragScroll);
stage.addEventListener(MouseEvent.MOUSE_UP, stopScroll);
stage.addEventListener(MouseEvent.MOUSE_WHEEL,mous eWheelHandler);
}
//
public function init(t:MovieClip, tr:String,tt:Number,sa:Boolean,b:Number):void {
target = t;
trans = tr;
timing = tt;
isArrow = sa;
sBuffer = b;
if (target.height <= track.height) {
this.visible = false;
}

//
upArrowHt = upArrow.height;
downArrowHt = downArrow.height;
if (isArrow) {
top = scroller.y;
dragBot = (scroller.y + track.height) - scroller.height;
bottom = track.height - (scroller.height/sBuffer);

} else {
top = scroller.y;
dragBot = (scroller.y + track.height) - scroller.height;
bottom = track.height - (scroller.height/sBuffer);

upArrowHt = 0;
downArrowHt = 0;
removeChild(upArrow);
removeChild(downArrow);
}
range = bottom - top;
sRect = new Rectangle(0,top,0,dragBot);
ctrl = target.y;
//set Mask
isUp = false;
isDown = false;
arrowMove = 3;

if (isArrow) {
upArrow.addEventListener(Event.ENTER_FRAME, upArrowHandler);
upArrow.addEventListener(MouseEvent.MOUSE_DOWN, upScroll);
upArrow.addEventListener(MouseEvent.MOUSE_UP, stopScroll);
//
downArrow.addEventListener(Event.ENTER_FRAME, downArrowHandler);
downArrow.addEventListener(MouseEvent.MOUSE_DOWN, downScroll);
downArrow.addEventListener(MouseEvent.MOUSE_UP, stopScroll);


}
var square:Sprite = new Sprite();
square.graphics.beginFill(0xFF0000);
square.graphics.drawRect(target.x, target.y, target.width+5, (track.height+upArrowHt+downArrowHt));
parent.addChild(square);
target.mask = square;

}
public function upScroll(event:MouseEvent):void {
isUp = true;
}
public function downScroll(event:MouseEvent):void {
isDown = true;
}
public function upArrowHandler(event:Event):void {
if (isUp) {
if (scroller.y > top) {
scroller.y-=arrowMove;
if (scroller.y < top) {
scroller.y = top;
}
startScroll();
}
}
}
//
public function downArrowHandler(event:Event):void {
if (isDown) {
if (scroller.y < dragBot) {
scroller.y+=arrowMove;
if (scroller.y > dragBot) {
scroller.y = dragBot;
}
startScroll();
}
}
}
//
public function dragScroll(event:MouseEvent):void {
scroller.startDrag(false, sRect);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveScroll);
}
//
public function mouseWheelHandler(event:MouseEvent):void {
if (event.delta < 0) {
if (scroller.y < dragBot) {
scroller.y-=(event.delta*2);
if (scroller.y > dragBot) {
scroller.y = dragBot;
}
startScroll();
}
} else {
if (scroller.y > top) {
scroller.y-=(event.delta*2);
if (scroller.y < top) {
scroller.y = top;
}
startScroll();
}
}
}
//
public function stopScroll(event:MouseEvent):void {
isUp = false;
isDown = false;
scroller.stopDrag();

stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveScroll);
}
//
public function moveScroll(event:MouseEvent):void {
startScroll();

}
public function startScroll():void {
ratio = (target.height - range)/range;
sPos = (scroller.y * ratio)-ctrl;

Tweener.addTween(target, {y:-sPos, time:timing, transition:trans});

}
}
}

kadaj
February 5th, 2010, 08:41 PM
In order to refer to a function inside an external file you can create an object of that class.
eg:
var obj:ExternalFile = new ExternalFile();

SarasotaTheBand
February 5th, 2010, 09:07 PM
Thanks for the help! I added the following code to test it but received this error:
"TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Scrollbar()
at test50_fla::MainTimeline/frame1()"

Any idea what I might be doing wrong?

in FLA:

var newClass:Scrollbar = new Scrollbar();
newClass.hi();

in AS:

public function hi():void {
trace("hi");
}

SarasotaTheBand
February 5th, 2010, 09:08 PM
Just to note--the AS file isn't being imported--it's being linked to with a "Scrollbar" object in the library set to export. The AS file is called "Scrollbar.as".

Thanks again for the help!