Using value of variable as variable name for urlVariable in actionscript - actionscript-3

I have the next code
var uv:URLVariables = new URLVariables();
//lot of them in an array
var variable:String = "mode";
var value:String = "easy";
uv.<variable_value> = <value_value>;
It's like using the value of variable as the variable name. How can I achieve this?
Edit: The next code represents what I need as result
uv.mode = 'easy';
uv.category = 256;
..
..
Where the variables mode and category are values of other variables.
Using php would be something like this:
$next = 10;
$var = 'next';
echo ${$var}; //10
Thanks.

I'm not quite sure what you want ( because of the question formulation )
but i'm using URLVariables like this:
import flash.net.URLVariables;
import flash.net.URLLoader;
var urlVariables:URLVariables = new URLVariables ();
// setting the values with names
urlVariables[value_name_1] = value_1; // value_name_* is a string, value_* is anything you need, Number, String, Array, Object etc...
urlVariables[value_name_2] = value_2;
urlVariables[value_name_3] = value_3;
// and now for the loading
var loader:URLLoader = new URLLoader ();
loader.data = urlVariables;

Related

How to show my Array in Text without Repetition using AS3?

var arr:Array = new Array("A","B","C")
//random number
var rand:Number = Math.floor(Math.random()*arr.length)
//my text
t1.text = arr[rand]
t2.text = arr[rand]
t3.text = arr[rand]
Something like
private function getRandomText():String
{
var rand:Number = Math.floor(Math.random() * arr.length);
// this will both get you the random string from the array
// and remove it from the array making sure you won't get the same text next time
var randomString:String = arr.splice(rand, 1);
return randomString;
}
t1.text = getRandomText();
t2.text = getRandomText();
t3.text = getRandomText();
Naturally, this will modify the array by removing the displayed string. So if you need to keep the array for the future use you'd need to make a copy of that and use the copy
At my opinion, you may use this kind of function.
This was quick done but I think that avoid to check everytime in a do while loop (useless and slower).
So you may easily change the code as you need it...
var choices:Vector.<String> = new <String>["A","B","C","D","E","F"];
var randomChoices:Vector.<String> = new Vector.<String>();
var choicesBackup:Vector.<String>;
function populateLetters():void{
var n: uint = Math.floor(Math.random()*choices.length);
randomChoices.push(choices[n]);
choices.splice(n,1);
}
function getDifferentLetters():Vector.<String>{
choicesBackup = choices.slice();
randomChoices = new Vector.<String>();
for(var i:uint=choices.length; i>0; i--){
populateLetters();
}
choices = choicesBackup.slice();
return randomChoices;
}
trace ("letters = " + choices + ", flush = " + getDifferentLetters());
// output : letters = A,B,C,D,E,F, flush = D,E,B,C,F,A
If I missed something, just let me know!
T1.text = getDifferentLetters();
etc...
Example :
var t1:TextField = new TextField();
addChild(t1);
t1.text = getDifferentLetters().toString();
t1.x = 100;
t1.y = 100;
var t2:TextField = new TextField();
addChild(t2);
t2.text = getDifferentLetters().toString();
t2.x = 100;
t2.y = 150;
var t3:TextField = new TextField();
addChild(t3);
t3.text = getDifferentLetters().toString();
t3.x = 100;
t3.y = 200;
This should work if you have a reference to your variable T1.
In your code try to use "Lowercase" for your variables and methods.
"Uppercase" for the first letter of classes and all in "uppercase" if you use a constant.
If you use only Strings in an Array, use Vector.<String> instead of Array!
trace was just an example to get the result in the output.
Best regards.
Nicolas.

Why does (myMC.myArray = myOtherMC.myOtherArray;) cause any changes to myMC.myArray to also change myOtherMC.myOtherArray?

