2012-01-05 78 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