На спрайтах отрисован персонаж, смотрящий вправо. Нужно чтобы при движении влево, он начинал смотреть в левую сторону, как этого добиться без изменения спрайтов? Какие махинации сделать с канвасом, чтобы он не повернул спрайт, а именно отзеркалил его? Код:
const players = {
first:{
x:SIZE*2,
y:(HEIGHT/SIZE-2)*SIZE,
width: SIZE-9,
height: SIZE-9,
color:'maroon',
weight: 0.05,
ableToJump: false,
direction:'right',
states:{
jump:{
image: {
left:firstImages.jump,
right:firstImages.jump
},
frame: 0,
countFrames: 1,
},
idle:{
image: firstImages.idle,
frame: 0,
countFrames: 11
},
current:{
image: firstImages.jump,
frame: 0,
countFrames: 1,
},
},
collision:{
top: false,
bottom:false,
left:false,
right:false
},
gravity:{
y:GRAVITY
},
speed: {
x:0,
y:0
},
draw()
{
ctx.save();
ctx.beginPath();
// ctx.fillStyle = this.ableToJump?this.color:'green';
// ctx.fillRect(this.x,this.y,this.width,this.height);
if(this.ableToJump)
{
ctx.save()
var rad = 180 * Math.PI / 180;
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
ctx.rotate(rad);
// ctx.drawImage(this.states.idle.image,this.x,this.y,this.width,this.height);
ctx.drawImage(this.states.idle.image,this.states.idle.frame*32,0,32,32,this.x-10,this.y-10,this.width+10,this.height+10);
if(FRAME%3==0)
{
this.states.idle.frame = this.states.idle.frame+1>=this.states.idle.countFrames?0:this.states.idle.frame+=1;
}
}
else{
ctx.drawImage(this.states.jump.image.left,0,0,32,32,this.x-10,this.y-10,this.width+10,this.height+10);
}
ctx.closePath();
ctx.restore();
},
update(){
let ghostPlayer = copyObject(this);
if(BUTTONS.w && ghostPlayer.ableToJump)
{
if(ghostPlayer.abletoDoubleJump)
{
ghostPlayer.abletoDoubleJump = false;
ghostPlayer.speed.y = -25;
}
ghostPlayer.ableToJump = false;
ghostPlayer.speed.y = -25;
}
if(BUTTONS.s)
{
}
if(BUTTONS.a)
{
ghostPlayer.direction='left';
ghostPlayer.speed.x = -6;
}
if(BUTTONS.d)
{
ghostPlayer.direction='right';
ghostPlayer.speed.x = 6;
}
ghostPlayer.x = ghostPlayer.x + ghostPlayer.speed.x;
ghostPlayer.y = ghostPlayer.y + ghostPlayer.speed.y + ghostPlayer.gravity.y;
ghostPlayer.collision.top = ghostPlayer.collision.bottom = ghostPlayer.collision.left = ghostPlayer.collision.right = false;
ghostPlayer = collisionWithCanvas(ghostPlayer);
ghostPlayer = collisionWithBlocks(this,ghostPlayer);
ghostPlayer = calculateCollisions(ghostPlayer);
this.x = ghostPlayer.x; this.y = ghostPlayer.y;
this.speed.x = ghostPlayer.speed.x; this.speed.y = ghostPlayer.speed.y;
this.collision = ghostPlayer.collision; this.ableToJump = ghostPlayer.ableToJump;
this.direction = ghostPlayer.direction;
this.gravity.y = ghostPlayer.gravity.y;
this.speed.y = this.speed.y*0.85;
this.speed.x = this.speed.x*0.85;
console.log(this.direction);
}
}
};
Не знаю, нужен ли тут код...
ctx.scale(-1, 1)
если по горизонтали отзеркалить надо. Конкретно по вашему коду разбираться надо))