PDA

View Full Version : Using value variables throw public functions


LuisPedro
01-19-2008, 10:43 AM
He all!!

I'm trying to use value variables throw public functions in the same class.
Is there any possible to do it?

I'll explain:

Imagine we have a class name Mp3Player that plays some mp3 file and get is ID3 songName:
In that class we have the package:
package Mp3Player{

import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.media.SoundChannel;
import flash.media.ID3Info;
import flash.net.URLRequest;
import flash.events.Event;

public class Mp3Player {

private var sound:Sound;
private var req:URLRequest;
private var context:SoundLoaderContext;
private var channel:SoundChannel;
private var id3:ID3Info;
private var nomeMusica:String;
Then we have the public class to load and play the mp3 file passed using argument "file":
public function tocarMusica(file) {
sound=new Sound ;
req=new URLRequest(file);
context=new SoundLoaderContext(8000,true);
sound.load(req,context);
channel=sound.play();
sound.addEventListener(Event.ID3,setNome);
}
After we load and start play the mp3 file, we whant to get the ID3 songName and save it in the "nomeMusica" variable:
public function setNome(event:Event) {
id3=event.target.id3;
nomeMusica=id3.songName;
trace("Nome: "+nomeMusica);
}

Now the problem is...How can i trace(nomeMusica) outside the "public function setNome(event:Event)"?

Or in other words:
In the FLA file i can call to play the mp3 file like this:
var musica = new Mp3Player();
musica.tocarMusica("song.mp3");
And how can i call the ID3 songName using sometinh like:
musica.getName();

Sorry my english and i hope you can help me in a simple way.

:)

neznein9
01-19-2008, 02:29 PM
You need getters and setters (also called accessor functions).


public function get nome():String{
return nomeMusica;
}


The getter is then used just like a read-only property of the object...so outside you can use myTextField.text = musica.nome;

It doesnt really make sense for you right now but just so you know, you can pair a getter with a 'setter' like so:

public function set nome(s:String):void{
nomeMusica = s;
}


That way you could:
musica.nome = 'abc';
trace(musica.nome); // abc

LuisPedro
01-19-2008, 08:17 PM
Tnks 4 the answer.

Is this your sugestion?


//AS Code
package Mp3Player{

import flash.media.Sound;
import flash.media.SoundLoaderContext;
import flash.media.SoundChannel;
import flash.media.ID3Info;
import flash.net.URLRequest;
import flash.events.Event;

public class Mp3Player {

private var sound:Sound;
private var req:URLRequest;
private var context:SoundLoaderContext;
private var channel:SoundChannel;
private var id3:ID3Info;
private var nomeMusica:String;

public function tocarMusica(file) {
sound=new Sound ;
req=new URLRequest(file);
context=new SoundLoaderContext(8000,true);
sound.load(req,context);
channel=sound.play();
trace("A tocar");
sound.addEventListener(Event.ID3,id3Info);
}

public function id3Info(event:Event):void {
id3=event.target.id3;
nomeMusica=id3.songName;
}
public function get getNome():String {
return nomeMusica;
}
}

//FLA Code
import Mp3Player.Mp3Player;

var musica:Mp3Player = new Mp3Player();
var nome:String = musica.getNome;

musica.tocarMusica("Saia_Indiscreta.mp3");
trace(nome);
}

The trace result is not what i espected... :confused:
A tocar
Null

Why NULL ??????? :eek:

What is wrong or missing???

Anyone?

LuisPedro
01-21-2008, 08:18 AM
Does anyone can help me on this?

I need only to pass the value of the variable nomeMusica on the event function setNome(event:Event) in the class file (AS)to trace it in the FLA file.

I'm stick here...please, anyone??

panel
01-21-2008, 09:35 AM
It is null becouse you accessing variale befode ID's have been laoded.

Besr best way to make ti work would be do dispatch custom event and the lisnten for this event in fla file.
To do this your class have to extend EventDispatcher (or one of it's sublasses like MovieClip or Sprite)

your class

package mp3Player{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.media.ID3Info;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;

public class Mp3Player extends EventDispatcher{

private var sound:Sound;
private var req:URLRequest;
private var context:SoundLoaderContext;
private var channel:SoundChannel;
private var id3:ID3Info;
private var nomeMusica:String;

public function tocarMusica(file) {
sound=new Sound ;
req=new URLRequest(file);
context=new SoundLoaderContext(8000,true);
sound.load(req,context);
channel=sound.play();
trace("A tocar");
sound.addEventListener(Event.ID3,id3Info);
}

public function id3Info(event:Event):void {
id3=event.target.id3;
nomeMusica=id3.songName;
dispatchEvent(new Mp3PlayerEvent(Mp3PlayerEvent.ID3_INITIALIZED));
}
public function get getNome():String {
return nomeMusica;
}
}



Custom Event Class

package mp3Player
{
import flash.events.Event;

public class Mp3PlayerEvent extends Event
{
public static const ID3_INITIALIZED:String = "id3Initialized";

public function Mp3PlayerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}

}
}


Fla

import Mp3Player.Mp3Player;

var musica:Mp3Player = new Mp3Player();
musica.addEventListener(Mp3PlayerEvent.ID3_INITIAL IZED, onId3Init);

function onId3Init(evt:Mp3PlayerEvent):void
{
trace(musica.getNome);
}

musica.tocarMusica("Saia_Indiscreta.mp3");

}


BTW
it is good practice to name packages starting with lowercase and cloass names with Uppercase

LuisPedro
01-22-2008, 11:54 AM
Sorry my question, but can you explain what a "dispatch custom event" do?
How it works in my project?

panel
01-22-2008, 02:13 PM
It just dispatches event. In this case it is dispatched when value of nomeMusica is konown.

Now you can listen for this event on Mp3Player, so when value of nomeMusica will be known you will be informed