# My First Experience with Writing OOP with AS3

**URL:** <https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809>\
**Category:** flash\
**Created:** [May 8, 2008, 9:25pm UTC](https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809 "2008-05-08T21:25:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrutter](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jrutter/32/2004_2.png) [@jrutter](https://forum.kirupa.com/u/jrutter)\
**Post date:** [May 8, 2008, 9:25pm UTC](https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809/1 "2008-05-08T21:25:43Z")

</div>

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?

```auto
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();	
	}

	}
}

```

---

<div class="post-metadata">

**Author:** ![Groady](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/groady/32/3750_2.png) [@Groady](https://forum.kirupa.com/u/Groady)\
**Post date:** [May 8, 2008, 10:29pm UTC](https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809/2 "2008-05-08T22:29:52Z")

</div>

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.

```auto
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();
        }**
	

	}
}

```

---

<div class="post-metadata">

**Author:** ![jrutter](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jrutter/32/2004_2.png) [@jrutter](https://forum.kirupa.com/u/jrutter)\
**Post date:** [May 9, 2008, 1:39pm UTC](https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809/3 "2008-05-09T13:39:21Z")

</div>

[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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Groady](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/groady/32/3750_2.png) [@Groady](https://forum.kirupa.com/u/Groady)\
**Post date:** [May 9, 2008, 11:26pm UTC](https://forum.kirupa.com/t/my-first-experience-with-writing-oop-with-as3/259809/4 "2008-05-09T23:26:11Z")

</div>

No worries. FYI, snd = new Sound() should be inside the LoadSoundClip(mp3File) method not the playSound method. My bad.
