как получить координаты мышки и связать их с персонажем, чтобы персонаж двигался мышкой

240
12 февраля 2020, 00:30

class Mause { 
    constructor(x, y) { 
        this.x = x; 
        this.y = y; 
        this.life = 5; 
        this.directions = []; 
    } 
    getNewcordinates(){ 
        this.directions = [ 
            [this.x - 1, this.y - 1], 
                [this.x    , this.y - 1], 
                [this.x + 1, this.y - 1], 
                    [this.x - 1, this.y    ], 
                    [this.x + 1, this.y    ], 
                    [this.x - 1, this.y + 1], 
                    [this.x    , this.y + 1], 
                    [this.x + 1, this.y + 1] 
               ]; 
             
    } 
    chooseCell(character) { 
        this.getNewcordinates(); 
        var found = []; 
        for (var i in this.directions) { 
            var x = this.directions[i][0]; 
            var y = this.directions[i][1]; 
            if (x >= 0 && x < matrix[0].length && y >= 0 && y < matrix.length) { 
                if (matrix[y][x] == character) { 
                    found.push(this.directions[i]); 
                } 
            } 
        } 
        return found; 
    } 
    mul() { 
        let emptyCells = this.chooseCell(0); 
        let newCell = random(emptyCells); 
 
        if (newCell) { 
 
            let x = newCell[0]; 
            let y = newCell[1]; 
 
            // matrixi mej gru mem MEK ->  
            matrix[y][x] = 2; 
 
            let a = new Mause(x, y); 
            fishArr.push(a); 
 
            this.life = 0; 
        } 
    } 
    eat() { 
        let emptyCells = this.chooseCell(1); 
        let newCell = random(emptyCells); 
 
        if (newCell) { 
            this.life++; 
            let x = newCell[0]; 
            let y = newCell[1]; 
 
            matrix[y][x] = 2; 
            matrix[this.y][this.x] = 0; 
 
            for (let i in fishArr) { 
                if (fishArr[i].x == x && fishArr[i].y == y) { 
                    fishArr.splice(i, 1) 
                } 
            } 
 
            this.x = x; 
            this.y = y; 
 
            if (this.life >= 10) { 
                this.mul(); 
            } 
        } else { 
            this.move() 
        } 
    } 
    move() { 
        this.life--; 
 
        let emptyCells = this.chooseCell(0); 
        let newCell = random(emptyCells); 
 
        if (newCell) { 
            let x = newCell[0]; 
            let y = newCell[1]; 
 
            // matrixi mej gru mem MEK -> 1 
            matrix[y][x] = 2; 
            matrix[this.y][this.x] = 0; 
 
            this.y = y; 
            this.x = x; 
        } 
        if (this.life < 0) { 
            this.die(); 
        } 
    } 
    die() { 
        matrix[this.y][this.x] = 0; 
 
        for (let i in fishArr) { 
            if (fishArr[i].x == this.x && fishArr[i].y == this.y) { 
                fishArr.splice(i, 1) 
            } 
        } 
    } 
}

READ ALSO
Поиск по условию

Поиск по условию

Имеется массив объектов ценУ каждого объекта цены есть её значение и условие применения, которое зависит от количество заказанных продуктов...

273
Вебсокеты и bitfinex

Вебсокеты и bitfinex

Хочу использовать вебсокеты для получения курса валют с биржи битфинексНо в браузере получаю ошибку

261
Фиксированная сетка в chartist.js

Фиксированная сетка в chartist.js

Фиксированную сетку задаю вот так:

258