how to increase a variable which name You give - actionscript-3

how do i create a function where i put the name of the variable I want to make Math with,
what i mean is :
so somewhere in a enterFrameFunction i call effectOne and give its 1st parameter to be with the name of "myGravity"
private function effectOne(TriggeringVariable:String, minYposition:Number = 300, maxYposition:Number = 100):Number
{
var myReturnVariable:Number = minYposition + maxYposition;
TriggeringVariable +=10;
if(TriggeringVariable > 980) TriggeringVariable = 980;
if(TriggeringVariable > 500) minYposition = minYposition / 2;
if(TriggeringVariable > 980) maxYposition = maxYposition / 2;
return myReturnVariable;
}
but it gives me ->
1067: Implicit coercion of a value of type String to an unrelated type Number.
how do i make this ?

you can do it like this:
this[triggeringVariable]+=10;
That will get the variable by the name you gave, so if triggeringVariable is "x", it will increase the x value of your instance.
But that will only work when that variable actually exists, and that cannot be checked at compile time. So if you make mistakes, you'll get runtime errors.

Related

AS3 event.target and var

I have an editable text field (c) in a movieClip, well 3 movielips like this actually named a1, a2 and a3. Movieclips are already on stage. The path to text field in each MC is mc.a1.c, mc.a2.c and mc.a3.c
The initial value for each textfield is set by XML which is also stored in variables with the same names and the movieclip(a1,a2,a3). If the user updates a textfield a CHANGE event listener triggers checkValue function. If the value is greater than my maxValue I want my function to return the text field to its original value and give the user an error message. So if textfield c in mc.a1.c is updated, I'm currently taking the name of its parent (a1) and then trying to reference the variable with the same name so that textfield c will be returned to the initial value held in var a1 (I'll only know which var to reference once a textfield update has been attempted.. hope that makes sense)
I've tried several things but always end up with the variable name, and not its value in the textfield. So, for now I've reverted to populating the field with 0 until I can find an answer.
example code:
aH.t1 is the predefined max value
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
if (thR =="a"&&thN>int(aH.t1.text)){
event.target.text = 0; //I want the reference var a(x)and have its value in the text field
aH.errorMsg.text = "The number cannot be greater than 10 so the original value has been restored";
}
}
As you can probably tell my my code, I'm not a developer and I've already looked here in search of but can't seem to get grasp it...Is it me?
reference variable AS3
AS3: Using string as variable
Is what I'm trying to do achiveable in AS3?
Thanks to guidance from dene the solution looks like this:
function chngTh(event:Event):void{
var thR:String = String(event.target.parent.name.substring(0,1));
var thN:int = (event.target.text);
var thov:int = root[event.target.parent.name];
if (thR =="a"&&thN>int(aH.thrsh.t1.text)){
event.target.text = thov;
aH.errorMsg.text = hclbl[12];
}
}
Use event.target in the listener function to reference the text field that changed:
var maxValue = 5;
myTextField.addEventListener(Event.CHANGE, textListener);
function textListener(event:Event)
{
var tf = event.target as TextField;
var currentValue = parseFloat(tf.text);
if (currentValue > maxValue) {
tf.text = getOriginalValue(tf);
}
}
function getOriginalValue(tf:TextField) : Number
{
// Assuming the textfield's parent is named "a" + number (eg. a1, a2 etc.)
// Get the number of the parent by ignoring the character at index 0
var parentName = tf.parent.name;
var parentNumber = parentName.substring(1);
// Now you can use parentNumber to access the associated variable (a1, a2, etc)
// Assuming these variables are defined on the root (main timeline).
var originalValue = root["a" + parentNumber]
// If the variables are stored as Strings, this line is needed to convert it to a Number type
originalValue = parseFloat(originalValue)
return originalValue;
}

Assign function argument to global variable of the same name

