官术网_书友最值得收藏!

Manipulating sprites

A Sprite is a 2D image that can be animated or transformed by changing its properties, including its rotation, position, scale, color, and so on. After creating a sprite you can obtain access to the variety of properties it has, which can be manipulated.

How to do it...

Rotate

You can change the sprite's rotation to positive or negative degrees.

sprite->setRotation(30.0f);

You can get the rotation value using getRotation method.

float rotation = sprite->getRotation();

The positive value rotates it clockwise, and the negative value rotates it counter clockwise. The default value is zero. The preceding code rotates the sprite 30 degrees clockwise, as shown in the following screenshot:

Rotate

Scale

You can change the sprite's scale. The default value is 1.0f, the original size. The following code will scale to half size.

sprite->setScale(0.5f);

You can also change the width and height separately. The following code will scale to half the width only.

sprite->setScaleX(0.5f);

The following will scale to half the height only.

sprite->setScaleY(0.5f);

The following code will scale that width to double and the height to half.

sprite->setScale(2.0f, 0.5f);
Scale

Skew

You can change the sprite's skew, either by X, Y or uniformly for both X and Y. The default value is zero for both X and Y.

The following code adjusts the X skew by 20.0:

sprite->setSkewX(20.0f);

The following code adjusts the Y skew by 20.0:

sprite->setSkewY(20.0f);
Skew

Color

You can change the sprite's color by passing in a Color3B object. Color3B has an RGB value.

sprite->setColor(Color3b(255, 0, 0));
Color

Opacity

You can change the sprite's opacity. The opacity property is set between a value from 0 to 255.

sprite->setOpacity(100);

The sprite is fully opaque when it is set to 255, and fully transparent when it is set to zero. The default value is always 255.

Opacity

Visibility

You can change the sprite's visibility by passing in a Boolean value. If it is false, then the sprite is invisible; if it is true, then the sprite is visible. The default value is always true.

sprite->setVisible(false);

Tip

If you want to check the sprite's visibility, use the isVisible method rather than the getVisible method. The sprite class does not have the getVisible method.

if (sprite->isVisible()) {
    // visible
} else {
    // invisible
}

How it works...

A Sprite has a lot of properties. You can manipulate a sprite using the setter and getter methods.

RGB color is a 3 byte value from zero to 255. Cocos2d-x provides predefined colors.

Color3B::WHITE
Color3B::YELLOW
Color3B::BLUE
Color3B::GREEN
Color3B::RED
Color3B::MAGENTA
Color3B::BLACK
Color3B::ORANGE
Color3B::GRAY

You can find them by looking at the ccType.h file in Cocos2d-x.

主站蜘蛛池模板: 金秀| 徐闻县| 天镇县| 伊川县| 来安县| 房山区| 康定县| 庆元县| 海口市| 临西县| 大姚县| 新蔡县| 衡水市| 文登市| 广元市| 岑巩县| 阿合奇县| 大田县| 米易县| 台东市| 秦皇岛市| 锡林浩特市| 平乐县| 双桥区| 九寨沟县| 石柱| 黄浦区| 龙南县| 濉溪县| 东光县| 尼勒克县| 佛山市| 禹城市| 枣庄市| 夏津县| 鸡泽县| 德阳市| 抚松县| 丁青县| 东莞市| 鄱阳县|