I have an AssetManager singleton that I'm using as a data provider of sorts.
public static var L1_1:Object = {pic:'../Assets/images/test.jpg', answer:'someAnswer', pool: 'somePool'}
So I'm trying to use this in another class to get a path for an image class that takes a string as a param like so:
var image:Image = new Image(AssetManager.L1_1.pic , 540, 540);
This works great, however, I need to increment the second '1' above to make it dynamic so I can say for eg.,
var image:Image = new Image(AssetManager.L1_5.pic , 540, 540);
I have tried things like this, passing an imageNum property but can't sort it.
var tempObj:Object = ['AssetManager.L' + imageNum + '_' + imageNum + '.pic'];
trace(tempObj)
Is there an easier way to do this? Thanks!!
Static and public properties in AS3 can be accessed using bracket syntax.
Here's an example:
public static var L1_1:Object = {pic:'../Assets/images/test.jpg', answer:'someAnswer', pool: 'somePool'};
var imageNum:int = 1;
var propertyName:String = "L1"+ "_" + imageNum.toString();
var tempObj:Object = AssetManager[propertyName];
var pathToPic:String = tempObj.pic;
trace(pathToPic);
The above example should trace out ../Assets/images/test.jpg
However, be aware that bracket syntax is slower than dot syntax and is not type checked at compile time.
I hope this helps.
I have a fix for now by simply using an array like so:
public static var L1_1:Array = new Array();
L1_1[0] = ['../Assets/images/test.jpg', "apple", "appletakeout"];
And calling it with:
image = new Image(AssetManager.L1_1[0][imageNum], 540, 540);
And it works, just feels a tiny bit messy. If there's a more efficient way to do this I'd love to hear. Thanks.
Related
Using shared preferences to save an arrayListOf in my app. Im converting the array to a hashset and saving it but when I load it I am unable to convert it back to an arrayListOf. I have tried toTypedArray() and toArray() but neither solve the problem. Getting the error below.
Required:
kotlin.collections.ArrayList /* = java.util.ArrayList */
Found:
(Mutable)Set<String!>?
Using the following to try and load the hashet and convert it.
var sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
var places = arrayListOf<String>()
var loadSet = sharedPreferences.getStringSet("key", null)
places = loadSet
go.setOnClickListener{
thread {
val place = newLocation.text.toString()
var stringCounter = counter.toString()
if(place !in places){
places.add(place)
//editor.putStringSet("key", places)
//editor.commit()
var sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE)
var editor = sharedPreferences.edit()
set.add(place)
editor.putStringSet("key", set);
editor.commit();
Log.d("set", set.toString())
var loadSet = sharedPreferences.getStringSet("key", HashSet<String>())?.toList()
Log.d("load", loadSet.toString())
places = loadSet as ArrayList<String>
}
arrayListOf isn’t a type. It’s a function. The type is ArrayList, but it’s more typical to only need the more abstract List or MutableList, depending on what you’re doing with it. For those you can use set.toList() or set.toMutableList().
If you have an existing ArrayList or MutableList, you could also do list.addAll(set).
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);
I have such objects in ScriptDb,
[{a:1,b:2,c:3},{a:0,b:0}]
How do I query object without key c?
It seems the only way is to query all object using db.query({}), then use something like "typeof result.c == 'undefined'".
Is there a way to do it in ScriptDb?
Thanks.
You can use that to get records without c:
var db = ScriptDb.getMyDb();
var result = db.query({c: db.not(db.anyValue())});
while (result.hasNext()) {
var current = result.next();
Logger.log ("a= "+current.a+", c="+current.c);
}
The ones with c:
var result = db.query({c: db.anyValue()});
These functions (not, anyValue...) are documented in Class ScriptDbInstance
I have created an array which is being used to store a series of .gif images and I'm just trying to test everything out by using document.getElementById to change the .src value but when I change it and load the page the image stays the same as it was before.
function setImage()
{
var images = new Array();
images[0] = anemone.gif;
images[1] = ball.gif;
images[2] = crab.gif;
images[3] = fish2.gif;
images[4] = gull.gif;
images[5] = jellyfish.gif;
images[6] = moon.gif;
images[7] = sail.gif;
images[8] = shell.gif;
images[9] = snail.gif;
images[10] = sun.gif;
images[11] = sunnies.gif;
images[12] = whale.gif;
var slots = new Array();
slots[0] = document.getElementById("slot" + 0);
slots[1] = document.getElementById("slot" + 1);
slots[2] = document.getElementById("slot" + 2);
slots[0].src = "snail.gif";
document.getElementById('slot0').src = images[0];
alert(images.length);
}
I can't understand why the image wont change, but I know it has to be something very simple. I've been wasting hours trying to get this one thing to change but nothing works. can anyone please point out the error of my ways?
There are a couple of issues with your code:
Your filenames need to be Strings, so they'll have to be quoted (also you can simplify the Array creation):
var images = ['anemone.gif', 'ball.gif', 'crab.gif', 'fish2.gif', 'gull.gif', 'jellyfish.gif', 'moon.gif', 'sail.gif', 'shell.gif', 'snail.gif', 'sun.gif', 'sunnies.gif', 'whale.gif'];
Also make sure you are getting your slot-elements right, quote all the attributes like:
<img id="slot0" class="slot" src="crab.gif" width="120" height="80">
When you create the slots-Array you can do it like this (no need to concat the ID string):
var slots = [document.getElementById('slot0'), document.getElementById('slot1'), document.getElementById('slot2')];
Finally make sure you call your function when the document has loaded / the DOM is ready. If you don't want to use a framework like jQuery your easiest bet is probably still using window.onload:
window.onload = setImage; //note that the parens are missing as you want to refer to the function instead of executing it
Further reading on Arrays, window.onload and DOMReady:
https://developer.mozilla.org/de/docs/DOM/window.onload
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array
javascript domready?
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;