Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Getting the sprite's position and size


There is a certain size and position of the sprite. In this recipe, we explain how to view the size and position of the sprite.

How to do it...

To get the sprite position, use the following code:

Vec2 point = sprite->getPosition();
float x = point.x;
float y = point.y;

To get the sprite size, use the following code:

Size size = sprite->getContentSize();
float width = size.width;
float height = size.height;

How it works...

By default, the sprite position is (0,0). You can change the sprite position using the setPosition method and get it using the getPosition method. You can get the sprite size using the getContentSize method. However, you cannot change the sprite size by the setContentSize method. The contentsize is a constant value. If you want to change the sprite size, you have to change the scale of the sprite. You will learn about the scale in the next recipe.

There's more...

Setting anchor points

Anchor point is a point that you set as a way...