using a variable name to load movie clip to stage using As3 - arrays

I have created a Spelling Puzzle in AS3 that loads a list of words from an XML file into an Array.
As the code loops through the array, it assigns each word to a variable called "current_word", then scrambles the letters of "current_word" and displays them on stage.
I would like to add an animated MovieClip as a visual aid with the class name that matches the value of current word.
For example if the current word is 'bear', then a MovieClip with the class name 'bear' is loaded from library to stage.
I was trying to create an empty movie clip called "tempItemClip" and overwrite its value with the value of the var current_word.
No errors, but it's not working. I am new to ActionScript. Can someone advise me on the best solution?
public function getWord()
{
current_word=pWord[ques_num];
setTiles(current_word.length);
ques_num++;
trace(current_word);
var tempItemClip:MovieClip = new MyItem();
Puzzle_screen.addChildAt(tempItemClip,4);
tempItemClip.x=380;
tempItemClip.y=130;
//var myClip:Object = getDefinitionByName(current_word);
var myClip:MovieClip = new MovieClip();
tempItemClip[current_word] = myClip;
tempItemClip.addChild(myClip);
}

It's not clear what you are asking, but I believe this is what you want :
var clipClass:Class = getDefinitionByName(current_word) as Class;
var myClip:MovieClip = new clipClass();

Related

SWIFT: assigning all values of an item in a structure array to a variable

Sorry, I don't even have an idea of the keywords to search for answer.
I want to store all items within a global structure to a local variable.
struct HighScores: Codable {
var highscoreRecord: [HighscoreRecord]
}
struct HighscoreRecord: Codable {
var Rank:Int
var Date:Date
var avDuration:Float
var Score:Int
}
A global variable is based on this structure and populated within a UIViewController
var jsonResult: HighScores?
Now, in another UIViewController, I want to extract the values of Score for all Highscores and store it to a local variable. I thought it should look somewhat like this, however, I do not get it to work
#IBDesignable class ScoreTimeGraphView: UIView {
var graphScore = jsonResult!.highscoreRecord.Score
The declaration above throws "Value of type '[HighscoreRecord]' has no member 'Score'"
Any ideas how to do this?
Cheers!
highscoreRecord is an Array. You can't use .Score directly on it because the Array type doesn't have a property named Score.
However, because it's element type is HighScore (which does have the property you want), you can iterate over it and collect the Score property from each one.
I think this is what you are after:
var allGraphScores = jsonResult!.highscoreRecord.map { $0.Score }
.map(_:) takes a closure with one parameter, and passes in each element of a sequence in turn.
So, highscoreRecord.map { $0.Score } returns a new array, by finding the Score property of each HighScoreRecord in the array highscoreRecord.
PS it's probably a good idea to name your variables using lowercase camelCase, for readability and instant recognition by any Swift dev that Score is a variable and not an object.
You're trying to access the property from an array. You need to provide an index to remove the error. Update this line:
var graphScore = jsonResult!.highscoreRecord.Score
To this:
var graphScore = jsonResult!.highscoreRecord[0].Score

Flash getter method is not able to return a 2D Array

As the title says. I tried it with a String or a normal Array and it works. But when I try to pass on my 2D Array my class won't get anything. We're talking about an Array 16 width and about 50 in length.
In my XMLLoader.as class I have this:
function getConvoArray():Array
{
trace("convoArray send");
return convoArray;
}
And in my DialogueGenerator.as class I have this:
xmlLoader = new XMLLoader("ConvoLines.xml");
convoArray = xmlLoader.getConvoArray();
I've checked if the variable convoArray is filled in the XMLLoader.as class by tracing it in a for loop; it works perfectly. But then, when I try to pass it on to the DialogueGenerator.as class it seems to be empty. I cannot excess anything and Flash doesn't give me an error or a warning.
I simply have my Array in DialogueGenerator declared as this:
public var convoArray:Array;
But I tried different ways of declaring it.
Is there a solution for this? A workaround?
For loading xml I use something like this....
var fileName:String = "ConvoLines.xml";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, LoaderComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
loader.load(new URLRequest(fileName + "?rnd=" + Math.random()));
// we affix rand to prevent it from caching the file,
// you don't have to add the ? variable if you aren't worried about it
// updating smoothly
Then in the "LoaderComplete" function we just get the xml out. Hope that helps.
var convoXML:XML = new XML(event.target.data);

