Issue Resolved, any comments on best practice would be welcome
I'm working on a JS (and JQuery) Battleships game and have function 'dropBoat' that allows a user to place a ship on a board. The details of this function are not important (it takes board and boatLength as arguments) however I'm unable to reuse it to place a second boat.
I'm trying to achieve this chain of events:
dropBoat function called --> dropBoat function called --> dropBoat function called --> StartPlay function called. Here's a sketch of my current attempt.
var dropBoat = function(board, length, callback){
//function code here
$(document).keydown(function(e){
if(e.which == "38"){
//more function code here
callback()
}
}
}
var dropThirdBoat = dropBoat(board1, 3, startPlay);
var dropSecondBoat = dropBoat(board1, 2, dropThirdBoat);
var dropFirstBoat = dropBoat(board1, 3, dropSecondBoat)();
this seems to work
var dropThirdBoat = function(){
dropBoat(board1, 3, startPlay);
}
var dropSecondBoat = function(){
dropBoat(board1, 2, dropThirdBoat);
}
var dropFirstBoat = dropBoat(board1, 3, dropSecondBoat);
var dropBoat = function(board, length, callback){
//function code here
$(document).keydown(function(e){
if(e.which == "38"){
//more function code here
callback();
}
}
}