Reza Ghobadinic

Creating Sprites With Image Files

The easiest way to create sprites is using some image file(s)!

I will create a new project called “sprite” and add some image files to the Resources folder in Xcode and create sprites from them. These are the original images that I used in my Breakanator game (A break out game which is not in the App Store anymore!). Although, in that game I used an image atlas (Sprite sheet) for performance reasons. We will see how that works in the next post.

 

Adding images to Xcode project

cocos new sprite -p com.games.sprite -l cpp -d ~/Projects/

ball.png, pad.png, block.png, brick_red.png, brick_blue.png

Now you should have the whole folder with those images in your project, under Resources.

We have our images in our project, so lets get to business and make some sprites.

 

Creating Sprites

The Sprite class has a “create” method that gets an image as an input and returns a Sprite pointer.

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    auto ball = Sprite::create("sprites/ball.png");
    ball->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2 - 100);
    this->addChild(ball);
    
    auto pad = Sprite::create("sprites/pad.png");
    pad->setPosition(origin.x + visibleSize.width / 2, 10);
    this->addChild(pad);
    
    auto block = Sprite::create("sprites/block.png");
    block->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
    this->addChild(block);

    auto brick_red = Sprite::create("sprites/brick_red.png");
    brick_red->setPosition(origin.x + visibleSize.width / 2 - 72, origin.y + visibleSize.height / 2);
    this->addChild(brick_red);

    auto brick_blue = Sprite::create("sprites/brick_blue.png");
    brick_blue->setPosition(origin.x + visibleSize.width / 2 + 72, origin.y + visibleSize.height / 2);
    this->addChild(brick_blue);

    return true;
}

As simple as that!

Basically, we created a sprite using the create() method and then set the position of the sprite and then parented it to the current scene.

Keep in mind that the Sprite class is inherited from the Node class. This means we can do everything that we could do with the Nodes that we created in the previous blog about Nodes. That is including parenting, position, rotation, scale changes and more…

 

Compile and Run

The result of running you code should be something like this on a 1024×768 design resolution on Mac desktop (not the simulator).

Notice that there are 5 GL calls in this scene, because of the 5 sprites that we created.

Download Source

There are other create functions in the Sprite class. One of them uses a Texture that is created using an Image instance.

    Image *image = new Image();
    image->initWithImageFile("ball.png");
    
    Texture2D *texture = new Texture2D();
    texture->initWithImage(image);
    
    Sprite *sprite = Sprite::createWithTexture(texture);
    this->addChild(sprite);

In the next post, we will create sprites from sprite frames in a sprite sheet! I’ll explain…

Next

Exit mobile version