2015-12-30 45 views
1

我正在創建一個戰艦遊戲來學習node.js.在app.js文件中,我創建了兩個基於名爲Users的模塊的玩家,這個模塊導入了一個名爲Board的模塊。在Board模塊內部,我有一個名爲placeShip的函數。我如何從app.js訪問這個placeShip函數?因爲它是我得到一個TypeError:Player1.placeShip不是一個函數。在node.js中,如何從另一個模塊中的模塊中的app.js訪問函數?

用戶模塊:

var User = function() { 
var board = require('./Board'); 
return { 
    Username: "", 
    Gender: "", 
    Player: "", 
    Turn: "", 
    Ships: { 
     Carrier: ['C','C','C','C','C'], 
     Battleship: ['B','B','B','B'], 
     Cruiser: ['Z','Z','Z'], 
     Submarine: ['S','S','S'], 
     Destroyer: ['D','D'] 
    }, 
    Board: new board, 
    Hit: function (ship,position) { 
     var x = this.Ships[ship][position]; 
     if(x != null && x != undefined) { 
      this.Ships[ship][position] = 'X'; 
     } 
    }, 
    OutputAll: function() { 
     for (var item in this) { 
      if (item == "Ships") { 
       console.log("Ships: "); 
       for (var ship in this.Ships) { 
        console.log(" " + ship + ": " + this.Ships[ship]); 
       } 
      } else if(item != "Hit" && item != "OutputAll" && item != "Board") { 
       console.log(item + ": " + this[item]); 
      } 
     } 
    } 
} 
} 

module.exports = User; 

板模塊:

var GameBoard = function() { 
return { 
    Yours: createArray(10), 

    Mine: createArray(10), 

    ClearBoards: function() { 
     this.Yours = createArray(10); 
     this.Mine = createArray(10); 
    }, 

    DisplayBoard: function(board){ 
     for(var i in board){ 
      console.log(board[i]); 
     } 
    } 
} 
} 

function createArray(length) { 
var table = new Array(length); 

for (var i = 0; i < length; i++) { 
    table[i] = new Array(length); 
    // Make each space a 0 
    for (var row = 0; row < length; row++) { 
     table[i][row] = 0; 
    } 
} 
return table; 
} 

function placeShip(ship,rowStart,rowEnd,colStart,colEnd) { 
var letter; 

//=====Get Ship Letter====== 
for (x = 0; x < ship.length; x++) { 
    if (ship[x] != 'X') { 
     letter = ship[x]; 
     break; 
    } 
} 

//=====Get Orientation======= 
// Ship is horizontal 
if (rowStart === rowEnd) { 
    // Put the ship letter where it lies 
    for (x = colStart; x <= colEnd; x++) { 
     this.Board.Mine[rowStart][x] = letter; 
    } 
} 
// Or Ship is vertical 
else if (colStart === colEnd) { 
    // Put the ship letter where it lies 
    for (x = rowStart; x <= rowEnd; x++) { 
     this.Board.Mine[x][colStart] = letter; 
    } 
} 
// Or Ship is diagonal 
else { 
    // Put the ship letter where it lies 
    this.Board.Mine[rowStart][colStart] = letter; 
    for (x = 0; x < ship.length; x++) { 
     this.Board.Mine[rowStart + 1][colStart + 1] = letter; 
    } 
} 
} 

module.exports = GameBoard; 
module.exports.placeShip = placeShip; 

app.js:

var http = require('http'); 
var fs = require('fs'); 
var Users = require('./public/Scripts/Users'); 

var Player1 = new Users; 
var Player2 = new Users; 

// When a user navigates to the site 
function onRequest(request, response){ 
console.log("User made a " + request.method + " request from " + request.url); 

Player1.Username = "Jamie"; 
Player1.Hit("Carrier", 4); 
console.log("Player 1 Board:========"); 
Player1.Board.DisplayBoard(Player1.Board.Mine); 
Player1.placeShip(Player1.Ships.Carrier,0,4,0,0); 
Player1.Board.DisplayBoard(Player1.Board.Mine); 


console.log("Player 2 Board:========"); 
Player2.Username = "Kimyl"; 
Player2.Board.DisplayBoard(Player2.Board.Yours); 
console.log("Player 1: " + Player1.OutputAll()); 
console.log("Player 2: " + Player2.OutputAll()); 

// If user asks for the home page 
if(request.method == 'GET' && request.url == '/') { 
    console.log('Successfully requested Home Page'); 

    // Write a header response 
    response.writeHead(200, {"Content-Type": "text/html"}); 

    fs.createReadStream("./public/index.html").pipe(response); 
} else{ 
    send404Error(response); 
} 
} 

// 404 Error 
function send404Error(response){ 
// Write a header response 
response.writeHead(200, {"Content-Type": "text/plain"}); 
response.write("The page you are looking for could not be found!"); 
response.end(); 
} 

http.createServer(onRequest).listen(3000); 
console.log("Server running..."); 
+0

不知道,但嘗試用'board.js'代替'./board' –

+0

我可以訪問電路板模塊,因爲我可以做Player1.Board.DisplayBoard,它給我看板子。一旦我添加了placeShip功能它是一個不行。 – Peavey2787

+0

嘗試在主js文件中要求板模塊。 –

回答

1

我們需要在用戶模塊中使用電路板模塊,所以最好的方法是將電路板模塊作爲參數傳遞給用戶,並要求在主文件app.js

app.js:

var board = require("./public/scripts/board"); 
var Users = require("./public/scripts/Users")(board); 

用戶模塊

var User = function(board){ 
    return{ 
    Board: new board; 
    } 
} 

移動董事會的功能,改變你的this.Board.Mine內placeShip功能this.Mine

+0

欲瞭解更多詳情,請參閱此[問題](http://stackoverflow.com/questions/13039825/how-to-use-one- module-feature-within-another-module-in-nodejs-require) –

+0

我可以像使用Player1.DisplayBoard()一樣在用戶模塊中使用主板模塊,它向我展示了主板。我不能做的是訪問板子模塊中的函數placeShip。 – Peavey2787

+0

我試過移動Board功能裏面的一個屬性來做功能,但是後來我得到了placeShip沒有定義的 – Peavey2787

0

app.js可以創建一個裁判child1,等等的child2此Ref將允許對孩子的功能呼叫...

child1可以通過fireing事件調用它的兄弟姐妹中的一個功能:app.js還可以在child1觸發的事件,等的child2

window.addEventListener('child1.event1', function() { $ref.child2.func2(options)} 

的傳遞調用鏈是「聽」 (app.js)

+0

如何在app.js中引用child 2?從我的理解,我需要來電者成爲一名球員,以便玩家的董事會得到更新 – Peavey2787

相關問題