can I do "js using c++ function to add some sprites to layer created in c++ function"


#1
void Jackie::HelloWorld::addNum()
{
//this funtion is to add rand sprite to layer
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    int N = rand() % 10;
    char src[64]="";
    sprintf(src,"add/d%d.png",N);
    CCSprite* sprite = CCSprite::create(src);
    sprite->setScale(0.5);
    sprite->setPosition(ccp(visibleSize.width/2 + origin.x + N, visibleSize.height/2 + origin.y + N));
    this->addChild(sprite);

    // and I binding this funtion to js
    cocos2d::CCLog("JSBinding Test");
    js_proxy_t * p = jsb_get_native_proxy(this);
    jsval retval;
    JSContext* jc = ScriptingCore::getInstance()->getGlobalContext();

    jsval v[] = {
        v[0] = int32_to_jsval(jc, 22),
        v[1] =UINT_TO_JSVAL(38)
    };

    ScriptingCore::getInstance()->executeFunctionWithOwner(OBJECT_TO_JSVAL(p->obj), "callback", 2, v, &retval);

}

void Jackie::HelloWorld::addSprite_js(CCObject* pSender)
{
// this funtion is called when I click a menu item,and I run script hello.js
    CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
    ScriptingCore::getInstance()->runScript("hello.js");    

    //CCScriptEngineManager::sharedManager()->removeScriptEngine();
}


//code of js like

require("jsb.js");


cc.dumpConfig();

var director = cc.Director.getInstance();

cc.log("This is print by js!");

/*
var sprite = cc.Sprite.create("arrow-up.png");
sprite.setScale(0.5);
sprite.setPosition(cc.p(100, 100));
director.getRunningScene().getChildByTag(99).addChild(sprite);
*/

cc.log("=====Here we go JSB function=====");
var hello = new Jackie.HelloWorld();
hello.callback = function (i, j) {
    cc.log("hello.callback " + i + j);
};
hello.retain();
hello.addNum();
hello.release();
cc.log("=====Here we end JSB function====");

***h1. well,when I run the test case,and click add sprite menu item to add sprite,the console log is


JS: This is print by js!

JS: =Here we go JSB function=

js JSB lsleafsoar constructor …
JSBinding Test
JS: hello.callback 2238

JS: =Here we end JSB function
+
but , sprite did not added to the layer?

who knows? or am I right? or what i said in title can not achieved?

Thank you,as well!


#2

I have solved this demo. I changed this js code like

cc.log("=====Here we go JSB function=====");
var hello = Jackie.HelloWorld.create();
hello.callback = function (i, j) {
    cc.log("hello.callback " + i + j);
};

hello.addNum();
var myScene = cc.Scene.create();
myScene.addChild(hello);
director.replaceScene(myScene);
cc.log("=====Here we end JSB function====");

then, I saw the sprite .