cocos2dx getChildByTag function ,declaring the tag - cocos2d-x

I am trying to update score in an app using the method from this website
http://fabiosistemas.com.br/show-strings-using-numbers-in-cocos2d-x/
CCLabelBMFont *label2 = CCLabelBMFont::create( “Score: 0″, “Arial.fnt” );
addChild(label2, 100, kTagSprite2);
CCLabelBMFont* label2 = (CCLabelBMFont*) getChildByTag(kTagSprite2);
label2->setString(stringPontos);
compiler gives error: of the kTagSprite2 not declared in this scope
how do i declare the KTagSprite2 , as in what type?

You have to declare kTagSprite2 before using it, because it is not a keyword or any constant declared by cocos2d-x. So here you go..
We can assign an integer value as a tag to any CCNode in cocos2d.
//Global declaration
#define kTagSprite2 1234
//Here you are setting kTagSprite2 or 1234 as tag value of label2
addChild(label2, 100, kTagSprite2);
//Here you are getting the child to which kTagSprite2 or 1234 is assigned as tag value
//i.e your label. This will return a child which has 1234 as its tag value
CCLabelBMFont* label2 = (CCLabelBMFont*) getChildByTag(kTagSprite2);
label2->setString(stringPontos);
Hope this helps you.

Related

onClick is always returning 25

I'm using reactjs.
I have a series of divs that are created that represent a employee's availability. When clicked, they will display the number. However, they all alert the number 25 instead of their number.
for (
var i = state.availability[day][0];
i <= state.availability[day][1];
i++
) {
tempdaySlot.push(
<div
key={"ena:" + TimeConverter(i)}
style={LocalStyles.daySlot}
onClick={() => alert(i)}
>
{TimeConverter(i)}
</div>
);
}
state.availability is a two dimensional array that holds a number between 1-24 that represents someone's availability (ex [9-17])
TimeConverter is a function that converts a 24 hour type number to the usual 12 hour format (17 = "5 pm")
tempdaySlot is just a temporary array before I put it into a state variable
Use let i instead of var i. var hoists variable to the parent scope, let creates block scope.
this is a hoisting issue, because you defined the i using var it will be hoisted to the top of the function body so the last value which is 25 will be the only thing stored in that variable and since the variable isn't destroyed because it's hoisted it will only show the latest value.
just use let instead of var its a good practice to use let and const instead of var now so try getting used it, it will save you a lot of headaches.
Tl;dr declare i using let, not var
The var keyword creates variables with function scope. However, when you create a closure (like your onClick function), the lexical environment is captured by reference. When you call that onClick function later, it gets the current value of i (which is 25, since that's where the loop stopped), not the value of i when you created the function.
The let keyword creates i with traditional scoping, so in effect a new i is created on each iteration of the loop which solves this problem.
See What's the difference between using "let" and "var"? for more.

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.

Specify whever a lua parameter should be a copy or a reference

I'm wondering if there is a way to specify if the parameters of a lua function should be copied or just referenced. Color is an object representing a color.
For example, with this code
function editColor(col)
col.r = 0
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes the output
255
0
0
So col is a "reference" to color, but this code:
function editColor(col)
col = Color(0, 0, 0)
print(tostring(col.r))
end
color = Color(255, 0, 0)
print(tostring(color.r))
editColor(color)
print(tostring(color.r))
Makes this output
255
0
255
So here the color is copied.
Is there a way to force a parameter to be copied or referenced? Just like the & operator in C++?
No, parameters in Lua are always passed by value (mirror). All variables are references however. In your second example in editColor you're overriding what the variable col refers to, but it's only for that scope. You'll need to change things around, maybe instead of passing in a variable to be reassigned, have the function return something and do your reassignment outside. Good luck.
This will do what you want. Put the variable that you want to pass by reference into a table. You can use a table to pass anything by ref, not just a string.
-- function that you want to pass the string
-- to byref.
local function next_level( w )
w.xml = w.xml .. '<next\>'
end
-- Some top level function that you want to use to accumulate text
function top_level()
local w = {xml = '<top>'} -- This creates a table with one entry called "xml".
-- You can call the entry whatever you'd like, just be
-- consistant in the other functions.
next_level(w)
w.xml = w.xml .. '</top>'
return w.xml
end
--output: <top><next\></top>
Lua is a bad language. Suffer its simplicity when you need to do something just a little complex.

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