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

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);

Related

Node-red - need a multi-input function for a number value

So I'm just getting to grips with node-red and I need to create a conditional global function.
I have two separate global.payloads set to a number value of either 0 or 1.
What I need to happen now is, if global.payload is equal to value 1 then follow this flow, if it is equal to value 0 then follow this one.
I'm just a little confused with the syntax for the function statement. Any help gratefully appreciated.
Since you haven't accepted the current answer, thought I'd give this a try.
I think this is what you need to handle inputs from two separate global contexts. I'm simulating them here with two separate inject nodes to demonstrate:
The checkconf inject node emits a 1 or a 0. Same for the meshstatus node. Substitute your real inputs for those inject nodes. The real work is done inside the function:
var c = context.get('c') || 0; // initialize variables
var m = context.get('m') || 0;
if (msg.topic == "checkconf") // update context based on topic of input
{
c = {payload: msg.payload};
context.set("c", c); // save last value in local context
}
if (msg.topic == 'meshstatus') // same here
{
m = {payload: msg.payload};
context.set('m', m); // save last value in local context
}
// now do the test to see if both inputs are triggered...
if (m.payload == 1) // check last value of meshstatus first
{
if (c.payload == 1) // now check last value of checkconf
return {topic:'value', payload: "YES"};
}
else
return {topic:'value', payload: "NO"};
Be sure to set the "topic" property of whatever you use as inputs so the if statements can discriminate between the two input. Good luck!
You can use the Switch node to do this, rather than a Function node.

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.

Matlab: Instantiate the handle class inside a function within another class

Let's say, for instance, I have two classes: A & B. I have set B as the handle class and would like a property from A to instantiate this class (i.e. B).
Therefore, I have done something like this in class A:
% Constructor
function a = A()
a.objB = B(); % This works fine
...
for i = 1:10
a.var(i) = B(); % This causes an error to occur
end
end
The error is listed below:
"Error using double Conversion to double from B is not possible.
The code snippet inside the for loop seems to work if I change a.var(i) = B(); to var(i) = B();.
Do you have any idea why this is?
Your .var field is probably initialized to a double when you make the assignment (maybe to []). Using a.var(i) = xxx cannot change the type of a.var.
Try resetting the value the first time it is used. EG
for i = 1:10
if i == 1
a.var = B(); % Overwrite the existing value
else
a.var(i) = B(); % Now append to that value
end
end
This will cause your a.var field to be reallocated every loop. Pre-allocated your array will make everything go much faster. The easiest way to pre-allocate is actually to just loop backwards, like this:
for i = 10:-1:1
if i == 10
a.var = B(); % Overwrite the existing value, allocated room for 10 elements
else
a.var(i) = B(); % Assign each element in turn, 9 through 1
end
end

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
}
}

AS3 Boolean seemingly not working

Ok, so this is obviously going to be something that I stupidly overlooked in my code, but I am having problems with a boolean check in as3. In the below if statement I set a boolean, I can confirm that the boolean is set in this if switch as I have run a trace to check that:
if(switchA && switchB){
if(Side == "LEFT"){
localAttachCoords.x = (-Parent.collision.SideLength - entity.collision.SideLength)/2
localAttachCoords.y = Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2)
}
if(Side == "RIGHT"){
localAttachCoords.x = (Parent.collision.SideLength + entity.collision.SideLength)/2
localAttachCoords.y = -(Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2))
}
if(Side == "UP"){
localAttachCoords.y = (Parent.collision.SideLength + entity.collision.SideLength)/2
localAttachCoords.x = -(Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2))
}
if(Side == "DOWN"){
localAttachCoords.y = (-Parent.collision.SideLength - entity.collision.SideLength)/2
localAttachCoords.x = Parent.collision.SideLength/2 - (((TargNode-1)*8) + entity.collision.SideLength/2)
}
entity.attached = true
entity.Parent = Parent
}
This would all be well and good, but for the fact that in a function from another class, executed every frame, claims that the boolean was set to false, I confirmed this with another trace function.
This is the function, taken from the class whose instance is referred to as entity in the above switch:
public function update(){
if (physics) physics.update()
if (node && physics){
trace(attached)
if(attached){
physics.nodeUpdate()
}
}
}
This function claims in the trace that attached == false despite it being set true earlier with no other reference to the attached variable. Any help would be appreciated!
Pathing
There are some un-addressed variables in your issue, foremost being the pathing you're taking to check your variable. This is relevant because of namespaces/scope affect what each piece of code has access to.
If your functions and variables shared the same space (i.e., global/document/timeline), then any reference the the same named variable will always return the same value, unless (as LoremIpsum noted) it's being shadowed by a local variable by the same name.
Obviously, this is not the case since you're using public function which is a class-only declaration. If the boolean you're looking for is on the timeline, and the class wants to read that variable, you need to have a valid path to it. Instantiated classes that are DisplayObjects and have been added to the DisplayList have both parent and stage properties which you can use to access the timeline global namespace (thereby providing access to your boolean).
However, if the class is not a DisplayObject (e.g., it does not extend a Sprite, Shape, or MovieClip), then access to the timeline has to be provided manually, either by setting a property on the class, or passing an argument to a method on the class.
Further complicating the matter is if the Boolean exists in another class object (either instantiated or static), you'd then need a way to get between them. A case of A sees B, C sees B, but neither A or C see eachother.
Values
A boolean is always going to be false, even if the assigned value was null, so if your class is trying to reference a variable that it can't see, that value will always be false. For example...
var foo:Boolean = this["fiddlesticks"];
trace("foo = " + foo); // traces: "foo = false"
There is no property this.fiddlesticks, so while the resolved value is null, foo becomes false. Consider using hasOwnProperty(), which indicates whether an object has a specified property defined, and is a method available to all objects.
Switch
You don't have to manually create your own switch using if then else if, AS3 has its own switch statement.
switch (Side) {
case "LEFT":
// Do stuff for left
break;
case "RIGHT":
// Do stuff for right
break;
case "UP":
// Throw your hands up
break;
case "DOWN":
// Get down and boogie!
break;
}
I hope that all helps. I'd like to say exactly what's going on with the access to your Boolean, but there simply isn't enough information to say.
Cheers!