» »

[Flash] Rabil bi pomoč pri pisanju knjižnjic v AS3

[Flash] Rabil bi pomoč pri pisanju knjižnjic v AS3

mloncaric ::

Zdravo.

Ker je zunaj nov ActionScript 3 sem dobil veliko vprasanj in problemov...

V AS2 sem pisal knjiznice tako, da sem direktno klical dolocene metode knjiznic. Sedaj sem se odlocil, da bom pisal kar se le da fleksibilne knjiznice, da jih bom kasneje lahko preprosteje uporabljal.

To bom dosegel z ustvarjanjem dogodkov, katere bodo ostale knjiznice kasneje poslusale...

Prilagam preprosto kodo(knjiznica za delo z XML datotekami, FLV predvajalnik in koda fla datoteke).

Rad bi dosegel fleksibilnost knjiznic zato, da jih bom kasneje lahko uporabljal na vec moznih nacinov.

Ce ima kdo kaksno idejo, kako bi se lotil te zadeve mi prosim naj pomaga.

Hvala.

LP

------------------------------------------------
XML loader class:

package com.mloncaric.as3.utils
{
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import flash.net.URLLoader;
   import flash.net.URLRequest;
   
   public class LoaderXML extends EventDispatcher
   {
      public static var OPEN:String = "onLoadStart";
      public static var COMPLETE:String = "onLoadComplete";
      
      public var data:XML;
      
   //CONSTRUCTOR
      public function LoaderXML(targetXML:String = null)
      {
         if(targetXML != null) this.loadXML(targetXML);
      }
      
   //METHOD, WHICH GETS AN XML FILE
      public function loadXML(targetXML:String):void
      {
         var loader:URLLoader = new URLLoader(new URLRequest(targetXML));
         
         loader.addEventListener(Event.OPEN, onLoadStart);
         loader.addEventListener(Event.COMPLETE, onLoadComplete);
      }
      
   //EVENT HANDLERS
      private function onLoadStart(e:Event):void
      {
         e.currentTarget.removeEventListener(e.type, arguments.callee);
         
         dispatchEvent(new Event(LoaderXML.OPEN));
      }
      
      private function onLoadComplete(e:Event):void
      {
         e.currentTarget.removeEventListener(e.type, arguments.callee);
         
         this.data = new XML(e.currentTarget.data);
         
         dispatchEvent(new Event(LoaderXML.COMPLETE));
      }
   }
}

----------------------------------------------------
My simple FLVPlayer class:

package com.mloncaric.as3.players
{
   import flash.display.Sprite;
   import flash.media.Video;
   import flash.net.NetConnection;
   import flash.net.NetStream;
   import flash.events.MouseEvent;
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import flash.events.NetStatusEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.AsyncErrorEvent;
   
   import com.mloncaric.as3.utils.LoaderXML;
   
   public class FLVPlayer extends EventDispatcher
   {
      private var lx:LoaderXML;
      private var xml:XML;
      private var nc:NetConnection;
      private var ns:NetStream;
      private var v:Video;
      private var holder:Sprite;
      
      public var autoPlay:Boolean = false;
      private var isPlaying:Boolean = false;
      private var isPaused:Boolean = false;
      
      public function FLVPlayer()
      {
         
      }
      
      public function loadXML(targetXML:String):void
      {
         this.lx = new LoaderXML(targetXML);
         
         this.lx.addEventListener(LoaderXML.OPEN, onStart);
         this.lx.addEventListener(LoaderXML.COMPLETE, onComplete);
      }
      
      public function createPlayer(array:Object):void
      {
         this.holder = array.holder;
         this.autoPlay = array.autoPlay;
         
         lx.addEventListener(LoaderXML.COMPLETE, init);
      }
      
      private function onStart(e:Event):void
      {
         this.lx.removeEventListener(e.type, arguments.callee);
      }
      
      private function onComplete(e:Event):void
      {
         this.lx.removeEventListener(e.type, arguments.callee);
         
         this.xml = new XML(e.currentTarget.data);
      }
      
      private function init(e:Event):void
      {
         this.lx.removeEventListener(e.type, arguments.callee);
         
         this.nc = new NetConnection();
         this.nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
         this.nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
         this.nc.connect(null);
      }

      private function connectStream():void
      {
         this.ns = new NetStream(this.nc);
         this.ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
         this.ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
         this.ns.client = new CustomClient();
         
         if(this.autoPlay) this.onPlay();
         
         this.v = new Video(this.holder.width, this.holder.height);
         this.v.attachNetStream(this.ns);
         
         this.holder.addChild(this.v);
      }
      
      public function onPlay(e:MouseEvent = null):void
      {
         this.ns.play(this.xml.video[0].@src.toString());
         
         this.isPlaying = true;
      }
      
      public function onPause(e:MouseEvent = null):void
      {
         this.ns.pause();
      }
      
      public function onPlayPause(e:MouseEvent = null):void
      {
         this.isPaused = !this.isPaused;
         
         if(!this.isPlaying && !this.autoPlay) this.onPlay();
         else this.ns.togglePause();
         
         e.currentTarget.gotoAndStop((this.isPaused)?"play":"pause");
      }
      
      public function onStop(e:MouseEvent = null):void
      {
         this.ns.pause();
         this.ns.seek(0);
      }
      
      public function onRewind(e:MouseEvent = null):void
      {
         this.ns.seek(0);
      }
      
      private function netStatusHandler(event:NetStatusEvent):void
      {
         switch (event.info.code)
         {
            case "NetConnection.Connect.Success":
               this.connectStream();
            break;
            
            case "NetStream.Play.StreamNotFound":
               trace("Error! File not found...");
            break;
         }
      }
      
      private function securityErrorHandler(e:SecurityErrorEvent):void
      {
         
      }
      
      private function asyncErrorHandler(event:AsyncErrorEvent):void
      {
         
      }
   }
}

class CustomClient
{
   public function onMetaData(info:Object):void
   {
      for(var i:String in info) trace(i, info[i]);
   }
   
   public function onCuePoint(info:Object):void
   {
      for(var i:String in info) trace(i, info[i]);
   }
}

----------------------------------------------------
main file:

import com.mloncaric.as3.players.FLVPlayer;

var fp:FLVPlayer = new FLVPlayer();

fp.loadXML("playlist.xml");
fp.createPlayer({holder:this, autoPlay:false});

playPauseBTN.addEventListener(MouseEvent.CLICK, fp.onPlayPause);


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

android črta

Oddelek: Programiranje
412393 (1664) g333kk
»

Vprašanje iz Jave

Oddelek: Programiranje
71138 (878) illion
»

Prosim za predloge in pripombe

Oddelek: Izdelava spletišč
322116 (1259) msjr
»

Flash CS4 password access AS 3.0

Oddelek: Programiranje
81583 (1325) 680x0
»

[Flash AS3] Povezava z MySQL

Oddelek: Programiranje
81937 (1798) 680x0

Več podobnih tem