Note: this is the fifth part of the apricot.js tutorial. Please refer to the sidebar for the previous tutorials.
Now comes the fun part of game development: creating the in game entities. These objects are event driven, which means when certain things happen to them they respond is whatever you tell them to. Examples of events include: being created, colliding with something, and alarm going off, or being clicked on. Alright, let’s get down to the code. Before we can put an object in our game we must tell the framework what we want the object to behave like. To do this we rely on inheriting the apricot object class. I’m going to stop talking now and show you the code, and then I will explain it afterward.
test.prototype = new obj("type");
Alright, so what this chunk does is instantiate a new object called test (in line 1). Line 2 then tells the browser that we want the test object to have all the methods and members of the obj class. This will allow us to use all of the functions included in the obj class on our new object. Finally, the word “type” in the parenthesis is what type of object we want to make. This is mostly used for doing things to all of one kind of object, or checking collisions with a certain kind of object. Basically, the framework will treat all objects of the same type. This would be useful, for instance, if you have a bunch of different color and shape walls, but they all interact with the player in the exact same way, you might want to give them all the same type, even if they are different objects.
The next topic is creating events. We define the events with the following syntax:
objectName.prototype.eventName = function() {
Let’s use the create event as a sample. We made a test object earlier, but when an object is created it has an empty sprite. We can use the create event to give it a sprite.
test.prototype.create = function(){
this.sprite=new sprite("image.png");
This code tells the framework to create a new sprite and assign it as this object’s image whenever this particular entity is created.
Remember! Place the image.png file in the same directory as your .html file and apricot.js.
Note: This uses the “this.sprite” property of the object class, which we have not talked about yet. The object class contains a lot of properties you can change, and the sprite property is one of them. The important thing to understand is that whatever you assign to this.sprite will be drawn when the object is drawn.
Now we can put together everything we have learned and begin to make our game. As of now your code should look like this:
<script language="JavaScript" SRC="apricot.js"></script>
<script type="text/javascript">
window.onload = gameStart;
apricotInit("can", 300, 300);
setInterval(gameLoop, 1000/30);
test.prototype = new obj("test");
test.prototype.create = function(){
this.sprite=new sprite("image.png",1);
<canvas id="can">your browser is too lame for canvas!</canvas>
Right now opening this in a browser should not do anything. This is because we have not added any instances of our test object to the game world. To add one of our test objects we need to use the new_obj() function included in apricot.js. The syntax is as follows:
new_obj(new objectToAdd(), x, y);
The first argument is what new object we want to add to the game. The next arguments are where we want to the object positioned. So to add our object we would simply replace the objectToAdd part with our objects name (In this case “test”) and replace x and y with the desired position. This code would go wherever you wanted to create the object, in pour case, this would be in the gameStart() function after we initialized the apricot engine.
new_obj(new test(), 150,150); //place a new test object at (150,150)
The final thing we should do before running the game is deal with gameLoop function. Right now nothing happens in this function. We want it to update the game and draw all the objects to the screen. To do this we call the draw() and update() functions. Change the gameLoop function to look like this:
This tells apricot to update all of the objects and then draw them to the screen.
Tada! You now have everything you need to begin! If you open this file in a web browser it should display your object in the center of the screen.