var myArray:Array = new Array();
var myMC:MovieClip = new MovieClip();
myMC.myArray = myArray;
trace(myMC.myArray[10]); //Output: undefined
var newMC:MovieClip = new MovieClip();
newMC.myOtherArray = myMC.myArray;
newMC.myOtherArray[10] = [];
newMC.myOtherArray[10][0] = 100;
trace(myMC.myArray[10]); //Output: 100
Why does that happen, and is there any way to avoid it?
EDIT:
Found a function that can clone associative arrays here.
Here is the function (from the above link):
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
Does making the function return type "*" mean that it can be any type? or is it something specific to objects/arrays?
It's because you have passed a reference of the original array to the new clip. Arrays, as objects, are not primitives and therefore will always be referenced.
If you would like to clone an array, keeping the original intact, use this method:
var b:Array = a.concat();
b will be a new array. a can modified without changing b.

Loading more than one child image AS3

I'm trying to load 2 images in to my flash file but getting an error - new to this AS3 malarkey any help would be appreciated!
Getting error: 5 1151: A conflict exists with definition request in namespace internal.
var myImage:String = dynamicContent.Profile[0].propImage.Url;
var myImage2:String = dynamicContent.Profile[0].prop2Image.Url;
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
var myImageLoader2:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(enabler.getUrl(myImage2));
myImageLoader.load(request);
myImageLoader.x =17;
myImageLoader.y = 0;
if(this.currentFrame == 1) {
addChildAt(myImageLoader, 2);
}
if(this.currentFrame == 2) {
addChildAt(myImageLoader2, 2);
}
It's usually a good idea to move duplicate code into its own function instead of doing copy + paste:
function loadImage(file:String, x:Number, y:Number):StudioLoader{
var myImageLoader:StudioLoader = new StudioLoader();
var request:URLRequest = new URLRequest(file);
myImageLoader.load(request);
myImageLoader.x = x;
myImageLoader.y = y;
return myImageLoader;
}
addChild(loadImage(enabler.getUrl(myImage1),17,0));
addChild(loadImage(enabler.getUrl(myImage2),17,0));
That's not just giving you better structured code but also fixes your duplicate variable definition issue because what's defined locally inside a function stays inside the function.
This might provide some insight:
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html
You can't create a new variable with the same name as an existing one, in your case I'm speaking about your URLRequest request, so to avoid this type of error you can do like this :
var myImageLoader2:StudioLoader = new StudioLoader();
// assing a new URLRequest to an existing var
request = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Or :
var myImageLoader2:StudioLoader = new StudioLoader();
// create a new URLRequest var, so the name should be different
var request2:URLRequest = new URLRequest(enabler.getUrl(myImage2));
// here you want use the myImageLoader2, not myImageLoader
myImageLoader2.load(request2);
myImageLoader2.x =17;
myImageLoader2.y = 0;
Additionally, I notice in the second block that you declare myImageLoader2 but you then use the original myImageLoader to do the load request. So even if you do declare a new URLRequest, you wont get both images loaded.
Akmozo's solution has this corrected.

Locale.loadLanguageXML() Won't work in external script

