Developing TicToe HTML5/Implementing Client Entities: Difference between revisions

Content deleted Content added
imported>Hendrik Brummermann
imported>Hendrik Brummermann
Line 102:
== Generalizing the drawing code ==
 
We need very similar code to draw the crosses and noughts, that are placed by the player. Therefore we should move the drawing code from Gameboard up in the inheritance hierarchy to Entity. This, however, poses a problem: The name of the image file depends on the concrete object. We create a new method getImageName() to solve this issues. Depending on the concrete object, it will either return "board.png" or the name of the correct token image.
{{TODO|write this sub-section}}
 
<source lang="javascript">
marauroa.rpobjectFactory.entity.draw = function() {
if (typeof(this.img) == "undefined") {
this.img = document.createElement('img');
document.getElementsByTagName("body")[0].appendChild(this.img);
}
this.img.src = "images/" + this.getImageName();
this.img.style.position = "absolute";
this.img.style.zIndex = this.z;
this.img.style.left = this.x + "px";
this.img.style.top = this.y + "px";
}
 
 
/** gameboard and token */
marauroa.rpobjectFactory.gameboard.getImageName = function() {
return "board.png";
}
marauroa.rpobjectFactory.token.getImageName = function() {
return this.tokenType + ".png";
}
</source>
 
For simplicity, we ensure that the token-type matches the filename.
 
== Drawing players ==