Trying to dynamically code a variable when loading a bitmap in AS3 - actionscript-3

I am trying to use AS3 to call a series of linked bitmaps randomly. The bitmaps are all named "lines" + # so i created a variable to concatenate lines with a random number which works fine. The example below pulls the lines3 bitmap without issue but if i attempt to sub the lConstruct variable for lines3, it doesn't work. I know this is probably simple but i'm drawing a blank.
var lConstruct:String = ("lines" + RandNumGen())
var lTest:BitmapData=new lines3;
var bitmap1:Bitmap=new Bitmap(lTest);
addChild(bitmap1);

Related

Efficient way to work with multiple arrays in AS3

I'm creating a puzzle game in Flash/AS3. It uses about 30 to 40 groups of words. What I want to do is to load one random group of words in the beginning of the game and after using them, I want to load another random group and so on. What is the best way to solve this? Should I have multiple arrays which I will select from using switch statement based on random number or should I put them all into one multidimensional array? Every group of words could contain up to 2000 words.
It's better you use xml for creating wordgroups and to pull them easily. So, i mean, it's efficient you use multidimensional arrays.
var wordgroups:Array = new Array();
wordgroups.push(new Array("word1", "word3", "word5"), new Array("word2", "word4", "word6")); //you can easily create whole this multidimensional array with xml
wordgroups.shuffle(); //shuffles array (flashpunk library have this function, you may check it out and clone it's function if you can)
var selectedwordgroup:Array;
selectedwordgroup = wordgroups.pop(); //you can easily take a word group from that array and it removes selected wordgroup from the "wordgroups" array automatically with pop() function.
What about XML stuff
Thats a sample *.xml file:
<?xml version="1.0" encoding="utf-8"?>
<wordstuff>
<wordgroups>
<group>word1,word2,word3,word4,word5,word6</group>
<group>word7,word8,word9,word10,word11,word12</group>
<group>word13,word14,word15,word16,word17,word18</group>
</wordgroups>
</wordstuff>
and this is a sample to pull data from xml (you're going to embed your xml stuff to your game, so, you don't need any url loader object, you can embed it like you're embedding image files into your game, with a little difference)
then you can create arrays with this embedded xml
var pulledgroups:Array = new Array();
var my_xml:XML = WORDSTUFF //that's embedded file
var wordgroups:XMLList = my_xml.wordgroups.group;
var group:XML;
for each (group in wordgroups)
{
var pulledwordgroup:Array = group.split(",");
pulledgroups.push(pulledwordgroup);
}
THAT'S ALL, this single code paragraph, pulls all data at the same time

How can I reference a Flash file's own filename from actionscript?

In our pipeline, we ultimately publish HTML5 using Toolkit for CreateJS. However one of the steps to get us to that HTML5 includes publishing an SWF that outputs some Javascript code. I've mostly managed to automate this using JSFL. However, at present there is one line of AS3 that our artists have to find on the timeline and change manually, but it interrupts their workflow, and if they miss it or mess it up it is hard to catch, so I would like to automate it too:
Object(root).log.text += " root.skillAnime189 = factory();\n";
From the above, "skillAnime189.fla" is the name of the .fla file which contains this code. This is the case if the artist is working on Skill Animation #189, but if he is doing #304 or #6 or #1022 (no padding) the number changes accordingly, and he has to update that line accordingly.
So, I would like to change that line to something like:
var flaName:String = getThisFlashFileName().split(".")[0];
Object(root).log.text += " root." + flaName + " = factory();\n";
but I am at a loss as to how to access the name of the .fla file containing the code.
The common way to get the swf name is to parse stage.loaderInfo.url parameter:
var url:String = stage.loaderInfo.url;
url = url.split("?")[0]; //remove query string after "?"
var swfname:String = url.substring(url.lastIndexOf("/")+1);
trace(swfname);
output:
astest.swf
But this code gives swf but, rather than fla, so you need to maintain the same names for flas and published swfs (any case usually they are the same, so it shouldn't be the problem)

Swapping specific movieclips after event?

I've created a drag and drop "puzzle" that has 16 unique pieces, each with their own instance name.
The problem I have is that there are 4 target zones made up of arrays (4 pieces per zone, but the order of the pieces in the zone does not matter). When a piece gets dropped into its correct zone, I would like to "switch" it with another movieclip and have that new movieclip be in the drop target area.
So, for each of the 16 puzzle pieces, I also have 16 unique companion pieces that need to somehow be paired up so that when the visible puzzle piece gets dropped, it is both removed from view, but also replaced with its companion piece.
Any ideas on how to do this?
MovieClip is a dynamic class, meaning that you can add properties to it at runtime. Leveraging this, you can assign a property to your original pieces and call it something like pairedPiece. In this property, you'll store the appropriate value (the name of its pair in the library).
var firstMovieClip:MovieClip;
// do whatever you need to set up your firstMovieClip, attach listeners, etc
firstMovieClip.pairedPiece = "SecondMovieClip";
// the following will occur when the piece is dropped and you need to swap it
var secondMovieClip:MovieClip = new (getDefinitionByName(firstMovieClip.pairedPiece) as Class)() as MovieClip;
secondMovieClip.x = firstMovieClip.x;
secondMovieClip.y = firstMovieClip.y;
firtMovieClip.parent.addChildAt(secondMovieClip, firstMovieClip.parent.getChildIndex(firstMovieClip));
firstMovieClip.parent.removeChild(firstMovieClip);

Actionscript 3 - How to include current movieclip/root location to this.currentFrame?

I am trying to make save progress thingie with sharedobjects, I´ve figured out how those work, but now I should just try to figure out how to make my "save progress" button to not just remember "this.currentFrame", but also if this is on main timeline, or if it is not, which movieclip this is in so that when I upload the sharedObject, it goes to right location, in main timeline or movieclip.
thanks!
I have a feeling SharedObjects may not be able to hold actual movie clip references and be able to restore them reliably (correct me if I am wrong on this), but you could just store objects with key-data pairs in the SharedObject instead of just the frame number alone. Or if you have an unknown number of frame numbers (and movie clips) to remember, store an array of key-data pair objects and loop through them when it comes time to load.
Example of a way to store the data for each clip:
var someClip:Object = new Object();
// some id that refers to the clip (maybe an array/dictionary index)
// (or try replacing with the actual reference to the movie clip to see if it works)
someClip.id ="clipName";
someClip.frameNumber = 1; // the frame number of the clip to remember
Then just store the 'someClip' object into an array in the SharedObject data.
Maybe a more 'compact way' is to do away with having a temporary object and store the key-data pair as just a string with a delimiter. For example, you could just store the string "clipName,1", then when it comes time to load, split along the comma to get the clip id and parse the frame number back to an int.
Or I guess you could also store the frame numbers in a clip id index'ed dictionary and store that in the SharedObject data (as it may save the int parsing step on loading).

Actionscript 3 saving currentframe location to local hard drive?

I asked similar question sometime ago, but I am making new one to be much more specific with my question with some example!
I´ve found this code snippet/tutorial from googling, but I cant seem to figure out how to modify it for my needs:
// open a local shared object called "myStuff", if there is no such object - create a new one
var savedstuff:SharedObject = SharedObject.getLocal("myStuff");
// manage buttons
btnSave.addEventListener(MouseEvent.CLICK, SaveData);
btnLoad.addEventListener(MouseEvent.CLICK, LoadData);
function SaveData(MouseEvent){
savedstuff.data.username = nameField.text // changes var username in sharedobject
savedstuff.flush(); // saves data on hard drive
}
function LoadData(MouseEvent){
if(savedstuff.size>0){ // checks if there is something saved
nameField.text = savedstuff.data.username} // change field text to username variable
}
// if something was saved before, show it on start
if(savedstuff.size>0){
nameField.text = savedstuff.data.username}
So what I am trying to figure out how to do, is to save users current frame to local hard drive, as my flash progress is based on frame location. yeah, so how do I modify it so that it stores data of current frame? and how about current frame inside movieclip, if that is makes things different?
help MUUUCH appreciated! thanks!
In your example it looks like it is already saving something to the shared object:
savedstuff.data.username = nameField.text;
Just replace it with the movie clip frame value instead (and probably under a different property name other then "username").
Then on load, there is another line where it loads the data:
nameField.text = savedstuff.data.username;
It would be the same way, except replace "username" with whatever property name you choose. Then you may have to parse into an int again and use it to restore progress however way you have it set up.