按变量名加载数据 (AS3)
Loading Data By Variable Name (AS3)
我有一个代码可以读取用户选择的语言的声音。
现在,我的代码是:
if (english == true){
helloSound = new hello_english();
}
if (french == true){
helloSound = new hello_french();
}
因为我要添加更多的单词和更多的语言,所以我想简化我的代码。
我想做什么:
应用启动时,用户选择语言。
english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
function goEnglish(event:MouseEvent):void {
var audioChosen = "english";
}
function goFrench(event:MouseEvent):void {
var audioChosen = "french";
}
然后可以做类似的事情吗?
helloSound = new hello_(audioChosen)();
所以,在这个例子中,将要播放的声音是:hello_english
使用 getDefinitionByName
要使用字符串中的 class 名称实例化一个对象,您可以使用 getDefinitionByName
,如这个问题中所述:Instantiate a class from a string in ActionScript 3
// Find the relevant class:
var helloSoundFileClass:Class = getDefinitionByName("hello_english") as Class;
// Create a new instance of the class we found:
var helloSound:Sound = new helloSoundFileClass();
用法示例
下面是一个基于原始代码的示例:
var audioChosen:String;
var helloSound:Sound;
var goodbyeSound:Sound;
english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
// Set a "default" language.
audioChosen = "english"
onLanguageChanged();
// When user chooses a language:
function goEnglish(e:MouseEvent):void {
audioChosen = "english";
onLanguageChanged();
}
function goFrench(event:MouseEvent):void {
audioChosen = "french";
onLanguageChanged();
}
function onLanguageChanged() {
// Find the relevant classes:
var helloSoundFileClass:Class = getDefinitionByName("hello_" + audioChosen) as Class;
var goodbyeSoundFileClass:Class = getDefinitionByName("goodbye_" + audioChosen) as Class;
// Create a new instance of the class we found:
helloSound = new helloSoundFileClass();
goodbyeSound = new goodbyeSoundFileClass();
}
...
// Later, we can play the sounds:
helloSound.play();
goodbyeSound.play();
只是实现预期结果的另一种方法。
- 根据相关按钮有一个中心函数
setLanguage
到check/set。
- 加载 MP3 文件而不是使用 库 中的
Sound
。
你可以试试:
//english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
//french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
var helloSound :Sound;
var audioChannel :SoundChannel = new SoundChannel();
var isAudioPlaying :Boolean = false;
var audioChosen :String = "";
english.addEventListener(MouseEvent.CLICK, setLanguage);
french.addEventListener(MouseEvent.CLICK, setLanguage);
spanish.addEventListener(MouseEvent.CLICK, setLanguage);
japanese.addEventListener(MouseEvent.CLICK, setLanguage);
greek.addEventListener(MouseEvent.CLICK, setLanguage);
function setLanguage( evt :MouseEvent ) :void
{
//trace("clicked is : " + evt.target.name ); //# get instance name of clicked: eg... "english"
var user_language = evt.target.name; //# will set to instance name of "evt" that triggered a "clicked" event
//# check instance name of clicked object
switch ( user_language )
{
case "english": trace("Hello World!"); audioChosen = "english"; break;
case "french": trace("Bonjour Monde!"); audioChosen = "french"; break;
case "spanish": trace("Hola Mundo!"); audioChosen = "spanish"; break;
case "japanese": trace("こんにちは世界!"); audioChosen = "japanese"; break;
case "greek": trace("γειά σου κόσμος!"); audioChosen = "greek"; break;
}
//# also can check with IF / ELSE version instead of the above SWITCH checking...
//if( user_language == "english" ) { trace("Hello World!"); audioChosen = "english"; }
//if( user_language == "japanese" ) { trace("こんにちは世界!"); audioChosen = "japanese"; }
//# run some other function...
do_Some_Action();
}
function do_Some_Action() :void
{
trace("User has chosen language: " + audioChosen);
trace("::::::::::::::::::::::::::::::::::::::::");
//# check if the URL is correct, example of expected: "hello_english.mp3"
//var temp :String = "hello_" + audioChosen + ".mp3"; trace("temp is: " + temp);
if ( audioChannel == null ) { audioChannel = new SoundChannel(); }
if ( isAudioPlaying == true ) { audioChannel.stop(); }
helloSound = new Sound();
helloSound.load( new URLRequest( "hello_" + audioChosen + ".mp3" ) );
audioChannel = helloSound.play();
isAudioPlaying = true;
}
我有一个代码可以读取用户选择的语言的声音。 现在,我的代码是:
if (english == true){
helloSound = new hello_english();
}
if (french == true){
helloSound = new hello_french();
}
因为我要添加更多的单词和更多的语言,所以我想简化我的代码。
我想做什么:
应用启动时,用户选择语言。
english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
function goEnglish(event:MouseEvent):void {
var audioChosen = "english";
}
function goFrench(event:MouseEvent):void {
var audioChosen = "french";
}
然后可以做类似的事情吗?
helloSound = new hello_(audioChosen)();
所以,在这个例子中,将要播放的声音是:hello_english
使用 getDefinitionByName
要使用字符串中的 class 名称实例化一个对象,您可以使用 getDefinitionByName
,如这个问题中所述:Instantiate a class from a string in ActionScript 3
// Find the relevant class:
var helloSoundFileClass:Class = getDefinitionByName("hello_english") as Class;
// Create a new instance of the class we found:
var helloSound:Sound = new helloSoundFileClass();
用法示例
下面是一个基于原始代码的示例:
var audioChosen:String;
var helloSound:Sound;
var goodbyeSound:Sound;
english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
// Set a "default" language.
audioChosen = "english"
onLanguageChanged();
// When user chooses a language:
function goEnglish(e:MouseEvent):void {
audioChosen = "english";
onLanguageChanged();
}
function goFrench(event:MouseEvent):void {
audioChosen = "french";
onLanguageChanged();
}
function onLanguageChanged() {
// Find the relevant classes:
var helloSoundFileClass:Class = getDefinitionByName("hello_" + audioChosen) as Class;
var goodbyeSoundFileClass:Class = getDefinitionByName("goodbye_" + audioChosen) as Class;
// Create a new instance of the class we found:
helloSound = new helloSoundFileClass();
goodbyeSound = new goodbyeSoundFileClass();
}
...
// Later, we can play the sounds:
helloSound.play();
goodbyeSound.play();
只是实现预期结果的另一种方法。
- 根据相关按钮有一个中心函数
setLanguage
到check/set。 - 加载 MP3 文件而不是使用 库 中的
Sound
。
你可以试试:
//english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
//french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);
var helloSound :Sound;
var audioChannel :SoundChannel = new SoundChannel();
var isAudioPlaying :Boolean = false;
var audioChosen :String = "";
english.addEventListener(MouseEvent.CLICK, setLanguage);
french.addEventListener(MouseEvent.CLICK, setLanguage);
spanish.addEventListener(MouseEvent.CLICK, setLanguage);
japanese.addEventListener(MouseEvent.CLICK, setLanguage);
greek.addEventListener(MouseEvent.CLICK, setLanguage);
function setLanguage( evt :MouseEvent ) :void
{
//trace("clicked is : " + evt.target.name ); //# get instance name of clicked: eg... "english"
var user_language = evt.target.name; //# will set to instance name of "evt" that triggered a "clicked" event
//# check instance name of clicked object
switch ( user_language )
{
case "english": trace("Hello World!"); audioChosen = "english"; break;
case "french": trace("Bonjour Monde!"); audioChosen = "french"; break;
case "spanish": trace("Hola Mundo!"); audioChosen = "spanish"; break;
case "japanese": trace("こんにちは世界!"); audioChosen = "japanese"; break;
case "greek": trace("γειά σου κόσμος!"); audioChosen = "greek"; break;
}
//# also can check with IF / ELSE version instead of the above SWITCH checking...
//if( user_language == "english" ) { trace("Hello World!"); audioChosen = "english"; }
//if( user_language == "japanese" ) { trace("こんにちは世界!"); audioChosen = "japanese"; }
//# run some other function...
do_Some_Action();
}
function do_Some_Action() :void
{
trace("User has chosen language: " + audioChosen);
trace("::::::::::::::::::::::::::::::::::::::::");
//# check if the URL is correct, example of expected: "hello_english.mp3"
//var temp :String = "hello_" + audioChosen + ".mp3"; trace("temp is: " + temp);
if ( audioChannel == null ) { audioChannel = new SoundChannel(); }
if ( isAudioPlaying == true ) { audioChannel.stop(); }
helloSound = new Sound();
helloSound.load( new URLRequest( "hello_" + audioChosen + ".mp3" ) );
audioChannel = helloSound.play();
isAudioPlaying = true;
}