Templates

When we are creating objects for a game, templates are useful. A template is a blueprint for a certain type of object, when we want such an object we can use the same template to always get an equal object. The template will specify which modules that the object use and how the object’s attributes will be configured. Finally, a template can also define scripts, called triggers, linked to local events. When the local event occurs for an object of this type, the trigger is invoked.
Templates in Ophelia ware defined in XML files.
A template looks like this:

<!-- Always start with the Template tag-->
<Template>
	<!-- Give the template a name -->
	<Name>player</Name>
	<!-- Which components does this object use? -->
	<Components>
		<Image>true</Image>
		<Movement>true</Movement>
		<Collision>true</Collision>
	</Components>
	<!-- Which default attribute values does the object have? -->
	<Attributes>
		<name>player</name>
		<imageSrc>blue.bmp</imageSrc>
		<xCollisionSize>20</xCollisionSize>
		<yCollisionSize>20</yCollisionSize>
		<xAnchor>-20</xAnchor>
		<yAnchor>-20</yAnchor>
	</Attributes>
	<!-- Which scripts will trigger on which events? -->
	<Triggers>
		<onCollide>onPlayerCollide</onCollide>
		<onUpdate>onPlayerUpdate</onUpdate>
	</Triggers>
</Template>

To finish defining this object, we also need to define the scripts that make up the template’s triggers. This will be done in JavaScript and can be written anywhere in the project. It looks like this:

Ophelia.defineTrigger('onPlayerCollide', function(data){
	
	var player = data.collidingObject;
	// Code that is invoked when the player collides
});

Ophelia.defineTrigger('onPlayerUpdate', function(data){
	
	var player = data.updatedObject;
	// Code that is invoked when the game updates
});
Be Sociable, Share!

Published by

Albert Öhrling

Studies computer science in Skellefteå, at Lulea University of Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *