PDA

View Full Version : AS3: reusable blocks or actual classes?


felisan
04-08-2008, 01:14 PM
hi everybody.


for a long time, I've thought about using classes instead of writing my actionscript in the first frame of my .fla..

in example; recently. I've had to set up .flv-files to be shown on the internet, and this task is now kinda repetitive.

what I've done so far, is that I've copied these lines into the first frame of my new .fla-file:

var video:Video = new Video(640, 480);
addChild(video);
var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);
var videoStream:NetStream = new NetStream(videoConnection);
var meta:Object = new Object();
meta.onMetaData = function(meta:Object){
trace(meta.duration);
}
videoStream.client = meta;
video.attachNetStream(videoStream);
this.setChildIndex(video, numChildren - 3);

pause_mc.addEventListener(MouseEvent.CLICK, pauseMe);
play_mc.addEventListener(MouseEvent.CLICK, resumeMe);
pause_mc.buttonMode = true;
play_mc.buttonMode = true;

function pauseMe(e:Event):void{
videoStream.pause();
}
function resumeMe(e:Event):void{
videoStream.resume();
}

videoStream.play("lilly.flv");


fairly nice and not overwhelming... (I think).


but still I have an idea, that I can optimize my work, by making classes of it. so I've tried to make the script above into classes (and man, it took me a long time!)

- .fla-file with DocumentClass: FLVPlayer

FLVPlayer.as:

package {

import CustomFLVPlayer;
import flash.display.Sprite;

public class FLVPlayer extends Sprite {

private var _customFLVPlayer:CustomFLVPlayer;

public function FLVPlayer() {
init();
}

private function init():void {
customFLVPlayerInit();
trace("Class FLVPlayer");
}

private function customFLVPlayerInit():void {
_customFLVPlayer = new CustomFLVPlayer();
addChild(_customFLVPlayer);
}
}
}


CustomFLVPlayer.as:

package{

import flash.display.Sprite;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.Event;
import flash.events.MouseEvent;

public class CustomFLVPlayer extends Sprite {
private var _video:Video;
private var _videoConnection:NetConnection;
private var _videoStream:NetStream;
private var _meta:Object;
private var _play:Play;
private var _pause:Pause;

public function CustomFLVPlayer() {
init();
}

private function init():void {
trace("Class CustomFLVPlayer");
customFLVPlayerInit();
customFLVPlayerControlsInit();
}

private function customFLVPlayerInit():void {
_video = new Video(640, 480);
addChild(_video);
_videoConnection = new NetConnection();
_videoConnection.connect(null);
_videoStream = new NetStream(_videoConnection);
_meta = new Object();
_meta.onMetaData = function(_meta:Object){
trace(_meta.duration);
}
_videoStream.client = _meta;
_video.attachNetStream(_videoStream);
_videoStream.play("lilly.flv");
}

private function customFLVPlayerControlsInit():void {
_play = new Play();
_pause = new Pause();
addChild(_play);
addChild(_pause);
_play.x = 20;
_pause.x = 35;
_play.y = 10;
_pause.y = 10;
_play.buttonMode = true;
_pause.buttonMode = true;
_pause.addEventListener(MouseEvent.CLICK, pauseMe);
_play.addEventListener(MouseEvent.CLICK, resumeMe);
}

private function pauseMe(e:Event){
_videoStream.pause();
}

private function resumeMe(e:Event){
_videoStream.resume();
}
}
}



(the Play and Pause are movieclips, I add into the Library of the new .fla-file)

at first, my way of copying the code into the new project still seems as the easiest way to do it.
but at the same time, I'm aware, that the way my classes are made, are not the proper or right way to make them.

so, questions that could help me on my way, by being answered, are:

- can I improve the recent class-setup?
- should I split my classes into more classes?
- how would these classes easily be used in future projects?

other comments or answers to answers to questions like the ones above, are very welcome.

and I hope, that it might help me getting the idea of classes :)


thanks
felisan

sleekdigital
04-08-2008, 03:21 PM
All you have done here is put the code into classes. But these classes give you no benefit, in part because you have hard coded everything. A first step in the right direction might be to give your class a public "play" function (method) where you can pass in the video you want to play.

Think about the bits of code that you would be able to re-use for most any situation where you need to play a video, that functionality would be directly coded in your classes. Then consider all the things that are specific to a given use case of a player (path/name of video, position of player and controls) and make those things dynamic using method parameters and public properties.

This book might be helpful...
http://www.amazon.com/Object-Oriented-Thought-Process-Developers-Library/dp/0672326116/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1207667700&sr=8-1