Assign function argument to global variable of the same name - actionscript-3

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.

Related

How can I get updated parameter from CallFunc? (cocos2d-x V3.0)

I want to get updated the parameter time from CallFunc. However, the time I got from CallFunc where I updated, is always ZERO. That is DelayTime::create(updatedTime) is ZERO.
Do anyone know how to use parameter from CallFunc in DelayTime?
1/ Please provide your code. We don't understand your question.
2/ Both CallFunc and DelayTime return an cocos2d::Action, you can't call one of them inside each other.
3/ CallFunc:
http://www.cocos2d-x.org/docs/api-ref/cplusplus/v3x/d3/d32/classcocos2d_1_1_call_func.html
function define: static CallFunc* create (Ref *target, SEL_CallFunc selector)
example:
using namespace cocos2d;
class A() : Node //(or anything that inherited Ref)
{/*...(defined function f())...*/}
A *a = new A();
CallFunc* callFunc = CallFunc::create(a, SEL_CallFunc(&A::f));
a->runAction(callFunc);
DelayTime:
http://www.cocos2d-x.org/docs/api-ref/cplusplus/v3x/d5/d58/classcocos2d_1_1_delay_time.html
function define: static DelayTime* create(float d)
example:
A *a = new A();
DelayTime* wait = DelayTime::create(4);
a->runAction(wait); // this is useless, you must put a DelayTime in a Sequence
p/s: Sequence is an Action, too. To create a Sequence, use
Sequence* seq = Sequence::create(action1, action2, action3, NULL);

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.

Call a function if String number is inferior to 0?

I've got this code in my Toolbar.as :
var money = 9999;
argent.text = String(money);
trace(money);
How do I do to say
if (money < 0){
callFunction();
}
?
Thank you very much for your answers
EDIT
I've tried everything.
Here's what I did :
var money:int = 9999;
argent.text = money.toString();
trace(money);
stageRef.addEventListener("checkingMoney", checkMoney, false, 0, true);
I've add the EventListner in order to check the money (as nothing was triggering the condition if money<0 before).
And then :
public function checkMoney(event):void{
var money;
trace("checking");
if (parseInt(money) < 0){
trace("dangerous");
}
}
So the function is well triggered (the trace "checking" is on), but even the number is under 0 (-4600), the trace "dangerous" do NOT appear..
I don't understand.
You need to read up on how datatyping works. It works similarly in all OOP-based languages, so an AS3-specific article really isn't necessary. Avoid anything regarding PHP datatypes, though.
For what you have posted, though, you have done a few things incorrectly.
First off, every object (whether it be a variable, function, or class), should always have the datatype declared. You can get away with not doing it, but your app will run slightly faster if you do and there is less chance of compiler warnings.
So
var money = 9999;
should be
var money:int = 9999; // could also use Number (AS3's float) or uint
You should avoid hard-casts such as String(money), as well. This can be slow and can cause errors. For casting a Number to a String, you should always use Number.toString(), as that is its intended purpose and I believe it is optimized whereas other cast types are not.
if (money < 0) is actually correct. It may have been throwing IDE warnings because you didn't set the type of money. If you need to compare a numeric String value, you need to cast it back to a number using Number(var). Note that casting a String to Number is one of the few times you want to use a hard cast DataType(object) instead of a soft cast (object as DataType).
I don't get the problem.
1st of all money variable is missing a type.
If money is int here than:
var money:int = 9999;
argent.text = money.toString();
trace(money);
if (money < 0){
callFunction();
}
and that should work.
If for some reason money is String in here than:
var money:String = "9999";
argent.text = money;
trace(money);
if (Number(money) < 0){
callFunction();
}
I don't usually do this, but I'm posting a second answer because your edit is drastically different than what you originally posted. For future reference, please post the entire code that is relevant. You seemed to have missed some context originally and forced us to solve the wrong problem (don't get me wrong, it still needed to be fixed but it was not the issue at hand)
So what you are seeing is a scope issue. Basically, an object declared in an object (be it a Class, Function, loop, or conditional) is only available within that object and within child objects. Additionally, all objects declared in the top-level scope of a class must have an access modifier (public, private, internal, protected, etc).
So let's assume this class structure:
public class ClassName {
public function ClassName();
public function checkMoney();
}
An object declared in the constructor (ClassName()) is not available in checkMoney(). So you need to do one of two things:
Declare the object in top-level scope:
public class ClassName {
private var money:int;
public function ClassName(){
money = 9999;
checkMoney()
}
public function checkMoney() {
// you now have access to money
}
}
or pass the object into the function:
public class ClassName {
public function ClassName(){
var money:int = 9999;
checkMoney(money);
}
public function checkMoney(value:Number) {
// check "value" here. Note that Numbers and Strings are NOT passed be reference, so changing the value will NOT change the original variable
}
}

MatLab fmin function

I have an .m file in matlab named PowerMinimiser and two functions in it
function PowerMinimiser
PowerOut = fmin(minFunction,0,100);
display(PowerOut)
end
and
function PowerOut = minFunction(varargin)
RunMode = 2;
ThresholdValue = 10;
if nargin > 0
ThresholdValue = varargin{1};
end
%Receive PowerOut value from .main file and pass in ThresholdValue and
%RunMode values:
[PowerOut] = main(ThresholdValue,RunMode);
end
Now what I would like to do is use the matlab fmin function so that I can find a value for the variable ThresholdValue which will give the lowest value possible for the variable PowerOut. The value for ThresholdValue is a number between 1 and 100, and is passed into a function in the main.m where a number of calculations are done and then a value for PowerOut is outputted.
Using the minFunction function I am able to receive and pass the variable fine, using the line
[PowerOut] = main(ThresholdValue,RunMode);
but I am not sure how to use the fmin function to get the value of ThresholdValue that gives the lowest value for PowerOut. I would like the ThresholdValue value to be shown once the fmin has done the necessary calculations. How can I do this? Any help would be greatly appreciated.
Thanks
You should use function fminbnd, which replaced fmin function in newer versions of Matlab. To pass function as an argument to fminbnd use # in front of the function name, like this:
PowerOut = fmin( #minFunction, 0, 100 );

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