As3创建和使用外部类

时间:2012-01-05 21:31:35

标签: xml flash actionscript-3 class

我是flash,as3和这个论坛的新手,所以任何帮助都会很棒!

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

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

当用户点击图库时,系统会调用 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 = myXml.@FIRSTWIDTH;
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 myXml.IMAGE.@THUMB){
    _array[i++] = path;
}

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

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].@FULL;
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
}
}

1 个答案:

答案 0 :(得分:0)

要让您的库中的项目由外部.as文件支持,首先要创建一个类文件,通常包括:

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);
      ...
    }
  }
}

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

如果您想创建一个可重用的类,您将确定3个库之间的变量会有什么不同,并将它们存储在“可重用”部分的类中。懒人的方法是使变量public而不是private,并从类外GalleryClassName.someThingUsedInMultipleFunctions = "dude";访问。如果你愿意,你也可以查看传递参数以及getter和setter。

修改

在.fla代码中使用此类:

//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");