How to use string in path to object? - actionscript

I need to use string in path to object.
var nameOfTrails:String = "trail"+this.getDepth();
_parent.createEmptyMovieClip(nameOfTrails,this.getDepth()+1);
_parent.nameOfTrails.beginFill(FillColor,FillAlpha);
How to do this in ActionScript 2.0?

In you case you could access instance you created like this:
_parent[nameOfTrails]
The point is that you can access an object using string with his name by searching proper object for property with that name.
In your example you creating variable with id of nameOfTrails value inside some object which relate to your current code scope as parent, and put reference to newly created MovieClip inside this variable. So now, object referenced by _parent have property named for example 'trail0' (by the way this.getDepth() is not so smart thing to do then you creating objects in other scope). All you need to do now is access that property using classical 'give me property of that object by his key' style - obj['propertyName'] and voila.

You can do that like this :
for(var i:Number = 0; i < 3; i++){
var movie_clip:MovieClip = this.createEmptyMovieClip('movie_clip_' + i, i);
movie_clip._y = i * 22;
var text_field:TextField = movie_clip.createTextField('text_field', 0, 0, 0, 120, 18);
text_field.text = 'movie clip : ' + i;
}
var j:Number = 2;
trace(this['movie_clip_' + j].text_field.text); // gives : movie clip : 2
trace(eval('movie_clip_' + 1).text_field.text); // gives : movie clip : 1
For more details, take a look on the eval function.
Hope that can help.

Related

Count of the biggest bin in histogram, C#, sharp

