2016-08-17 12 views
1

キャンバスとJSで小惑星を再現しようとしています。角度に基づいて船を回転させるのに問題があります。私は角度を計算し、ラジアンに変換するための私の方法をテストしました。それらはチェックアウトし、draw()で呼び出されると角度が定義されますが、回転は起こっていません。私はいくつかの回転を得ることができます、それは全体のコンテキストではなく、船を回転させます。キャンバスコンテキストを回転させずにイメージを回転して再描画する方法

私は船級クラス全体を含めましたが、関連する関数は底部の描画関数です。

function Ship(posOptions) { 
    let options = {game: posOptions['game'], color: 'green', pos: posOptions['pos'], radius: 20, vel: [0,0], wrappable: true, type: 0} 
    MovingObject.call(this, options); 
    this.facingDir = 0; 
    this.H = window.innerHeight; //*0.75, 
    this.W = window.innerWidth; //*0.75; 
    this.xc = this.W/2; //zeby bylo w centrum :v 
    this.yc = this.H/2; //jw. 
    this.x = this.xc; 
    this.y = this.yc; 
    this.dv = 0.2; 
    this.dt = 1; 
    this.vx = 0; 
    this.vy = 0; 
    this.maxVel = 10; 
} 

Utils.inherits(Ship, MovingObject); 

//Relocate ship if hit by asteroid. 
Ship.prototype.relocate = function() { 
    this.pos = this.game.randomPosition(); 
    this.vel = [0,0]; 
}; 


Ship.prototype.fireBullet = function() { 
    let bulletVel = [(this.vel[0] * 4), (this.vel[1] * 5) - 10]; 
    let bullet = new Bullet({pos: this.pos, vel: bulletVel, game: this.game}); 
    this.game.bullets.push(bullet); 
}; 

//Calculate velocity based on keyboard input and angle of rotation 

Ship.prototype.power = function (impulse) { 
    if (impulse[0] === -2 && this.facingDir !== Math.PI) { 
    this.facingDir <= Math.PI ? this.facingDir += 30/180 * Math.PI : this.facingDir -= 30/180 * Math.PI; 
    } 
    if (impulse[0] === 2 && this.facingDir !== 0) { 
    this.facingDir <= 0 ? this.facingDir += 30/180 * Math.PI : this.facingDir -= 30/180 * Math.PI; 
    } 
    if (impulse[1] === 2 && this.facingDir !== (Math.PI/2 * 3)) { 
    this.facingDir <= (3 * Math.PI/2) ? this.facingDir += 30/180 * Math.PI : this.facingDir -= 30/180 * Math.PI; 
    } 
    if (impulse[1] === -2 && this.facingDir !== (Math.PI/2)) { 
    this.facingDir <= (Math.PI/2) ? this.facingDir += 30/180 * Math.PI : this.facingDir -= 30/180 * Math.PI; 
    } 
    this.vel[0] += impulse[0]; 
    this.vel[1] += impulse[1]; 
}; 


Ship.prototype.move = function() { 
    if (this.isWrappable) { 
    this.pos = this.game.wrap(this.pos); 
    } 

    this.pos[0] += this.vel[0]; 
    this.pos[1] += this.vel[1]; 
    //Attenuate the velocity over time so it doesn't accelerate forever. 
    this.vel[0] *= .98; 
    this.vel[1] *= .98; 
}; 

Ship.prototype.convertToRadians = function(degree) { 
    return degree*(Math.PI/180); 
} 

Ship.prototype.draw = function (ctx) { 
    const img = new Image(); 
    img.onload = function() { 
    ctx.drawImage(img, this.pos[0]-this.radius, this.pos[1]-this.radius) 
    }; 
    img.src = 'galaga_ship.png'; 
    ctx.drawImage(img, this.pos[0]-this.radius, this.pos[1]-this.radius); 

    **//This is where I am stuck.** 
    ctx.save(); 
    ctx.translate(this.x,this.y); 
    ctx.rotate(this.facingDir); 
    ctx.translate(-7,-10); 
    ctx.restore(); 

}; 

module.exports = Ship; 

答えて

1

コンテキストを復元する前に何も描画していません。 ctx.save()ctx.restore()の方法は、元の回転と原点の位置を保存し、それを変形してから元の位置に戻して保存するという方法です。したがって、図面のみが変換された座標系を使用し、それ以外はすべて正常に動作します。

**//This is where I am stuck.** 
    ctx.save(); 
    ctx.translate(this.x,this.y); 
    ctx.rotate(this.facingDir); 
    ctx.translate(-7,-10); 
// PUT DRAWING YOUR SHIP HERE 
    ctx.restore(); 
関連する問題