How to load Cocos2dx Sprite?

Hi I am just wondering, how do you load a single sprite? For example:

auto sprite1 = Sprite::create(“assets/sprite1.png”);

I am just learning Cocos2dx, please do not link me on tutorials. They are very not helpful so far.

Thank you.

Tutorials are your friends and are your very best chance to actually learn Cocos2dx.

To you posted question, what do you mean, “load a single sprite” specifically?

The code you posted looks correct to load the sprite into memory, if you have the png in place.

To render it to the screen, you’ll need to add it to the scene as well.

I agree that tutorials are helpful, but only if we’ll written. But if you haven’t found a tutorial you like, have you tried looking at the wiki?

http://www.cocos2d-x.org/wiki/How_To_Create_Sprites

Thanks for the reply. So the code snippet works? I am actually coding on Visual Studio 2013 Express and wanted to make a Cocos2dx app for Windows Desktop. This is just for learning purposes. I tried the tutorials and I play around with the code provided by the template and it always crash on this line:

addChild(sprite,1);

I believe we cannot add sprite since the function that loads the sprite returns nullptr. It crashes due to assert.

. I believe it can’t seem to find the image. I really need help. Any ideas? I am using Cocos2dx 3.3 Visual Studio 2013 Express.

Thank you.

2 problems:

You’re doing
addChild(sprite,1);
while your variable is sprite1

Secondly, the above shouldn’t be problem because ide would notify or it won’t compile. Crashing is problem with run time where your image is not getting located…
So, instead of

Try just “sprite1.png”

Hi Thanks, I did both and I still got the same error. I don’t know what causes it. I will try to create a new project and try it again. Thanks for helping.

Hi Thanks, I did both and I still got the same issue. I don’t know what causes it because it looks perfectly fine. I will try to create a new project and try it again. Thanks for correcting my typo. It is indeed sprite1.png.

Thanks for helping.

For me tutorial and sample are really important to learn.

I’m sorry as I don’t know if you have the basis, so I just try to explain it.
I work on VS 2012 Express so it should be the same.

On VS, you have to add existing file and then add your “sprite1.png” on the project.
This sprite should stay in the upper folder ‘Res’ in case one day you want to work on an other target.

Then your code should be:

auto sprite1 = Sprite::create("sprite1.png");

If sprite1 is nullptr here, don’t go further, you have a problem here and have to explain what you do.

And if it is good, I never use directly addChild function, I always use:

this->addChild(sprite1);

I hope it can help you to continue your test, but a good tutorial or some sample you are interested to should be a good read.

Hi Thanks for heads up. I will definitely try your suggestion, I will be posting back if ever I can’t still make it to work.

Hi I am still having the error. It crashes everytime I create a sprite that loads a file on folder “Content/imagenamehere” I really need help now :(.

I also notice from the generated template this line:
auto sprite = Sprite::create(“HelloWorld.png”);

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

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

Do you have idea where the heck did this project load that HelloWorld.png from? I am just wondering.

Also I don’t know if I am doing it on the right scope. I just tinkering with the given template, haven’t got that far, all I wanted is to create a sprite that will be loaded from a particular folder structure. I add a sprite on HelloWorld::init() scope, see the code below:

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

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 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
auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);

/////////////////////////////
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = LabelTTF::create("Hello World", "Arial", 24);

// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

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

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));

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

    // Just copied the same concept as the line that comes before this, but it crashes here
auto sprite1 = Sprite::create("Content/earth.png");
this->addChild(sprite1);

return true;

}

Maybe I forgot to setup the correct root directory? How I am going to do that? The code thats comes before that line shows that it loads HelloWorld.png from a folder I don’t even know where. Any ideas how can I change to my preferred directory?

Thanks in advance.

I finally made it to work. I forgot to say I am using Cocos2dx and if ever someone come across this kind of problem. YOu have to put your assets to Resource folder. For instance, I have a project created and named as SampleGame1. Inside this folder contains numerous project template for different platforms(iOS, Android) along with those is project folder named Resource, put all the game content here.

The reason for this is that, if ever you wanted to port your game, you will not have ‘COPIES’ of content on every template folder. Make sense though.

I’ve searched my computer for that file and found it there. This will lead me to my next question, how can I set up a different file location? Any ideas?