Javascript-форум (https://javascript.ru/forum/)
-   Ваши сайты и скрипты (https://javascript.ru/forum/project/)
-   -   Поворот за мышкой в Canvas (https://javascript.ru/forum/project/63641-povorot-za-myshkojj-v-canvas.html)

Camad 20.06.2016 02:39

Поворот за мышкой в Canvas
 
Здравствуйте ребята!
Помогите.
Пытаюсь сделать поворот в игре, игра сама рабочая вот решаил апгрейдом заняться.
Но хоть убей не поворачивается(
Возможно ошибка в маленькой глупости но я уже запарился и почти в первый раз перестал искать и решил написать.
Вот часть скрипта:
var igrok = new Player
//Сразу как получаю угол:
   igrok.angle = Math.atan2(my - igrok.y-(igrok.height / 2), mx - igrok.x-(igrok.width/ 2));

//Спасайте!!
//Что у меня не так?
//В цикле запускается функция 
igrok.update()

function Player(){
  this.x = 44
  this.y = 44
  this.width = 42
  this.height = 42
  this.angle=0;
  this.bcan=document.createElement('canvas');
  this.bcan.width=42;
  this.bcan.height=42;
  this.ctx=this.bcan.getContext('2d');      
  this.ctx.beginPath();
  this.ctx.fillStyle='rgb(88,96,184)';
  this.ctx.lineWidth=2;
  this.ctx.strokeStyle='black';
  this.ctx.fillRect(0,0,this.width,this.height);
  // this.ctx.arc(17,17,15,0,2*Math.PI, true);
  this.ctx.stroke();
  this.ctx.fill();
  this.bcan1=document.createElement('canvas');
  this.bcan1.width= gameWidth;
  this.bcan1.height= 5;
  this.ctx1=this.bcan1.getContext('2d');      
  this.ctx1.beginPath();
  this.ctx1.fillStyle='red';
  this.ctx1.lineWidth=2;
  this.ctx1.strokeStyle='black';
  this.ctx1.fill();
  this.hppolos = gameWidth
  Player.prototype.draw = function(){
  this.ctx1.clearRect(0, 0, gameWidth, this.bcan1.height)
  this.ctx1.fillRect(0, 0, this.hppolos, 5)
  this.ctx.translate(this.x + this.width/2,this.y + this.height/2);
   this.ctx.rotate(this.angle)
    ctxPl.drawImage(this.bcan,this.x,this.y);
    ctxPl.drawImage(this.bcan1,0,gameHeight-5);
  }
  Player.prototype.update = function(){
    if(this.x < 0){this.x = 2}
    if(this.x > gameWidth - this.width){this.x = gameWidth - this.width - 2}
    if(this.y < 0){this.y = 2}
    if(this.y > gameHeight - this.height){this.y = gameHeight - this.height - 2}
  this.chooseDir()
    this.draw()
    }
  Player.prototype.chooseDir = function(){
var prov
    oborona.forEach(function(enemy) {
      if (collides(enemy, igrok)) {
      	// console.log(1)
        // prov = true
      }
    })
   

    this.draw()
  }
    }
}

this.bcan1=document.createElement('canvas');
Полоска жизни

clearRect()
Находится в цикле обновления

Camad 20.06.2016 02:43

this.bcan1=document.createElement('canvas');
Полоска жизни


Часовой пояс GMT +3, время: 09:08.