Game Development Reference
// After this call you must not switch back to layer2 (index: 1) anymore!
CCLayerColor and CCLayerGradient
In the ScenesAndLayers project, so far the background is simply a black screen. You
can see it when you scroll to the edge of the grassy background image or tap the “user
interface” to have the GameLayer zoom out. To change the background color,
cocos2d provides a CCLayerColor , which is added to the ScenesAndLayers02 pro-
ject and works like this:
// Set background color to magenta. The most unobtrusive color imaginable.
CCLayerColor* colorLayer = [CCLayerColor layerWithColor:ccc4(255, 0, 255, 255)];
In the same way, you can create a much more vibrant background using CCLayer-
Gradient . It allows you to specify two colors between which cocos2d will smoothly
interpolate. And you can adjust the gradient's direction.
CCLayerGradient* gradient = [CCLayerGradient layerWithColor:ccc4(0, 150, 255, 255)
fadingTo:ccc4(255, 150, 50, 255)
The gradient direction is given by a vector. In this case the vector points one unit to the
right and one unit up, which has the gradient rotated at a perfect 45° angle. The vector
points in the direction of the fadingTo color, which means the blue-ish color is at the
bottom left whereas the orange-ish color is more to the upper right. To reverse the ori-
entation of the color, you can either switch colors or simply reverse the signs of the
Note that you can also change the width and height of CCLayerColor and CCLay-
erGradient and thus the area they fill with a color:
[gradientLayer changeWidth:400 height:200];