Is it possible to set a global variable from within a function that takes that same variable name as an argument?
var a:int = 0;
function test(a:int)
{
a *global* = a *local*;
}
test(1);
trace(a) // traces 0 but I'd like it to trace 1
(The reason why I'd like to do this, is to avoid constantly coming up different variables names for the same things)
Thanks in advance.
You could refer to it explicitly as this.a = a; in your function test
In this case this is the class instance that holds this variable. In case of a static variable, you can use ClassName.a = a.

AS3 - Get and Set

Why does the get and set keywords exist? They seem to be useless for me...
For example:
public function set player_X(x:Number):void
{
player.x = x;
}
public function setPlayerX(x:Number):void
{
player.x = x;
}
These two functions does the same thing right? And the second one does not use the set keyword.
The difference is that the set method is implicitly called when you set a property of the same name.
You do not have to type the ( ) that do the function call but assign the value via =.
player_X = 5;
vs.
setPlayerX(5);
It can help with information hiding as to the user of a class, this is appears to be a property and can be used as such.

How do I get this Scala count down function to work?

Basically what I want this code to do is define a function, timeCount, and each time it is called I want 1 to be subtracted from 21, have the result be printed out, and have the result equal timeNum. So, I want 1 to be subtracted from 21, resulting in 20, printing out 20, then making timeNum equal to 20. Then the next time the function is called, it would be equal to 19, 18, etc, etc, ...
However, this code does not work. How can I get what I want to happen?
def timeCount() = {
var timeNum = 21
var timeLeft = timeNum-1
println(timeLeft)
var timeNum = timeLeft
}
}
class TimeCount(initial: Int) extends Function0[Int] {
var count = initial
def apply = {
count = count - 1
count
}
}
Use it like this:
scala> val f = new TimeCount(21)
f: TimeCount = <function0>
scala> f()
res0: Int = 20
scala> f()
res1: Int = 19
scala> f()
res2: Int = 18
I made two changes from what you were asking for, I made the number to start with a parameter to the constructor of the function, and I returned the count instead of printing it, because these seem more useful If you really just want to print it, change Function0[Int] to Function0[Unit] and change count to println(count.toString)
This is, of course, not threadsafe.
As the compiler error below states, you defined the same var twice.
console>:11: error: timeNum is already defined as variable timeNum
var timeNum = timeLeft
^
Keep in mind, one can modify the value of a var, but not to define it again.
Additionally, you declare and initialize the timeNum var in the function call. The result being that once it does compile you will print the same value (20) repeatedly.
Also, the timeLeft var is superfluous.

ActionScript – Default Datatype for Untyped Variables?

if i don't specifically type a variable in my code will it compile as a default datatype? for example, the "for each ... in" function works best without typing the variable:
for each (var element in myArray)
{
//process each element
}
does my element variable have a datatype? if it is typed as Object is it better to actually write element:Object or does it matter?
EDIT
actually, that was a bad example, since the element variable is going to be typed to whatever element is in myArray.
but is that how it works if a variable is untyped? it will simply become what is passed to it?
here's a better example for my question:
var a = "i'm a string"; //does this var becomes a String?
var b = 50.98; //does this var becomes a Number?
var c = 2; //does this var becomes an int?
for each (var element in myArray)
The element variable doesn't have any data type - it is untyped and thus can hold anything.
And yeah, it is equivalent to writing element:Object or element:*, but it is always advisable to type your variables - this will help you catch some errors before you run the code. The mxmlc compiler will emit a warning if you don't do this, which can be fixed by typing it as e:Object or e:*.
var a = 45.3; //untyped variable `a`
a = "asd"; //can hold anything
/*
This is fine: currently variable `a` contains
a String object, which does have a `charAt` function.
*/
trace(a.charAt(1));
a = 23;
/*
Run time error: currently variable `a` contains
a Number, which doesn't have a `charAt` function.
If you had specified the type of variable `a` when
you declared it, this would have been
detected at the time of compilation itself.
*/
trace(a.charAt(1)); //run time error
var b:Number = 45.3;
b = "asd"; //compiler error
trace(a.charAt(1)); //compiler error