I hope to pass array from javascript to c*+
c*+
#include "MathCore.h"
MathCore::MathCore()
{
}
MathCore::~ MathCore()
{
}
float MathCore::cal (int n , float *myArray )
{
int i ;
float v=0;
for (i = 0; i < n ; i++) {
v=v+ myArray[i] ;
}
return v;
}
after binding
javascript code:
var t=new MyNameSpace.MathCore();
var aaa=[];
aaa.push(0.1);
aaa.push(0.2);
console.log(t.cal (2, aaa));
it always crashes at:
v=v+ myArray[i] ;
and says
EXEC BAD ACCESS
is there a common standard for passing array from javascript to c++?
Your comment welcome
Please use CCArray*
as parameter, bindings-generator doesn’t support float*
C Array.
James Chen wrote:
Please use CCArray*
as parameter, bindings-generator doesn’t support float*
C Array.
CCArray always has CCNode elements, how to link CCArray element to float or other struct data type?
CCArray could access CCObject
and its sub class, CCBool, CCInteger, CCFloat, CCDouble.
Please cast your type to CCFloat*
rather than CCFloat
. It’s the basic of cplusplus…… _!.
James Chen wrote:
Please cast your type to CCFloat*
rather than CCFloat
. It’s the basic of cplusplus…… _!.
I have try this before, but
cocos2d::CCFloat v=0;
cocos2d::CCFloat *p;
v=v+*p;
will report error and is different from processing of standard float
finally I noticed CCFloat has method getValue, and everything is ok
but another question comes, if I wrote
cocos2d::CCFloat MathCore::cal (int n , cocos2d::CCArray *a)
{
int i= a->count();
}
and code in js
var ttttt=new MyMathCore.MathCore();
var aaa=[];
aaa.push(0.1);
aaa.push(0.2);
cc.log(ttttt.cal (2, aaa));
int i= a->count();
will be 0 rather than I expected 2
What is wrong with my code?
lp43
#7
Is there really no convenience tool or class to send data from Javascript to C*+ or C*+ to Javascript?
For example,
Unity have a class called AndroidJNI that we can easily to call Java function from C# as below:
AndroidJNI.CallStaticVoidMethod(classID, methodID);