I have a script for multi-language-use working when added to the first frame of a timeline. However, when I try to adapt it to work in an external .as script, it throws a TypeError and I can't figure out why.
Here is the code that works in a timeline:
import fl.data.DataProvider;
import fl.text.TLFTextField;
import flash.text.Font;
import flashx.textLayout.elements.*;
import flashx.textLayout.formats.*;
//------------------
// CREATE TLFTEXTFIELD:
var field_001:TLFTextField = new TLFTextField();
field_001.x = 20;
field_001.y = 50;
field_001.width = 342
field_001.height = 54;
field_001.background = true;
addChild(field_001);
// Create text format
var format:TextLayoutFormat = new TextLayoutFormat();
format.fontFamily = "Arial";
format.fontSize = 36;
format.color = 0x666666;
// Apply the format
var textFlow:TextFlow = field_001.textFlow;
textFlow.hostFormat = format;
//------------------
// SETUP LOCALE OBJECT:
var languages:Object = new Object(); // Stores flags for loaded languages
var localeDefault:String = "ar"; // Default language
var locale:String = "ar"; // Current language selected in combobox
// Event handler for Locale object
function localeLoadedHandler(success:Boolean):void
{
if( success )
{
// Mark language as loaded and show localized string
languages[locale] = true;
field_001.text = Locale.loadStringEx("IDS_FIRSTFIELD", locale);
// field_002 is a field already on stage
field_002.text = Locale.loadStringEx("IDS_SECONDFIELD", locale);
}
}
// Load the default language...
Locale.setDefaultLang(localeDefault);
Locale.setLoadCallback(localeLoadedHandler);
trace("Locale.getDefaultLang() is: " + Locale.getDefaultLang());
Locale.loadLanguageXML(Locale.getDefaultLang());
Here is my adaptation to an external script and set up as the class for a standalone swf called "tempchild.swf" I want to open inside a parent swf at a later time:
package com.marsinc {
import fl.text.TLFTextField;
import flash.text.Font;
import flashx.textLayout.elements.*;
import flashx.textLayout.formats.*;
import flash.display.MovieClip;
import fl.lang.Locale;
public class tempchild extends MovieClip {
//------------------
// SETUP LOCALE OBJECT:
private var languages:Object; // Stores flags for loaded languages
private var localeDefault:String; // Default language
private var locale:String; // Current language selected in combobox
private var field_001:TLFTextField;
private var format:TextLayoutFormat;
public function tempchild()
{
// constructor code
languages = new Object();
localeDefault = "es";
locale = "es";
//------------------
// CREATE TLFTEXTFIELD:
field_001 = new TLFTextField();
field_001.x = 20;
field_001.y = 50;
field_001.width = 342
field_001.height = 54;
field_001.background = true;
addChild(field_001);
// Create text format
format = new TextLayoutFormat();
format.fontFamily = "Arial";
format.fontSize = 36;
format.color = 0x666666;
// Apply the format
var textFlow:TextFlow = field_001.textFlow;
textFlow.hostFormat = format;
// Load the default language...
Locale.setDefaultLang(localeDefault);
Locale.setLoadCallback(localeLoadedHandler);
trace("Locale.getDefaultLang() is: " + Locale.getDefaultLang()); // displays "es"
Locale.loadLanguageXML(Locale.getDefaultLang()); // this line returns an error
}
// Event handler for Locale object
private function localeLoadedHandler(success:Boolean):void
{
trace("running the loaded handler");
if( success )
{
// Mark language as loaded and show localized string
languages[locale] = true;
field_001.text = Locale.loadStringEx("IDS_FIRSTFIELD", locale);
//field_002.text = Locale.loadStringEx("IDS_SECONDFIELD", locale);
}
}
}
And this is the error in the output window:
TypeError: Error #1010: A term is undefined and has no properties.
at fl.lang::Locale$/loadXML()
at fl.lang::Locale$/loadLanguageXML()
at com.marsinc::tempchild()
Been digging around for an answer for a couple days now and I am stuck. Any help is greatly appreciated. Thanks!
--Kevin
You can make it .as file in one of (at least) two ways
1) copy paste all exactly as it is to .as file and do:
include "[path]filename.as"
2) change the code to be a class
- make field_001, format, textFlow, languages, localeDefault, locale as public vars
- insert all code in a function named "init"
- add the "localeLoadedHandler" as a function
- click on your stage and change in the properties panel the stage's class to the new class
Good Luck!!

Tracing MovieClips Name

I am creating some MovieClips like so:
var AClip:A = new A();
var A2Clip:A2 = new A2();
var A3Clip:A3 = new A3();
I then put the above into an array and am trying to trace out thier "name".
for(var i:int=0;i<theArray.length;i++){
trace(theArray[i].name);
}
This traces
instance99
instance77
instanceN...
What I want to accomplish is the trace is tracing out what I "initialized" them to
AClip
A2Clip
A3Clip
Is there a way to do that?
Thanks
Variable's name is not the same as MovieClip's name. You have to set its name manualy:
var AClip:A = new A();
var A2Clip:A2 = new A2();
var A3Clip:A3 = new A3();
AClip.name = "AClip";
A2Clip.name = "A2Clip";
A3Clip.name = "A3Clip";
Then you can get their name by calling trace(theArray[i].name);