About Lazy Layer¶
Lazy layer is a concept for games that doesn't need to redraw a static background image every frame. If a static background image gets drawn at the same rate as the game, we will not only waste valuable system resources, but also uses more electricity and therefore polluting the planet.
That is why, we the team of Cocos2d-html5 came up with the concept of Lazy Layer to save system resources and to save our planet!
- The Theory Short Version:
The theory is to split the game into multiple canvas elements, so some of them can be refreshed at slower rate, which is perfect for many static game elements such as the background.
- The Theory Long Version:
When a lazy Layer is initialized, it creates a div element and place the game <canvas> element inside, it also creates another <canvas> element to represent the Lazy Layer and places it behind the main <canvas>. Lazy Layers also has a flag called "isNeedUpdate", the layer will only redraw if the flag is true. The flag will set to true if any of the child of the lazy layer is transformed
- How to use Lazy Layer:
Lazy Layer is not only lazy, it is also very simple to use. All you need to do is initialize a LazyLayer object, and add it to a scene, then you can add sprite to the lazy layer just like any other layer.
For example://Create a new LazyLayer and attach it to the scene var lazyLayer = new cc.LazyLayer(); this.addChild(lazyLayer); //Create a sprite and attach it to the lazy layer this.pSprite = cc.Sprite.spriteWithFile("Resources/HelloWorld.png"); this.pSprite.setPosition(cc.ccp(cc.Director.sharedDirector().getWinSize().width / 2, cc.Director.sharedDirector().getWinSize().height / 2)); lazyLayer.addChild(this.pSprite, 0);
- Final note:
There is one last thing you need to know before you use Lazy Layer to save the planet, because Lazy Layer is designed to use as background layer, setting its Z order greater than 0 will result in an error, as you don't really want your lazy layer to block mouse or touch events for your main game layer.
This is all you need to know about Lazy Layer, Have fun and go save our planet!