me.ObjectEntity should not require an image
In some cases you will want to draw your sprite using the canvas draw functions rather than using an image. Due to this me.ObjectEntity should not require settings.image to be set.
me.SpriteObject requires the image. I guess it doesn't make sense using a SpriteObject if it doesn't have an image to draw.
The canvas context operations can be kind of slow. (Remember? https://groups.google.com/forum/?fromgroups=#!topic/melonjs/pOPf2f1BBnE ) For practical performance reasons, you would want to draw the sprites only once, then reference it through settings.image as a bitmap, anyway.
In this case there is only one on the screen at a time, and it is always a different size. It can't be a bitmap. I'm drawing a circle with a border to display the range of a tower.
Another example would be creating a particle system explosion rather than using a sprite sheet for an explosion. This is also on my todo list.
That's fine. Create a new object for each area with a different size. Regardless, you will always want to draw your sprite to a "bitmap" (an image) that can be drawn quickly ... unless it actually animates at 60fps.
Here's an example for your static range circle:
var CanvasEntity = me.SpriteObject.extend({
init : function (x, y, r, settings) {
settings.image = document.createElement("canvas");
settings.image.width = (r + 2) * 2;
settings.image.height = (r + 2) * 2;
settings.spritewidth = (r + 2) * 2;
settings.spriteheight = (r + 2) * 2;
var ctx = settings.image.getContext("2d");
ctx.fillStyle = "rgba(0, 192, 32, 0.75)";
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.arc(r + 2, r + 2, r, 0, Math.PI*2);
ctx.fill();
ctx.stroke();
this.parent(x, y, settings, settings.spritewidth, settings.spriteheight);
}
});
var range = new CanvasEntity(x, y, 100, {});
me.game.add(range, this.z);
me.game.sort();
The reason you don't want to do all of this context stuff in the draw method is because the draw method typically runs on every frame, and those operations can easily get a lot slower than a single context.drawImage call, which is what me.SpriteObject eventually does for you.
Also notice that I've extended SpriteObject, because you don't need any of the higher-level functionality provided by AnimationSheet or ObjectEntity! Namely, image-based animations and physics. You only need an image, and a position to draw it.
If you're interested in doing vector animations, you shouldn't even be extending me.SpriteObject. You should instead extend me.Rect and set a few important properties in the constructor, like this.visible = true;
Cool cool.
I'm getting the following error with the code above when I click on a tower to display the range:
TypeMismatchError: The type of an object is incompatible with the expected type of the parameter associated to the object [Break On This Error]
this.width, this.height);
http://greghoustondesign.com/sandbox/game-issue/
Ah! I made a small mistake. The last line of the init method should be:
this.parent(x, y, settings.image, settings.spritewidth, settings.spriteheight);
Sweet. Works like a charm, and now I understand why I haven't ever been able to get a SpriteObject to work. I was trying to pass the properties to them like an EntityObject, but they are a little different.
Cheers!
Yeah, they are a bit different. The inheritance chain hides a lot of the implementation details. Keep in mind, you can always look through the melonJS source code to spot these kinds of things.
I'll close this ticket now.