actionscript 3: how to access to elements of an array created in a loop dynamically

In the library of the .fla file I have a square exported as Class "cuad" on frame 1
I want to create an Array with 100 squares so as to move them later
So I do like this:
for (var i:uint = 0; i<100;i++)
{
var cuad_mc = new cuad();
addChild(cuad_mc);
myArray.push("cuad_mc");
trace(myArray[i]);
}
I have a runtime error
The error you experience is
Error #1069: Did not find alpha propiety in the String and there is not any value predetermined
The problem comes from your line
myArray.push("cuad_mc");
What you are doing here is pushing a String Object into your Array, not the cuad Object you want. String Objects don't have Alpha values, or x values.
What you want to do is
myArray.push(cuad_mc);
cuad_mc (without the " quotation marks) is a reference to the object you just created.
This should solve your problem. I also recommend using Vectors instead of Array if you only need to store one type of Object. Like this:
var myArray:Vector<cuad> = new Vector<cuad>();
for(var i:int=0;i<100;i++){
var cuad_mc:cuad = new cuad();
addChild(cuad_mc);
myArray.push(cuad_mc);
trace(myArray[i]);
}
Vectors are just like Arrays, but they only allow one specific type, so that a situation like yours doesn't occur.

Array Generating Images

I'm not overly sure if this is possible, as I am not a frequent programmer, but I have a question.
I've got an array that generates one random word in a text box and then a second array that generates another random word in a different text box. What I want is for when a certain word out of array number one is generated, a certain image appears with it. Here's the code:
var firstChoice:Array = ["Do this", "Do that", "Do something else"];
var secondOption:Array = ["while doing this", "while doing that", "while doing something else"];
generate_btn.addEventListener(MouseEvent.CLICK, getTask);
function getTask(event:MouseEvent):void {
var randomChoice:Number = Math.floor(Math.random() * firstChoice.length);
var randomOption:Number = Math.floor(Math.random() * secondOption.length);
Final_Choice.text = firstChoice[randomChoice];
Final_Option.text = secondOption[randomOption];
}
So for instance, when I click the button and the first array generates "Do this," I want a specific graphic to appear with it.
Hopefully this is possible :/ I'm stumped!
probably you need to use a HashMap, such as:
var map:Object = new Object();
map.first_choice = /*url of your image associated with this choice*/
map.second_choice = /*url of your image associated with this choice*/
//etc
and when a word is generated, you just compare the word with keys of the map, using a foreach, and get the url of your image

Easiest way to order array from a string word - Interesting

I am writing a childrens game and it consists of letter blocks.
The child has to put the blocks in a correct order (following silhouettes) to spell the word
Now my question is I have 2 Arrays.
var myLetters = new Array(
new BlockC(),
new BlockA(),
new BlockB()
);
public static var myLetters2 = new Array(
new BlockC(),
new BlockA(),
new BlockB()
);
So you see this is setup to spell the word C A B.
What i would would like to do is have a string variable that i can put the word in to and then have code fill the array in the correct order.
i.e.
var word:String = "CAB";
Hope this makes sense and i can get some good help from you guys
Thanks
If I understand the question correctly, here is one way of doing it :
var word:String = "CAB";
var letterClassMapping:Object = {
"C":BlockC,
"A":BlockA,
"B":BlockB
};
var myLetters:Array = [];
for(var i:int=0; i<word.length; i++) {
myLetters.push( new letterClassMapping[word.charAt(i)]() );
}
Another way is to use getDefinitionByName to get the class type :
var classType:Class = getDefinitionByName("Block" + word.charAt(i)) as Class;
myLetters.push(new classType());
You seem to need a letter collection with corresponding classes. So, you make yourself an Object of the following srtructure:
private static var LETTERS:Object={A:BlockA,B:BlockB,C:BlockC};
Then you split your word by single letters (copy one letter out of a word into a new string) and then you can get corresponding class via LETTERS[letter], and a new instance of that class via new LETTERS[letter]();
You can also create toString() functions in Your object and thant join array .
In class create function :
public class BlockA {
public function toString():String {
return "A";
}
}
And than You can join array items :
var arr:Array = [new BlockA , new BlockB , new BlockC];
trace(arr.join(""));
// and compare to Your string:
arr.join("") == word;

Resources