So Ive been reading Object Oriented Programming for AS3, and it makes sense. I wanted to try a script, as I should really apply what I have been learning. My script doesnt work, but I wanted to post it on here and see if I could get some feedback. I wanted to build a Mp3 player, very basic - one I could pull into my flash file and instantiate the class with an mp3 that loads in from the .fla. Here is my code, am I off to a good start?
package {
import.flash.media.*;
import flash.net.URLRequest;
public class SoundPlayer extends Sprite {
public var channel:SoundChannel;
public var mp3File:URLRequest;
private var snd:Sound = new Sound();
playBtn.addEventListener(MouseEvent.CLICK, SoundPlayer);
public function LoadSoundClip(mp3File) {
snd.load(new URLRequest(mp3File));
}
public function SoundPlayer() {
channel = sound.play();
}
}
}
Couple of things (in bold). Note the SoundPlayer method should be the first method after the class definition as it is the constructor method, this is just good practice. Also you should only be instantiating class’ within methods or the constructor.
package {
import.flash.media.*;
**import flash.display.Sprite;**
import flash.net.URLRequest;
public class SoundPlayer extends Sprite {
public var channel:SoundChannel;
public var mp3File:URLRequest;
private var snd:Sound;
public function SoundPlayer() {
**playBtn.addEventListener(MouseEvent.CLICK, SoundPlayer);**
}
public function LoadSoundClip(mp3File) {
snd.load(new URLRequest(mp3File));
** snd.addEventListener... // Listen for a "load complete" event on snd then fire playSound().**
}
** private function playSound(e:Event):void {
snd = new Sound();
channel = snd.play();
}**
}
}
[QUOTE=Groady;2323864]Couple of things (in bold). Note the SoundPlayer method should be the first method after the class definition as it is the constructor method, this is just good practice. Also you should only be instantiating class’ within methods or the constructor.
package {
import.flash.media.*;
**import flash.display.Sprite;**
import flash.net.URLRequest;
public class SoundPlayer extends Sprite {
public var channel:SoundChannel;
public var mp3File:URLRequest;
private var snd:Sound;
public function SoundPlayer() {
**playBtn.addEventListener(MouseEvent.CLICK, SoundPlayer);**
}
public function LoadSoundClip(mp3File) {
snd.load(new URLRequest(mp3File));
** snd.addEventListener... // Listen for a "load complete" event on snd then fire playSound().**
}
** private function playSound(e:Event):void {
snd = new Sound();
channel = snd.play();
}**
}
}
[/QUOTE]
Ok great thanks! Its nice to be able to post this stuff for a code review.
No worries. FYI, snd = new Sound() should be inside the LoadSoundClip(mp3File) method not the playSound method. My bad.