As I continue to expand my content on Phaser ce I have come around to expanding mu posts centered around the various components that are used with display objects in phaser ce. These components add features to display objects like events, and animation. In this post I will be covering the core component and what it brings to a display object, such as a sprite or text object.
This is a post on the core component in phaser ce that brings some properties and functions to just about all display objects. All display objects use this core module, but all of its features may not be functional depending on the type of display object. For example the core module brings the animations property to a display object, but if the display object does not also include the animation component, then animations will not work. This is the case with Graphics objects, the animations property is there, but an instance of the animation manager is not there and the value is undefined.
In this post I am using phaser community edition 2.11.1 of phaser, and not the later phaser 3 major release. As such code in this example will likely break in phaser 3. As long as phaser 2.x is still supported I will continue to expand and revise my 2.x content.
In this section I will be covering some examples that make use of the properties that are added via the core component.
All of these examples make use of features that are present in display objects such as sprites, and as such many of them need a sprite sheet to be skinned with something if I am not getting anything otherwise with the __default key sprite. So for some of the examples I am making use of a simple mkSheet helper that will make a sprite sheet via the 2d canvas drawing api.
So the core component does bring an animations property to a display object, however not all display objects support animation. In the event that the animations component is not used by the display object, then the value of the animations property will be undefined. So although the animations property is listed as part of the core component it is not really of any use unless the display object aslo includes the animation component as well.
I will not be getting into the ins and outs of the animation manager in detail here, however I have wrote a post on the animation manager a while back. As long as you stick to sprites, rather than graphics then the animation manager will also be there at the ready. When using graphics There might be a need to handle animation in a different manager that involves calling a draw method on each frame tick.
The components property is an Object where each key is the name of a component, and the value is a true boolean value if the display object supports the given component. This can be used to find out if a certain kind of display object supports a given component or not.
One of the most important properties that is added to a display object with the core component is the exists property. If this property is set to true then the display object will be removed from the game loop, and it will no longer be viable. So It is very useful for projects where a pool of sprites are being used and they need to be removed, and placed back into the game world given certain conditions.
So I did not get to all of the properties and methods that are used in the core component just yet. I will need to write some more code examples in order to do this subject justice. If you have any questions, or would like for me to give this post another look let me know in the comments. Feedback is a factor I take into concentration when it comes to updating content. In any case thanks for reading.