Actionscript add strings to embedded images? - actionscript-3

Say I have something like this:
[Embed(source='../lib/images/image01.png')] var Image:Class
But I want to change that images based on another string like so:`
var StringData:String
StringData = "02";
[Embed(source='../lib/images/image'+ StringData +'.png')] var Image:Class
But this gives me an error, is there another way to do something like this?

Embedded resources are evaluated at compiling time so you can't set a dynamic path.
If you want a unique path by compile type (debug / release for exemple), you can use compiler variables :
[Embed(source=CONFIG::ICON_PATH)]
var Image:Class;
And add compiler args:
-define+=CONFIG::ICON_PATH,'../lib/images/image01.png'
or
-define+=CONFIG::ICON_PATH,'../lib/images/image02.png'

Related

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)

Could not resolve variable (may be a dynamic member)

I created variable with datatype object and created property for this object and asignt value to it.
var tileModel : Object = new Object();
tileModel.property = 10;
But I got warning in FDT Ide. Could not resolve variable (may be a dynamic member)
I have found that possibly solution might be use /*FDT_IGNORE*/ but I would rather solve it some clasic way.
Thank you for every answer
Use array notation as follows:
tileModel["property"] = 10;
To not have FDT flag this as an error, you'll need to adjust the parser to ignore it.
See screenshot:

ActionScript Substituting Lengthy Display List Paths In Code?

i would like to make my code easier to read by replacing long paths with variables, or something similar.
in my code, i can target an instance many times, but the instance can have a lengthy path deep within the display list. for example: myButton instance could be located at myButtonsPanel.section2.redArea.myButton, or something like that.
is it possible to substitue this long path as a variable or constant? something like:
var myPath = myButtonsPanel.section2.redArea;
therefore, calling the instance would be:
myPath.myButton;
Not the path , but the actual Button
var myButton:MovieClip = myButtonsPanel.section2.redArea.myButton;
although you could also do this:
var myContainer:MovieClip = myButtonsPanel.section2.redArea;
//then access your button like this
myContainer.myButton
It really depends on what you need to do, but the idea is that instead of storing a path, you're actually referencing a MovieClip directly, then using that variable to access this MovieClip

Actionscript 3, read file text at compile time?

There is some way to make the flash put the content of one text file in a string, or at least put it in the .swf, so the user don't need to download it?
Solution:
[Embed(source = "ExampleText.txt", mimeType = "application/octet-stream")]
protected var pscene:Class;
var tmp:ByteArray = new pscene();
result = tmp.readMultiByte(tmp.bytesAvailable, tmp.endian);
It took me a while to understand how to read the data from the Byte Array.
I've always used a pre-compile step to populate a template. It's pretty easy to make a class that has a string to be populated before compilation.
Ant can do this pretty easily. Check out the replace task.

Accessing the Body of a Function with Lua

I'm going back to the basics here but in Lua, you can define a table like so:
myTable = {}
myTable [1] = 12
Printing the table reference itself brings back a pointer to it. To access its elements you need to specify an index (i.e. exactly like you would an array)
print(myTable ) --prints pointer
print(myTable[1]) --prints 12
Now functions are a different story. You can define and print a function like so:
myFunc = function() local x = 14 end --Defined function
print(myFunc) --Printed pointer to function
Is there a way to access the body of a defined function. I am trying to put together a small code visualizer and would like to 'seed' a given function with special functions/variables to allow a visualizer to 'hook' itself into the code, I would need to be able to redefine the function either from a variable or a string.
There is no way to get access to body source code of given function in plain Lua. Source code is thrown away after compilation to byte-code.
Note BTW that function may be defined in run-time with loadstring-like facility.
Partial solutions are possible — depending on what you actually want to achieve.
You may get source code position from the debug library — if debug library is enabled and debug symbols are not stripped from the bytecode. After that you may load actual source file and extract code from there.
You may decorate functions you're interested in manually with required metadata. Note that functions in Lua are valid table keys, so you may create a function-to-metadata table. You would want to make this table weak-keyed, so it would not prevent functions from being collected by GC.
If you would need a solution for analyzing Lua code, take a look at Metalua.
Check out Lua Introspective Facilities in the debugging library.
The main introspective function in the
debug library is the debug.getinfo
function. Its first parameter may be a
function or a stack level. When you
call debug.getinfo(foo) for some
function foo, you get a table with
some data about that function. The
table may have the following fields:
The field you would want is func I think.
Using the debug library is your only bet. Using that, you can get either the string (if the function is defined in a chunk that was loaded with 'loadstring') or the name of the file in which the function was defined; together with the line-numbers at which the function definition starts and ends. See the documentation.
Here at my current job we have patched Lua so that it even gives you the column numbers for the start and end of the function, so you can get the function source using that. The patch is not very difficult to reproduce, but I don't think I'll be allowed to post it here :-(
You could accomplish this by creating an environment for each function (see setfenv) and using global (versus local) variables. Variables created in the function would then appear in the environment table after the function is executed.
env = {}
myFunc = function() x = 14 end
setfenv(myFunc, env)
myFunc()
print(myFunc) -- prints pointer
print(env.x) -- prints 14
Alternatively, you could make use of the Debug Library:
> myFunc = function() local x = 14 ; debug.debug() end
> myFunc()
> lua_debug> _, x = debug.getlocal(3, 1)
> lua_debug> print(x) -- prints 14
It would probably be more useful to you to retrieve the local variables with a hook function instead of explicitly entering debug mode (i.e. adding the debug.debug() call)
There is also a Debug Interface in the Lua C API.