2012-01-05 43 views
1

我是新来的Flash,as3和这个论坛,所以任何帮助将是伟大的!As3创建和使用外部类

我制作了一个xml图库,所有的动画片段和一切都已经动态创建,并且图像通过xml文件加载。除库中的两个按钮外,链接名称为next_btn和prev_btn。

现在我想要做的是,我有3类画廊,所以我想将我的脚本转换为我可以用于每种类型的画廊的类。 (我希望我很清楚)

当用户点击图库时,会调用函数startGallery()

我需要知道如何去做它我很笨,我已经阅读了大量的关于类的教程,但我真的不明白如何做到这一点。我非常感谢任何帮助,谢谢! :)

这里是我的代码:

//declaring variables 
var _array:Array; 
var _lastX:Number; 
var _lastWidth:Number; 
var _length:Number; 
var _firstWidth:Number; 
var _widths:Array; 
var _names:Array; 
var _sizes:Array; 
var container_mc:MovieClip; 
var my_images:XMLList; 
var count:Number = 0; 
var full_mc:MovieClip 
var currentWidth:Number; 
var scrollCounter:Number = 0; 
var rect:Shape; 
var nameLabel:TextField = new TextField(); 
var sizeLabel:TextField = new TextField(); 
var myFont = new Font1; 
var format:TextFormat; 

var myXmlLoader:URLLoader; 
var myRequest:URLRequest; 

var next_mc = new next_btn; 
var prev_mc = new prev_btn; 

    //function called when user clicks on gallery link 
function startGallery():void{ 
myXmlLoader = new URLLoader(); 
myRequest = new URLRequest("gallery.xml"); 
myXmlLoader.load(myRequest); 
myXmlLoader.addEventListener(Event.COMPLETE, processXml); 
} 

    //getting all the xml info 
function processXml(e:Event):void{ 
var myXml:XML = new XML(e.target.data); 
_length = myXml.IMAGE.length(); 
_firstWidth = [email protected]; 
currentWidth = _firstWidth; 
my_images = myXml.IMAGE; 

_array = new Array(_length); 
_names = new Array(_length); 
_sizes = new Array(_length); 
var i:int = 0; 
var j:int = 0; 
var k:int = 0; 
var l:int = 0; 

for each(var path:String in [email protected]){ 
    _array[i++] = path; 
} 

_widths = new Array(_length); 
for each(var size:Number in [email protected]){ 
    _widths[j++] = size; 
} 
for each(var names:String in [email protected]){ 
    _names[k++] = names; 
} 
for each(var sizes:String in [email protected]){ 
    _sizes[l++] = sizes; 
} 
//both methods produce the same result 
/*for(var i:int = 0; i<_length; i++){ 
    _array[i] = myXml.IMAGE[i][email protected]; 
}*/ 

createContainer(); 
callThumbs(); 
} 

    //creates the main movieclip the holds all the stuff - container_mc 
function createContainer():void{ 
container_mc = new MovieClip(); 
container_mc.name = "container_mc"; 
addChild(container_mc); 
//container_mc.alpha = 0; 
//container_mc.mouseEnabled = false; 
//container_mc.mouseChildren = false; 
container_mc.x = ((stage.stageWidth-_firstWidth)/2); 
container_mc.y = 110; 
container_mc.buttonMode = true; 
container_mc.addEventListener(MouseEvent.CLICK, callFull); 
} 


    //loades the thumbnails 
function callThumbs():void{ 
if(_array.length>0){ 
    var loader:Loader = new Loader; 
    //addChild(loader); 

    //var request:URLRequest = new URLRequest(_array[0]); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbsLoaded); 
    loader.load(new URLRequest(_array[0])); 
    trace(_array[0]); 
    trace(_names[0]); 
    loader.x = _lastX + _lastWidth + 3; 
    loader.name = String(count); 

    _lastX = loader.x; 
    _array.shift(); 
    trace(loader.name); 
    count++; 
} 
} 

function thumbsLoaded(e:Event):void{ 
_lastWidth = e.target.width; 
var myThumb:Loader = Loader(e.target.loader); 
container_mc.addChild(myThumb); 
myThumb.alpha = 0; 
TweenLite.to(myThumb, 1, {alpha:0.5, ease:Strong.easeOut, onComplete:callButtons}); 
callThumbs(); 
} 

    //once the thumbnails are loaded, the buttons are created 
function callButtons():void{ 
//buttons & info 
if(count == _length){ 
    next_mc.x = _firstWidth; 
    next_mc.y = 125; 
    next_mc.alpha = 0.7; 
    container_mc.addChild(next_mc); 
    prev_mc.x = MovieClip(root).x; 
    prev_mc.y = 125; 
    prev_mc.alpha = 0.3; 
    container_mc.addChild(prev_mc); 


    initTextFields(); 

} 
} 

    //initialising the textfields containing information about each thumbnail 
