AS3 Referencing an object or variable via string etc - actionscript-3

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.

Related

Error: PKCS#5:unpad: Invalid padding value. expected [154], found [253] using as3crypto

I downloaded as3crypto and have been trying to integrate it into my project.
I'm trying to do a simple test to match the decrypt results I'm getting in the demo - http://crypto.hurlant.com/demo/ but I'm getting the above error.
I'm working on the Secret Key tab. I'm using AES, CBC, PKCS5 and prepending the IV.
Both the Key and Cipher text is set to HEX.
I first did an Encrypt and then copy the key and the cipher text to my function to test the decrypt to see if it match. I copied the code direct from the SecretTab.mxml and modified it a little to take the hard coded values.
I wrote a small program in C# to decrypt it using the same values and it works fine.
I verified the key and cipher text many times and it is correct.
public static function decrypt2():void
{
// 2: get a key
var k:String = Hex.fromString("e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c");
var kdata:ByteArray = Hex.toArray(k);
//trace(String.fromCharCode(kdata[0]));
// 3: get an output
var txt:String = Hex.fromString("691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a");
var data:ByteArray = Hex.toArray(txt);
// 1: get an algorithm..
var name:String = "simple-aes-cbc";
var pad:IPad = new PKCS5; //:new NullPad;
//var pad:IPad = new NullPad();
var mode:ICipher = Crypto.getCipher(name, kdata, pad);
pad.setBlockSize(mode.getBlockSize());
// if an IV is there, set it.
if (mode is IVMode) {
trace("mode is IVMode");
var ivmode:IVMode = mode as IVMode;
//ivmode.IV = Hex.toArray(iv.text);
}
mode.decrypt(data);
trace(Hex.fromArray(data));
}
You need to explicitly set both byte arrays, the data and the IV, before decrypting. I don't think as3crypto will break it out for you, even if the simple flag is used on the name.
I don't know what your example states, however the error indicates that the byte array is not correct. Example with the IV explicitly set.
var cipher:ICipher = Crypto.getCipher("aes256-cbc", keydata);;
var dataBA:ByteArray = Hex.toArray(Hex.fromString(data));
var data:ByteArray = Hex.toArray(Hex.fromString("some data"));
(cipher as IVMode).IV = Hex.toArray(Hex.fromString("some IV"));
cipher.decrypt(data);
I found my typos. I had to change the following lines
var k:String = Hex.fromString("e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c");
to
var k:String = "e5693983c5c21e0f6191eb025d12803d6d17c5359994bf435b964cd0c107fc2c";
v
and
var txt:String = Hex.fromString("691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a");
to
var txt:String = "691682969f1946a1465ccfe19d429ace4188ee254425caa7fa84db5b1fba44a77f1dedfba7a1ffe516cb0646638e28f8ae6422b3cd63d380b21f8b8dcfbe067a";
and
trace(Hex.fromArray(data));
to
trace(Hex.toString(Hex.fromArray(data)));

How to use a For loop to create Google Forms checkbox choices

I have tried numerous iterations of the code below, with limited success. The logger seems to show the commands correctly, but the form doesn't seem to EXECUTE the commands. After executing, the form just generates a single non defined checkbox.
Given that I have to repeat this checkbox question multiple times with slightly different question phrasing, I'm trying to reduce my code footprint, and hopefully become more efficient.
Here is the snippet of code I have that's failing:
var storerangestart = 9901;
var storerangeend = 9999;
page402_cbitem1.setTitle('What stores do you have allocated for this project?');
var page402array = 'page402_cbitem1.setChoices([\n';
for (var i = storerangestart; i < storerangeend; i++) {
var storerangecurrent = i + "";
page402array += 'page402_cbitem1.createChoice(' + storerangecurrent + '),\n';
}
page402array += 'page402_cbitem1.createChoice(' + storerangeend + ')\n]);';
Logger.log(page402array);
page402array();
The Logger isn't showing you commands, it's showing you a string. Your code has created a string that contains text that looks like code, but there is no way to execute the content of that string directly in Google Apps Script.
You've got a good idea - you're just stuck trying to manhandle the wrong object types.
Look at the definition of CheckboxItem.setChoices(), you'll see it takes an array of choices as a parameter. The example code may have thrown you because it created the item elements inside the .setChoices() method call:
item.setChoices([
item.createChoice('Cats'),
item.createChoice('Dogs')
]);
You could realize the same result this way:
var choices = []; // create a new array of choices
choices.push( item.createChoice('Cats') ); // Add 'Cats'
choices.push( item.createChoice('Dogs') ); // Add 'Dogs'
item.setChoices( choices ); // set the array of choices
That sets up the opportunity for the loop that you're thinking about. Instead of building a string that contains code, you need to call the various object methods to first build up an array of choices, and then to assign that array as the choices for your form item:
var storerangestart = 9901;
var storerangeend = 9999;
page402_cbitem1.setTitle('What stores do you have allocated for this project?');
var page402array = [];
for (var i = storerangestart; i < storerangeend; i++) {
var storerangecurrent = i + "";
page402array.push( page402_cbitem1.createChoice( storerangecurrent ) );
}
Logger.log( page402array );
page402_cbitem1.setChoices( page402array );

Creating a dynamic key for an object at creation with ASC2.0 compiler

t's always been a bit of an irritation for me (I do more than a bit of coding in Python too, and the way that deals with dict/object types is much clearer and better), but prior to ASC2.0, I was always able to get around it with,
var fooS:String = 'foo';
var barO:Object = { ('' + fooS): 'bar' };
for (var key:String in barO)
{
trace(key, barO[key]);
}
(prints 'foo bar')
But trying to do that in ASC2.0 results in an
'(' is not allowed here
Error.
Any ideas?
I know that I can always set a key with a dynamic variable through square bracket access, but any time I have to do
var fooS:String = 'foo';
var a:Object = {};
a[fooS] = 'bar';
I find it fairly overkill and wasteful.
AS3 comforms to ECMA 4th edition.
In this edition there is no "inline" way to generate keys.
As you said, the only the following code may be used:
var object:Object = {};
object['some' + 'dynamic' + 'key'] = 'some value';
The only alternative is to create your own API, perhaps like the following:
var map:Map = new Map().put('some' + 'dynamic' + 'key', 'some value');
where Map.put() returns "this"

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]);
}