I want to make histogram of my data so, I use histogram class at c# using MathNet.Numerics.Statistics.
double[] array = { 2, 2, 5,56,78,97,3,3,5,23,34,67,12,45,65 };
Vector<double> data = Vector<double>.Build.DenseOfArray(array);
int binAmount = 3;
Histogram _currentHistogram = new Histogram(data, binAmount);
How can I get the count of the biggest bin? Or just the index of the bigest bin? I try to get it by using GetBucketOf but to do this I need the element in this bucket :(
Is there any other way to do this? I read the documentation and Google and I can't find anything.
(Hi, I would use a comment for this but i just joined so today and don't yet have 50 reputation to comment!) I just had a look at - http://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Histogram.htm. That documentation page (footer says it was built using http://docu.jagregory.com/) shows a public property named Item which returns a Bucket. I'm wondering if that is the property you need to use because the automatically generated documentation states that the Item property "Gets' the n'th bucket" but isn't clear how the Item property acts as an indexer. Looking at your code i would try _currentHistogram.Item[n] first (if that doesn't work try _currentHistogram[n]) where you are iterating the Buckets in the histogram using something like -
var countOfBiggest = -1;
var indexOfBiggest = -1;
for (var n = 0; n < _currentHistogram.BucketCount; n++)
{
if (_currentHistogram.Item[n].Count > countOfBiggest)
{
countOfBiggest = _currentHistogram.Item[n].Count;
indexOfBiggest = n;
}
}
The code above assumes that Histogram uses 0-based and not 1-based indexing.

Create an empty OPENARRAY in C++Builder

I'm using C++Builder with SuperObject JSON parser, and trying to construct an array.
_di_ISuperObject json = SO("{}");
json->O["data.names"] = SA(ARRAYOFCONST(("")));
for (int i=0; i < v.size(); ++i)
json->A["data.names"]->S[i] = v[i];
Now, the code above does what I want - unless v.size() == 0. In that case, I get an array with a single 'empty' string in it.
This is because of the 'dummy' array creation using ARRAYOFCONST(("")).
What's the correct way to create an 'empty' OPENARRAY to pass to SuperObject?
You cannot use ARRAYOFCONST() or OPENARRAY() to create a 0-element openarray. Those macros require a minimum of 1 input value.
I am not familiar with SuperObject, but if O[] creates a new JSON array from existing values and A[] simply fills the array, you could try using the SLICE() macro to create and fill an openarray from v directly if v is a std::vector<TVarRec>:
if (!v.empty())
json->O["data.names"] = SA( SLICE(&v[0], v.size()) );
If you really need a 0-element openarray if v is empty, try this:
if (v.empty())
json->O["data.names"] = SA( NULL, -1 );
else
json->O["data.names"] = SA( SLICE(&v[0], v.size()) );
If v does not contain TVarRec values then you can create a separate std::vector<TVarRec> first and then SLICE() that into SuperObject (just be careful because TVarRec does not perform reference counting on reference-counted data types, such as strings - by design - so make sure temporaries are not created when you assign the TVarRec values or else they will be leaked!):
if (v.empty())
json->O["data.names"] = SA( NULL, -1 );
else
{
std:vector<TVarRec> tmp(v.size());
for (size_t idx = 0; idx < v.size(); ++idx)
tmp[idx] = v[idx];
json->O["data.names"] = SA( SLICE(&tmp[0], tmp.size()) );
}

using concatenation in getUrl during loop?

I would like to write Actionscript loop that involves "getURL". However, from what I can see getURL does not allow concatenation of variable names?
I have variables textholder0, textholder1, textholder2 that have movieclip names as values and link0, link1, link2 that have website addresses as values.
I can use this["textholder" + 0].onRelease but getURL("link"+ 0) gives "undefined"
textholder0.onRelease = function()
{
getURL(link0);
}
textholder1.onRelease = function()
{
getURL(link1);
}
textholder2.onRelease = function()
{
getURL(link2);
}
Any way to do this so I can create a loop for the above?
Here is a test. Unfortunately, it still gives me "undefined/" for the URL. To keep it simple I created three movie clips, with instances textholder0, textholder1, textholder2. Put a loop on the main timeline.
var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];
for(var i:Number=0; i<links.length; i++){
this["textholder" + i].linkURL = links[i];
this["textholder" + i].onRelease = function() {
getURL(linkURL);
}
}
Here is output from debugger window
Variable _level0.links = [object #1, class 'Array'] [
0:"http://www.google.ca",
1:"http://www.google.com",
2:"http://www.google.ru" ]
Variable _level0.i = 3
Movie Clip: Target="_level0.textholder0"
Variable _level0.textholder0.linkURL = "http://www.google.ca"
Variable _level0.textholder0.onRelease = [function 'onRelease']
Movie Clip: Target="_level0.textholder1"
Variable _level0.textholder1.linkURL = "http://www.google.com"
Variable _level0.textholder1.onRelease = [function 'onRelease']
Movie Clip: Target="_level0.textholder2"
Variable _level0.textholder2.linkURL = "http://www.google.ru"
Variable _level0.textholder2.onRelease = [function 'onRelease']
I am starting to think that you can not use onRelease within a loop at all.
getURL("link"+ 0) will try to go to a URL "link0", since "link"+ 0 will be concatenated to the string "link0", and not get the value of link0. But you can try doing this:
getURL(this["link" + 0]);
The difference, and the mechanism of the bracket notation, is that you can reference a property of an object in two ways - using dot notation, like this.link0, or the bracket notation, this["link0"]. But it has to be expressed as an object property, just saying "link" + 0 anywhere, like in getURL("link"+ 0) won't give a reference to link0.
ok, so I think the problem with the loop here is that it was incrementing "i" variable before any of the buttons were clicked.
http://www.senocular.com/flash/tutorials/faq/#loopfunctions
Senocular.com says "you need to define a new, unique variable to represent that value at the time of function creation and have the function reference that value"
So the loop goes as following
var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];
var curr_button;
for(var i=0; i<=links.length; i++){
curr_button = this["textholder"+i];
//note creation of an extra variable "num" below to store the temp number
curr_button.num = i;
curr_button.onRelease = function() {
getURL(links[this.num]);
}
}

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I'm trying to use to dynamically create variable.
Example
for( i = 0 ; i &lt 3 ; i++)
{
var MyVar+i = i;
}
after this for loop runs, i would like to have 3 variables named MyVar1, MyVar2, MyVar3. I know the code above will give you a syntax error, but that is just to illustrate what I am trying to do. Any takers?
The primary reason i'm doing this is because I'm having scope problems noted here in this other unanswered Action Script question: How to pass variables into inline functions in Action Script 2
Thanks!
I could be wrong (I haven't done AS2 for a long while), but I think you can do this using array syntax:
for( i = 0 ; i < 3 ; i++)
{
this["myVar"+i] = i;
}
and then for variable access:
var foo = this["myVar0"] //etc
First answer is correct, but if you make the class dynamic (ie. new members can be created dynamically) ...
dynamic class ClassName { // etc. }
... then you can reference the variable in normal syntax:
var foo = this.myVar0;
You won't be able to access the variable at all without 'this' whether the class is dynamic or not.

Is there a better way to get hold of a reference to a movie clip in actionscript using a string without eval

I have created a bunch of movie clips which all have similar names and then after some other event I have built up a string like:
var clipName = "barLeft42"
which is held inside another movie clip called 'thing'.
I have been able to get hold of a reference using:
var movieClip = Eval( "_root.thing." + clipName )
But that feels bad - is there a better way?
Movie clips are collections in actionscript (like most and similar to javascript, everything is basically key-value pairs). You can index into the collection using square brackets and a string for the key name like:
_root.thing[ "barLeft42" ]
That should do the trick for you...
The better way, which avoids using the deprecated eval, is to index with square brackets:
var movieClip = _root.thing[ "barLeft42" ]
But the best way is to keep references to the clips you make, and access them by reference, rather than by name:
var movieClipArray = new Array();
for (var i=0; i<45; i++) {
var mc = _root.thing.createEmptyMovieClip( "barLeft"+i, i );
// ...
movieClipArray.push( mc );
}
// ...
var movieClip = movieClipArray[ 42 ];
You can use brackets and include variables within them... so if you wanted to loop through them all you can do this:
for (var i=0; i<99; i++) {
var clipName = _root.thing["barLeft"+i];
}

Resources