Covert to Actionscript 3 Class
I have created some code in Actionscript 3 following various tutorials which is a simple media player linked to an XML file for the source information. I have found out though I need to use actionscript classes for the code and wondered is their a way to convert it to classes or does anyone know of a tutorial in actionscript 3 for creating a media player based on classes? My code is below:
import flash.net.URLLoader;
import flash.events.Event;
import flash.net.U开发者_StackOverflow中文版RLRequest;
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];
}
Any ideas?
Update, thanks for the help guys both helped loads wish I could pick you both a solved answers.
Getting away from the timeline and embracing some basic OOP structure is the single best thing you can do as a budding flash developer, programmer, or earnest student. Its can be a deep subject, but the sooner you get started figuring it out the better. The googles will explain everything.
At any rate - you have most of what you need to write this procedure into a class. Check out the comments for a brief explanation:
// 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];
}
}
}
I haven't tested this and it's probably not error free, but it is the basic package/class structure and should get you going.
I'd highly recommend Shupe / Rossers "Learning Actionscript 3.0" as a great intro to the subject, and Mook's "Essential Actionscript 3.0" as a comprehensive reference. And google. Lots of google.
Hope that helps.
here's a quick conversion of your code to a 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];
}
}
}
I assumed that it will be associated to a sprite so i extended it as such. If you want to associated the classe to something else, you will want to change this line :
public class mediaPlayer extends Sprite {
I kept both nextvid and prevvid methods public (meaning you can access it from another level, a parent for example) and your other methods and variables private (accessible from this level only). You might want to change that to suit your need.
For a startup tutorial on how classes works, i would suggest this one on GoToAndLearn.com http://gotoandlearn.com/play.php?id=43
m.
精彩评论