Is is possible to pass a string object inside another object? - actionscript-3

I have a string and i would like to pass it by reference in a object.
var str:String = '';
var object:Object = {s:str};
object.s = 'test';
trace(str); // i would like this to output test but it is just blank
trace(object.s); //this has a value stored in it
Is this possible or am i thinking about this wrong?
What i am trying to do is update the str value by updating object.s. I want the sting object to be inside the object not the value of the str.
I want it to do something more like this
var obj2:Object = new Object();
obj2.txt = '';
var object:Object = {s:obj2};
object.s.txt = 'test';
trace(obj2.txt); // outputs test
trace(object.s.txt); //outputs test

Of course it is possible. In the above example i suspect the trace works fine, but since there is no value, it does not display anything. I don't know actionscript very well, but since it compiles down to javascript then all objects are passed by reference. your example code actually has two objects in it one called string that is a string with no value assigned, and the other an object with a property called s that is a string. that it why there is no output.
var string:String = '';
var object:Object = {s:string};
object.s = 'test';
string = object.s;
trace(string); // i would like this to output test but it is just blank
trace(object.s);
This should show you what you want to see, and is passed by reference, unless actionscript enforces pass by value, which i do not believe it does

Related

AS3 Referencing an object or variable via string etc

I have found answers 'similar' to the one I'm looking for. I really hope I didn't overlook an already answered problem.
code:
var Randy:Object = {age:32, gender:1};
var Joey:Object = {age:35, gender:1};
var slot_0 = Randy;
var slot_1 = Joey;
myFunction();
function myFunction():void{
for(var i = 0; i < 2; i++){
var thisObject = ("Slot_" + i);
trace(thisObject); // example 1
trace(thisObject.age); //example 2
}
}
it will trace in //example 1
slot_0
slot_1
*if I 'trace(thisObject)' the 'name of the Objects' ("slot_0" ; "slot_1") trace out.*
but in //example 2 I get:
Error #1069: Property age not found on String and there is no default value.
*How do I get it to understand that I want it to reference the properties of the object itself? e.g. 'trace(thisObject.age) means slot_0.age witch means Randy.age etc...*
Without a for-loop, I have to write a lot of redundant script, so I need to know this!
Thank You in advance for the help!
var thisObject = this["slot_"+i];
That's how you can do a string reference. Of course, if the var is located elsewhere, use the proper parent object.

Return JSON value from a function

I am able to pass a JSON string to my function but can't return a value back.
var json = {"First":"ABC", "Middle":"DEF", "Last":"GHI"};
allFunction6(json);
alert(first); //this does not work
function allFunction6(json) {
var first = json.First;
alert(first); //this alerts "ABC"
return first;
}
Does the variable not retain its value outside of the function, or am I missing something?
Thanks.
Nope, the variable does not retain its value outside of the function, because that is where it is scoped to, the function.
You need to save the returned value.
var storedReturnValue = allFunction6(json);

How to use a variable as part of an URL

I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.

How to assign URLVariables result to a String Variable?

In the following example (yes, I am coding on my timeline while I try to work this out - I know, I know) I am loading an SWF in an HTML page and then directing the SWF to get the query parameters from the current URL. The query parameter will contain the source for the video to play.
This seems straight forward to me but I cannot get myURL = urlVars.videoloc; to work. More specifically, urlVars.videoloc seems to be undefined rather than holding the query parameter from the URL. All other variables are correct; both wholeURL and urlVars are defined.
//Initialize Global Event Listener
player.addEventListener(Event.ADDED_TO_STAGE, getPlay, false, 0, true);
//Function to play the video
function getPlay(e:Event):void {
var wholeURL:String = ExternalInterface.call("window.location.search.toString");
var urlVars:URLVariables = new URLVariables(wholeURL);
var myURL:String = urlVars.videoloc; //<--- Trouble, returning 'undefined'
errorBox.text = "videoloc="+urlVars.videoloc+"\nwholeURL="+wholeURL+"\nurlVars="+urlVars+"\nmyURL="+myURL; //<--- The reason I know it is returning 'undefined'
if (myURL) {
player.load(myURL);
player.play();
}
}
Ideally you should use a debugger to inspect the makeup of your URLVariables object.
If you're unable to do things the easy way, you could do this to trace its contents:
for (var parameter:String in urlVars) {
trace(parameter + "=" + urlVars[parameter]);
}
As you can see, you can step through every parameter inside urlVars using a for in loop.
I'm guessing videoLoc is your first parameter? Look at the results of this test of mine:
var address:String = "http://www.google.ca/search?q=test&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a";
var urlVars:URLVariables = new URLVariables(address);
for (var parameter:String in urlVars) {
trace(parameter + "=" + urlVars[parameter]);
}
The output of this is:
aq=t
rls=org.mozilla:en-GB:official
client=firefox-a
http://www.google.ca/search?q=test
ie=utf-8
oe=utf-8
See what happened to the q parameter? To fix this, use only the text past the ?
var address:String = "http://www.google.ca/search?q=test&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a";
var urlVars:URLVariables
= new URLVariables(address.substr(address.indexOf('?')+1));
for (var parameter:String in urlVars) {
trace(parameter + "=" + urlVars[parameter]);
}

How to send array through HTTPservice in Adobe Flex 3

How to send array in Httpservice in Adobe Flex3
I am not quite sure what you mean by sending an array to a httpservice. If you mean to send an array to a httpservice with the same field name, you can pass an array as field value.
var service:HTTPService = new HTTPService();
service.useProxy = true;
service.destination = "myservicet";
service.resultFormat = HTTPService.RESULT_FORMAT_XML;
var fields:Array = ["categories", "organisation"];
var params:Object = new Object();
params.q = "stackoverflow";
params.rows = 0;
params.facet = "true";
params["facet.field"] = fields;
service.send(params);
The HTTPService will convert this to the url parameters:
facet=true&q=stackoverflow&facet%2Efield=categories&facet%2Efield=organisation&rows=0
Hope this helps!
Added for more clarity. When there is only 1 argument in the array, do not pass the fields as an array. For some reason, flex will not send this to the http service
It really depends what is the back end technology you're using. If you're sending it to PHP you could try:
var fields:Array = ["categories", "organisation"];
var params:Object = {};
params.q = "stackoverflow";
params.rows = 0;
params.facet = "true";
params["facet.field[]"] = fields;
service.send(params);
PHP will generate an array for you.
AFAIR this works fine in Rails as well.
if it is a simple string array, you can join it with a well know separator char, and on the other site, split the string with the same separator back to an array.
If it is a simple array, you could send it as a comma separated string.
httpService.request = new Object;
httpService.request.csv = array.toString();