unable to display sprite

unable to display sprite
0.0 0


I am using the HelloCpp sample for android with a small modification. I am calling a method from jni to display an image as a sprite. But instead of the image, all I see is a black rectangle in the center of the screen. Code is attached.

Found similar question here http://www.cocos2d-x.org/boards/6/topics/20519

#include “HelloWorldScene.h”
#include “SimpleAudioEngine.h”
#include <android/log.h>

using namespace cocos2d;
using namespace CocosDenshion;

static HelloWorld* m_pGameLayerSingleton = NULL;

CCScene* HelloWorld::scene()
// ‘scene’ is an autorelease object
CCScene *scene = CCScene::create();

if(m_pGameLayerSingleton NULL) {
m_pGameLayerSingleton = HelloWorld::create();

// add layer as a child to scene

// return the scene
return scene;


// on “init” you need to initialize your instance
bool HelloWorld::init()
// 1. super init first
if ( !CCLayer::init() )
return false;

// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage \*pCloseItem = CCMenuItemImage::create(
                                    menu\_selector(HelloWorld::menuCloseCallback) );
pCloseItem-\>setPosition( ccp(CCDirector::sharedDirector()-\>getWinSize().width - 20, 20) );

// create menu, it's an autorelease object
CCMenu\* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu-\>setPosition( CCPointZero );
this-\>addChild(pMenu, 1);

// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label
CCLabelTTF\* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

// ask director the window size
CCSize size = CCDirector::sharedDirector()-\>getWinSize();

// position the label on the center of the screen
pLabel-\>setPosition( ccp(size.width / 2, size.height - 20) );

// add the label as a child to this layer
this-\>addChild(pLabel, 1);

// add "HelloWorld" splash screen"
CCSprite\* pSprite = CCSprite::create("HelloWorld.png");

// position the sprite on the center of the screen
pSprite-\>setPosition( ccp(size.width/2, size.height/2) );

// add the sprite as a child to this layer
this-\>addChild(pSprite, 0);

return true;


void HelloWorld::menuCloseCallback(CCObject* pSender)


void HelloWorld::onInputReceived()
__android_log_write(ANDROID_LOG_VERBOSE, “HelloWorldScene”, “Received input”);//Or ANDROID_LOG_INFO, …

CCSprite* sprite = CCSprite::create(“google.png”);
//get director
CCDirector *director = CCDirector::sharedDirector();

// ask director the window size
CCSize size = director~~>getWinSize;
// position the sprite on the center of the screen
sprite~~>setPosition( ccp(size.width/2, size.height/2) );

m_pGameLayerSingleton->addChild(sprite, 0);

HelloWorldScene.cpp.zip (1.2 KB)

google.png (7.0 KB)


My first question for you is why do you create a static variable? Because you should know that every class that derived from CCObject is an auto release object. So eventually when your scene is out of scope every children from that scene will be automatically released. Even if you feel that your object is fine because you always check if it’s NULL, but it will always become NULL after released or exception occurred when you’re trying to access the variable. So that’s mean your singleton will become useless.


Thanks Terry,
I wanted to access the layer from a static method, so I created the Layer as static. I am passing some data from android’s activity and accordingly I need to make changes in the layer. Can you please suggest an efficient way to do this?


Ok, I understand what do you want. The idea is you could use CCDirector::sharedDirector~~>getRunningScene to get your scene . But I guess it would return a CCTransitionScene rather than your scene. So you should get your scene by using the getChildren function and use the first index of the array to get your scene. Then from there you could call your function. But you have to cast it first as your scene class to be able to call your function. Also inside your scene function you have to set tag for your layer.
// Get sharedDirector
CCDirector* director = CCDirector::sharedDirector;
// Get running scene
CCScene* currentScene = director-getRunningScene;
// But the currentScene is not your scene if we use scene transition , so you have to find it using getChildren and it should be the first child of the array.
MyScene* realScene = currentScene~~>getChildren()>objectAtIndex;
// Now you can get your layer, inside your scene function you should setTag your layer
CCLayer* myLayer = realScene

// In summary
MyLayer* myLayer = (MyLayer *) CCDirector::sharedDirector->getRunningScene()->getChildren()->objectAtIndex(0)>getChildByTag;

I hope you get the idea. Best thing is you should debug the code to get my approach is right or not.


Hi, am having the same problem. I have tried both approaches of saving a static HelloWorld instance and accessing the instance through CCDirector’s currently running scene.
Both approaches give me black sprites though. Did you manage to solve this?


I figured it out after consulting the EasyNDK code. All you have to do is instead of calling your onInputReceived function directly, call it using CCCallFunc instead.


`Richard_Foo good you figured it out. I had added the link to a similar question above http://www.cocos2d-x.org/boards/6/topics/20519. The problem is with the cycle of cocos2d-x rendering. So if you add a delay using a selector, it works fine.

scheduleOnce(schedule\_selector(PhotoPage::callbackPhotoDidSave), 0);@ wherecallbackPhotoDidSave` is the method that creates the sprite based on the image.


Yes, that link helped me to figure out the problem. An additional advantage of using CallFuncO or CallFuncND is that you can pass a parameter.