我是新來的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
}
}
你提的問題是非常廣闊的。你認爲你可以把它分解成一些關於你遇到的問題的更小的具體問題嗎? – Cadin 2012-01-05 22:41:07