在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ 用canvas畫五子棋出現(xiàn)的問題

用canvas畫五子棋出現(xiàn)的問題

通過我的代碼當(dāng)我需要在canvas上下棋(描點(diǎn))時(shí),顯示(里面的4只是我點(diǎn)擊的點(diǎn)對(duì)應(yīng)的property)

Uncaught TypeError: Cannot set property '4' of undefined
at Gobang._next 
at HTMLCanvasElement.<anonymous> 

我的部分代碼如下:
html部分:

<div id="chessboard_container" class="chessboard-container hidden"></div>
<div id="chessboard_canvas_container" class="chessboard-canvas-container ">
    <canvas id="chessboard_bg_canvas" width="458" height="458"></canvas>
    <canvas id="chessboard_shadow_canvas" width="458" height="458"></canvas>
    <canvas id="chessboard_canvas" width="458" height="458"></canvas>
</div>

js部分:

function Gobang() {
        this._status = 0; // 棋局狀態(tài),0表示對(duì)戰(zhàn)中,1表示對(duì)戰(zhàn)結(jié)束
        this._role = 0; // 棋子顏色,0表示黑棋,1表示白棋
        this._chessDatas = []; // 存放下棋數(shù)據(jù)
        this._chessboardDatas = [];//存放棋盤數(shù)據(jù)            
        this._chessCanvas = document.getElementById('chessboard_canvas');
        this._chessContext = this._chessCanvas.getContext('2d');
    };
    
/**
     * 在canvas落子
     */
    Gobang.prototype._drawChessInCanvas = function(position,role) {
        var vm = this;
        if(position == undefined || position == null) return;
        var x = 4 + ((position % 15) + 0.5) * 30;
        var y = 4 + (parseInt((position / 15), 10) + 0.5) * 30;
        vm._chessContext.beginPath();
        vm._chessContext.arc(x, y, 13, 0, 2 * Math.PI);// 畫圓
        //定義棋子顏色
        if(role){
            vm._chessContext.fillStyle = "#FFF";
        }else{
            vm._chessContext.fillStyle = "#000";
        }
        vm._chessContext.fill();
        vm._chessContext.closePath();
    };

    /**
     * 落子
     */
    Gobang.prototype._drawChess = function(position) {
        var vm = this;
        if (position === undefined || position === null) return;          
        vm._drawChessInCanvas(position, vm._role);
    };
    
/**
     * 下一步棋
     */
    Gobang.prototype._next = function(position) {
        var vm = this;
        if(vm._hasChess(position)) return;
        vm._chessboardDatas[(position % 15)][parseInt((position / 15), 10)] = vm._role;
        vm._chessDatas.push(position);

        // 繪制棋子
        vm._drawChess(position,vm._role);
    };
    
    vm._chessCanvas.addEventListener('click', function(e) {
                var x = e.offsetX;
                var y = e.offsetY;
                var i = Math.floor((x - 4) / 30);
                var j = Math.floor((y - 4) / 30);
                var position = i + j * 15;
                if(vm._status == 0) {
                    vm._next(position);
                   
                    vm._role = 1 - vm._role;
                }
            }, false);
            
            

請(qǐng)問我的代碼如何改,才能正常實(shí)現(xiàn)下棋的效果。

回答
編輯回答
毀與悔

應(yīng)該
clipboard.png
這個(gè)是空的。

2018年7月14日 01:38