Estoy usando lienzo para mostrar algunos sprites, y tengo que voltear uno horizontalmente (de modo que mire hacia la izquierda o hacia la derecha). Sin embargo, no puedo ver ningún método para hacerlo con drawImage
.Voltear un sprite en lienzo
Aquí está mi código relevante:
this.idleSprite = new Image();
this.idleSprite.src = "/game/images/idleSprite.png";
this.idleSprite.frameWidth = 28;
this.idleSprite.frameHeight = 40;
this.idleSprite.frames = 12;
this.idleSprite.frameCount = 0;
this.draw = function() {
if(this.state == "idle") {
c.drawImage(this.idleSprite, this.idleSprite.frameWidth * this.idleSprite.frameCount, 0, this.idleSprite.frameWidth, this.idleSprite.frameHeight, this.xpos, this.ypos, this.idleSprite.frameWidth, this.idleSprite.frameHeight);
if(this.idleSprite.frameCount < this.idleSprite.frames - 1) { this.idleSprite.frameCount++; } else { this.idleSprite.frameCount = 0; }
} else if(this.state == "running") {
c.drawImage(this.runningSprite, this.runningSprite.frameWidth * this.runningSprite.frameCount, 0, this.runningSprite.frameWidth, this.runningSprite.frameHeight, this.xpos, this.ypos, this.runningSprite.frameWidth, this.runningSprite.frameHeight);
if(this.runningSprite.frameCount < this.runningSprite.frames - 1) { this.runningSprite.frameCount++; } else { this.runningSprite.frameCount = 0; }
}
}
Como se puede ver, estoy usando el método drawImage
dibujar mis sprites a la lona. La única forma de voltear un objeto que puedo ver es voltear/rotar todo el lienzo, que no es lo que quiero hacer.
¿Hay alguna manera de hacerlo? ¿O tendré que hacer un nuevo sprite mirando hacia el otro lado y usar eso?
Gracias! Pero debido a la respuesta de Simon, usaré su técnica. Aunque marcaré la tuya como la respuesta aceptada, ya que está respondiendo mi pregunta :) –