function initTextFields():void{ 
//all the textfields are initialised here 
} 

    //loads the full images when a thumbnail is clicked 
function callFull(e:MouseEvent):void{ 
if(scrollCounter == e.target.name){ 
var full_loader:Loader = new Loader(); 
var full_url = my_images[e.target.name][email protected]; 
full_loader.load(new URLRequest(full_url)); 
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded); 

container_mc.removeEventListener(MouseEvent.CLICK, callFull); 
TweenLite.to(container_mc, 1, {colorTransform:{tint:0xffffff, tintAmount:0.7}, ease:Strong.easeOut}); 
TweenLite.to(navBar_mc, 1, {colorTransform:{tint:0xffffff, tintAmount:0.7}, ease:Strong.easeOut}); 
navBar_mc.mouseEnabled = false; 
navBar_mc.mouseChildren = false; 

container_mc.buttonMode = false; 
} 
} 

function fullLoaded(e:Event):void{ 
full_mc = new MovieClip(); 
full_mc.buttonMode = true; 
addChild(full_mc); 
var my_loader:Loader = Loader(e.target.loader); 
full_mc.addChild(my_loader); 
my_loader.alpha = 0; 
TweenLite.to(my_loader, 1, {alpha:1, ease:Strong.easeOut}); 
my_loader.x = (stage.stageWidth - my_loader.width)/2; 
my_loader.y = (stage.stageHeight - my_loader.height)/2; 
my_loader.addEventListener(MouseEvent.CLICK,removeFull); 
} 

    //removes the full images once the user closes the image 
function removeFull(e:MouseEvent):void{ 
//removed the full image 
} 



function scrollOver(e:MouseEvent):void{ 
TweenLite.to(e.currentTarget, 1, {alpha:1, ease:Strong.easeOut}); 
} 

function scrollOut(e:MouseEvent):void{ 
TweenLite.to(e.currentTarget, 1, {alpha:0.7, ease:Strong.easeOut}); 
} 

function scrollClick(e:MouseEvent):void{ 
//the container_mc is moved left/right when the next/previous buttons are clicked 
} 
} 
+0

你提的问题是非常广阔的。你认为你可以把它分解成一些关于你遇到的问题的更小的具体问题吗? – Cadin 2012-01-05 22:41:07

回答

0

要在你的库由外部的支持。作为一个项目文件,先制作一个类文件,它们通常布置是这样的:

package { 

    import flash.display.MovieClip; 

    public class GalleryClassName extends MovieClip { 

    //Put reusable variables here 
    private var someThingUsedInMultipleFunctions:String = "hello"; 

    public function GalleryClassName() { 
     //Constructor, in this case it will be called automatically 
    } 

    //Put additional functions here 
    private function additionalFunction() { 
     //Do stuff 
    } 

    //Load a gallery 
    public function startGallery(xmlPath:String) { 
     //start loading the specified XML file 
     ... 
     myRequest = new URLRequest(xmlPath); 
     ... 
    } 
    } 
} 

然后在您的图书馆中,右键单击>属性>,然后导出/链接并输入您的“GalleryClassName”。这应该将代码文件与库对象链接起来。

如果你想创建一个可重用类,你可以确定3个画廊之间的变量会有所不同,并将它们存储在“可重用”部分的类中。懒惰的人的方式是使变量public而不是private和从外部访问像GalleryClassName.someThingUsedInMultipleFunctions = "dude";。如果你愿意,你也可以查看传球参数以及获得者和制定者。

编辑

要在你的根代码使用这个类:

//Assuming you do not have an instance already on the stage: 
var gal:GalleryClassName = new GalleryClassName(); 
gal.x = 50; //Place it wherever you want 
gal.y = 50; 
addChild(gal); 

然后加载XML画廊

gal.startGallery("somepath/gallery1.xml"); 
+0

嗨ToddBFisher感谢您的快速回复:) 虽然你的答案确实帮助了我(是的,我也对此感到困惑),我不认为我的问题是明确的 基本上我的脚本创建了一个container_mc,其中包含所有的缩略图和一切。 现在,我将如何去从我的.fla使用这个类。如在,我必须创建类的对象,或者我可以像我以前那样调用startGallery()函数吗?至于传递参数,我想我可以通过一些XML细节来告诉类加载哪个画廊? – user1133065 2012-01-06 03:17:04

+0

请参阅上面的编辑。 – ToddBFisher 2012-01-06 03:24:26