PDA

View Full Version : External SWF actionscript problem



RossKidd
March 10th, 2010, 05:10 PM
I am using Lee Brimelow's oop scrollbar.

http://www.gotoandlearn.com/play?id=72


This works on its own but when I try and load it as am external movieclip the scrollbar stops working.

my external swf is added to the stage and cast as a movieclip with.

var externalMovie = MovieClip(loader.content);


I think the line

stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);

may be the problem. How do I reference the stage from an external MC?

Many thanks.

full as code below



package com.leebrimelow.ui
{
import flash.display.*;
import flash.events.*;

public class ScrollBar extends MovieClip
{
private var yOffset:Number;
private var yMin:Number;
private var yMax:Number;

public function ScrollBar():void
{
yMin = 0;
yMax = track.height - thumb.height;
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
}

private function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
yOffset = mouseY - thumb.y;
}

private function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
}

private function thumbMove(e:MouseEvent):void
{
thumb.y = mouseY - yOffset;
if(thumb.y <= yMin)
thumb.y = yMin;
if(thumb.y >= yMax)
thumb.y = yMax;
dispatchEvent(new ScrollBarEvent(thumb.y / yMax));
e.updateAfterEvent();
}
}
}



package com.leebrimelow.ui
{
import flash.events.*;

public class ScrollBarEvent extends Event
{
public static const VALUE_CHANGED = "valueChanged";
public var scrollPercent:Number;

public function ScrollBarEvent(sp:Number):void
{
super(VALUE_CHANGED);
scrollPercent = sp;
}

}
}


package com.leebrimelow.ui
{
import flash.display.*;
import flash.events.*;
import caurina.transitions.*;

public class ScrollBox extends MovieClip
{
public function ScrollBox():void
{
sb.addEventListener(ScrollBarEvent.VALUE_CHANGED, sbChange);
}

private function sbChange(e:ScrollBarEvent):void
{
Tweener.addTween(content, {y:(-e.scrollPercent*(content.height-masker.height)),
time:1});
}
}
}

snickelfritz
March 10th, 2010, 05:45 PM
I seem to recall that the classes from that tutorial required some changes to get them to work properly in an external SWF.
Here's classes I made from that tutorial, with some changes.
The code works perfectly, AFAIK, in external SWF files.

Scroller.as


package com.snick.ui
{
import flash.display.*;
import flash.events.*;

public class Scroller extends MovieClip
{
var yOffset:Number;
var yMin:Number;
var yMax:Number;

public function Scroller():void
{
yMin = 0;
yMax = track.height - thumb.height;
thumb.buttonMode = true;
addEventListener(Event.ADDED_TO_STAGE, initScroll);
}

private function initScroll(e:Event):void
{
thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
removeEventListener(Event.ADDED_TO_STAGE, initScroll);
addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
}

private function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
yOffset = mouseY - thumb.y;
}

private function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
trace("thumbUp");
}

private function thumbMove(e:MouseEvent):void
{
thumb.y = mouseY - yOffset;
if (thumb.y <= yMin)
{
thumb.y = yMin;
}
if (thumb.y >= yMax)
{
thumb.y = yMax;
}
dispatchEvent(new ScrollerEvent(thumb.y / yMax));
e.updateAfterEvent();
}

private function cleanUp(e:Event):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, thumbUp);
thumb.removeEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
removeEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
}

}

}


ScrollerContent.as


package com.snick.ui
{
import flash.display.*;
import flash.events.*;
import flash.text.TextField;
import com.greensock.*;
import com.greensock.easing.*;

public class ScrollerContent extends MovieClip
{
public function ScrollerContent():void
{
sb.addEventListener(ScrollerEvent.VALUE_CHANGED, sbChange);
}

private function sbChange(e:ScrollerEvent):void
{
TweenMax.to(content, 2, {y:-e.scrollPercent * (content.height - masker.height), ease:Cubic.easeOut});
}
}
}



ScrollerEvent.as


package com.snick.ui
{
import flash.events.*;

public class ScrollerEvent extends Event
{
public static const VALUE_CHANGED = "valueChanged";
public var scrollPercent:Number;

public function ScrollerEvent(sp:Number):void
{
super(VALUE_CHANGED);
scrollPercent = sp;
}

}
}