PDA

View Full Version : An Embedding Sounds and Classes Problem


Darakan
07-03-2008, 10:30 PM
I'm kind of new to Flex, so obviously I did something wrong. I'm trying to create a Database class for my project which will contain all the sound files I need through the running.

package tpow.database {

import flash.media.Sound;
import flash.media.SoundChannel;

public class DBSounds {

[Embed(source="imported/sounds/whoosh01.mp3")]
private var buttonOver : Class;

public static function playSound(string : String):void {
switch (string) {
case "buttonOver" :
var sound:Sound = new buttonOver() as Sound;
sound.play();
break;
}
}

}
}

When I want to play a sound, I just call to the playSound function like this:
tpow.database.DBSounds.playSound("buttonOver");

But I get a compilation error at the line I marked: "1180: Call to a possibly undefined method buttonOver().".

What is wrong? Is my design for this kind of idea is generally good at all (I know the "switch" isn't the best solution but I couldn't think about a different way to access multiple sounds in the future).

Sly_cardinal
07-04-2008, 09:04 AM
Your playSound function is defined as static whilst your buttonOver variable is not.

Defining your buttonOver variable as static might fix the problem.

Darakan
07-04-2008, 09:55 AM
Your playSound function is defined as static whilst your buttonOver variable is not.

Defining your buttonOver variable as static might fix the problem.

Doh! Can't believe I haven't noticed this! Thanks a lot. Everything works perfectly.

Thought I have another question, is there a way I can send to the playSound() function a class name instead of a string (in order to prevent using switch and making the code look like crap)?

For example:

tpow.database.DBSounds.playSound(buttonOver);

and:

[Embed(source="imported/sounds/whoosh01.mp3")]
private static var buttonOver : Class;

public static function playSound(class : Class):void {
var sound:Sound = new class() as Sound;
sound.play();
}
}

I can't send a "class" to the function obviously, since the class which issues the static function doesn't know any "buttonOver" class object.

Any ideas?

Sly_cardinal
07-04-2008, 10:33 AM
I can't send a "class" to the function obviously, since the class which issues the static function doesn't know any "buttonOver" class object.


Actually, that would be my suggestion. Just make the static properties public and pass them to your playSound function.


import tpow.database.DBSounds;

DBSounds.playSound(DBSounds.buttonOver);


With your previous code you were passing in a string that matched the sound's name so you might as well just pass in the static class variable instead :)

Darakan
07-04-2008, 10:58 AM
But now I get this error:
1178: Attempted access of inaccessible property buttonOver through a reference with static type Class.


Seems like I can only get the methods from the static function to work but not the variables...

EDIT: OH bummer, they were private :P works perfectly now! Thanks a lot for the help!