转换为Actionscript 3 Class

时间:2011-04-12 23:39:11

标签: flash actionscript-3

我在各种教程中创建了一些ActionScript 3中的代码,这些教程是一个链接到源文件XML文件的简单媒体播放器。我发现虽然我需要为代码使用actionscript类,并想知道是他们将它转换为类的方法,还是有人知道在actionscript 3中创建基于类的媒体播放器的教程?我的代码如下:

import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,xmlloaded);

var xml:XML = new XML();
var amountofvid:Number=0;
var currentvideo:Number=0;

btn_prev.addEventListener (MouseEvent.CLICK, prevvid);
btn_next.addEventListener (MouseEvent.CLICK, nextvid);

loader.load(new URLRequest('videos.xml'));

function xmlloaded (e:Event) {
    xml=XML(e.target.data);
    amountofvid=xml.video.length ()-1;
    changevid();
}

function nextvid (e:Event) {
    currentvideo++;
    changevid();
}

function prevvid (e:Event) {
    currentvideo--;
    changevid();
}

function changevid():void {
    var cv:Number=Math.abs(currentvideo);

    if (cv>amountofvid) {
        currentvideo=cv=0;
    }
    if (currentvideo<0) {
        currentvideo=cv=amountofvid;
    }

    vid.source = xml.video.@src[cv];
    title.text = xml.video.@title[cv];

}

有什么想法吗?

更新,感谢帮助人们帮助加载希望我能找到你们两个已经解决的答案。

2 个答案:

答案 0 :(得分:2)

远离时间表并拥抱一些基本的OOP结构是你作为一个崭露头角的Flash开发人员,程序员或认真学生所做的最好的事情。它可以是一个深刻的主题,但你越早开始搞清楚就越好。谷歌将解释一切。

无论如何 - 您拥有将此过程写入课程所需的大部分内容。查看评论以获得简要说明:

// package encloses the class and identifies its scope
package you.com.app
{
    //imports
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.text.TextField;

    /**
     * ... declare your class, whatever it may extend and any interfaces
     */

    public class MediaPlayer extends Sprite
    {
        // variables now include an access modifier to define their scope (private, here)
        private var xml             :XML;
        private var amountofvid     :Number=0;
        private var currentvideo    :Number=0;
        private var loader          :URLLoader;
        private var vid             :MovieClip; //or video component or whatever
        private var title           :TextField;
        private var btn_prev        :SimpleButton;
        private var btn_next        :SimpleButton;
        private var currentvideo    :int;

        /**
         * constructor - must match class name. returns statement omitted
         */
        public function MediaPlayer()
        {
            // call superclass
            super();
            //initialize procedure
            init();
        }

        private function init():void
        {
           //build display list
            assembleDisplayObjects();

            //grab data
            retreiveData();

        }

        private function retreiveData():void
        {
            loader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, xmlloaded);
            loader.load(new URLRequest('videos.xml')); 
        }

        private function xmlloaded (e:Event):void  
        {
            xml = new XML();
            xml=XML(e.target.data);
            amountofvid=xml.video.length ()-1;
            changevid();

            addEventHandlers(); //when data has loaded, activate clickables
        }

        private function assembleDisplayObjects():void
        {
            // create or instantiate display objects, and into the display list
            // adjust x,y values as needed
            vid = new MovieClip();
            this.addChild(vid);

            title = new TextField();
            this.addChild(title);

            btn_next = new SimpleButton();
            this.addChild(btn_next);

            btn_prev = new SimpleButton();
            this.addChild(btn_prev);
        }

        private function addEventHandlers():void
        {
            //centralized event listener control

            btn_prev.addEventListener (MouseEvent.CLICK, prevvid);
            btn_next.addEventListener (MouseEvent.CLICK, nextvid);
        }

        private function nextvid (e:Event):void 
        {
            currentvideo++;
            changevid();
        }

        private function prevvid (e:Event):void  
        {
            currentvideo--;
            changevid();
        }

        private function changevid():void 
        {
            var cv:Number=Math.abs(currentvideo);

            if (cv>amountofvid) {
                currentvideo=cv=0;
            }
            if (currentvideo<0) {
                currentvideo=cv=amountofvid;
            }

            vid.source = xml.video.@src[cv];
            title.text = xml.video.@title[cv];
        }

    }   

}

我没有测试过它,它可能没有错误,但它是基本的包/类结构,应该让你去。

我强烈推荐Shupe / Rossers“学习动作脚本3.0”作为该主题的一个很好的介绍,并且Mook的“Essential Actionscript 3.0”作为综合参考。和谷歌。很多谷歌。

希望有所帮助。

答案 1 :(得分:0)

这里是您的代码快速转换为classe。

package  {

import flash.net.URLLoader;
import flash.events.Event;
import flash.net.URLRequest;    

public class mediaPlayer extends Sprite {

    private var loader:URLLoader;
    private var xml:XML;
    private var amountofvid:Number=0;
    private var currentvideo:Number=0;

    public function mediaPlayer() {

        loader = new URLLoader();
        loader.addEventListener(Event.COMPLETE,xmlloaded);
        btn_prev.addEventListener (MouseEvent.CLICK, prevvid);
        btn_next.addEventListener (MouseEvent.CLICK, nextvid);
        loader.load(new URLRequest('videos.xml'));

    }

    private function xmlloaded (e:Event) {
        xml = new XML();
        xml=XML(e.target.data);
        amountofvid=xml.video.length()-1;
        changevid();
    }

    public function nextvid (e:Event) {
        currentvideo++;
        changevid();
    }

    public function prevvid (e:Event) {
        currentvideo--;
        changevid();
    }

    private function changevid():void {
        var cv:Number=Math.abs(currentvideo);

        if (cv>amountofvid) {
            currentvideo=cv=0;
        }

        if (currentvideo<0) {
            currentvideo=cv=amountofvid;
        }

        vid.source = xml.video.@src[cv];
        title.text = xml.video.@title[cv];

    }

}

}

我假设它将与精灵相关联,所以我将其扩展。如果要将classe与其他内容相关联,则需要更改此行:

public class mediaPlayer extends Sprite {

我将nextvid和prevvid方法保持公开(意味着你可以从另一个级别访问它,例如父级)和你的其他方法和变量私有(只能从这个级别访问)。您可能希望根据需要进行更改。

关于类如何工作的启动教程,我会在GoToAndLearn.com上推荐这个 http://gotoandlearn.com/play.php?id=43

米。