2012-09-25 38 views
0

我想创建一个随机产生一个桌面策略游戏我与我的朋友玩,基本上用户选择什么派系来创建它(然后这会改变可用的名字)并选择它是国王还是国王(根据当前选择的阵营和等级更改标题)。试图从数组中检索数据取决于用户输入,JS/JQuery

该脚本还会随机生成忠诚度,财富和影响力(游戏中需要的统计数据)。目前发生的情况是显示统计数据,有时会将NaN而不是预期随机数的影响关闭,它还会显示未定义未定义的未定义,而不是CurNit的CurTitle CurLordName。 我通过JSlint运行它,只有事情告诉我它应该在列5而不是列9等,所以我很难过。

// King/Lord creator 
// when starting a campaign all kings are NPCs, also some lords are npcs. 
//Global Variables 
var iFaction, isKing;// faction in numbers (used for calculations)i and is character lord or king? 1 = king 2=lord 
var curLordname, curTitle, curNation; //Current lord name (For output), Current title (For output) and Current nation (For output) 
var iModder; // Number to add to the random math 
var curWealth; // Lord wealth (random number 200 - 1000) 
var curLoyalty; //Lord Loyalty (random number between 1-10) 
var curInfluence; //Lord Influence ((random number 1-10 + wealth/100)/2) 
// King Titles 

var aTitles = new Array(); 
aTitles[1] = "King"; 
aTitles[2] = "High Chief"; 
aTitles[3] = "Anax"; 
aTitles[4] = "Emperor"; 
// Lord Titles 
aTitles[5] = "Baron"; 
aTitles[6] = "Jarl"; 
aTitles[7] = "Stratigos"; 
aTitles[8] = "Warlord"; 

// Lords names, Leontrois (1-25), Saxathia(26-50), Hellios(51-75), Drakon(75-100) 
var aNames = new Array(); 
aNames[0] = "Maverick"; 
//Leontrois Names (Math.floor((Math.random()*25)+1);) 
aNames[1] = "Henry"; 
aNames[2] = "Edward"; 
aNames[3] = "Thomas"; 
aNames[4] = "George"; 
aNames[5] = "Stephen"; 
aNames[6] = "William"; 
aNames[7] = "James"; 
aNames[8] = "Louis"; 
aNames[9] = "Charles"; 
aNames[10] = "David"; 
aNames[11] = "Oliver"; 
aNames[12] = "Duncan"; 
aNames[13] = "Leonardo"; 
aNames[14] = "Phillip"; 
aNames[15] = "Francis"; 
aNames[16] = "Johnathan"; 
aNames[17] = "Richard"; 
aNames[18] = "Robert"; 
aNames[19] = "Jacob"; 
aNames[20] = "Peter"; 
aNames[21] = "Patrick"; 
aNames[22] = "Arthur"; 
aNames[23] = "Isaac"; 
aNames[24] = "Laurence"; 
aNames[25] = "Edmund"; 
// Saxathia Names (Math.floor((Math.random()*25)+26);) 
aNames[26] = "Mordra"; 
aNames[27] = "Horsa"; 
aNames[28] = "Offa"; 
aNames[29] = "Sigfryd"; 
aNames[30] = "Coelwulf"; 
aNames[31] = "Sledda"; 
aNames[32] = "Harold"; 
aNames[33] = "Ulfryk"; 
aNames[34] = "Ongar"; 
aNames[35] = "Ragnar"; 
aNames[36] = "Kren"; 
aNames[37] = "Merkar"; 
aNames[38] = "Canute"; 
aNames[39] = "Kealin"; 
aNames[40] = "Echbert"; 
aNames[41] = "Aethelred"; 
aNames[42] = "Ulfred"; 
aNames[43] = "Bryn"; 
aNames[44] = "Godwin"; 
aNames[45] = "Cin"; 
aNames[46] = "Oswine"; 
aNames[47] = "Mul"; 
aNames[48] = "Cnut"; 
aNames[49] = "Aelthor"; 
aNames[50] = "Gryff"; 
//Hellios Names (Math.floor((Math.random()*25)+51);) 
aNames[51] = "Alexander"; 
aNames[52] = "Ajax"; 
aNames[53] = "Telemachus"; 
aNames[54] = "Odysseus"; 
aNames[55] = "Antinous"; 
aNames[56] = "Marcus"; 
aNames[57] = "Julius"; 
aNames[58] = "Nero"; 
aNames[59] = "Constantine"; 
aNames[60] = "Achillies"; 
aNames[62] = "Agamemnon"; 
aNames[63] = "Menelaus"; 
aNames[64] = "Nestor"; 
aNames[65] = "Laertes"; 
aNames[66] = "Priam"; 
aNames[67] = "Hector"; 
aNames[68] = "Paris"; 
aNames[69] = "Diomedes"; 
aNames[70] = "Patroclus"; 
aNames[71] = "Glaucus"; 
aNames[72] = "Sarpedon"; 
aNames[73] = "Idomeneus"; 
aNames[74] = "Eurymachus"; 
aNames[75] = "Amphinomus"; 
//Drakon names (Math.floor((Math.random()*25)+76);) 
aNames[76] = "Xiahou"; 
aNames[77] = "Cao Yi"; 
aNames[78] = "Sun Li"; 
aNames[79] = "Ma Tao"; 
aNames[80] = "Jin Yuan"; 
aNames[81] = "Huang"; 
aNames[82] = "Zhong"; 
aNames[83] = "Fei Song"; 
aNames[84] = "Chan Yin"; 
aNames[85] = "Dong Xa"; 
aNames[86] = "Pang Shao"; 
aNames[87] = "Keiji"; 
aNames[88] = "Reiji"; 
aNames[89] = "Matsuhide"; 
aNames[90] = "Samonuske"; 
aNames[91] = "Nobunaga"; 
aNames[92] = "Ieyasu"; 
aNames[93] = "Minghuan"; 
aNames[94] = "Yisun"; 
aNames[95] = "Erdene"; 
aNames[96] = "Tolui"; 
aNames[97] = "Kenji"; 
aNames[98] = "Masamune"; 
aNames[99] = "Hassan"; 
aNames[100] = "Al'Tar"; 
//End of lord names 

// First what nation are you from: 
$("#selNation").change(function() { 
iFaction = $(this).val(); 
    switch (iFaction) { 
    case 1: 
     curNation = "Leontrois"; 
     iModder = 1; 
     break; 
    case 2: 
     //iFaction=2; 
     curNation = "Saxathia"; 
     iModder = 26; 
     break; 
    case 3: 
     //iFaction=3; 
     curNation = "Hellios"; 
     iModder = 51; 
     break; 
    case 4: 
     //iFaction = 4; 
     curNation = "Drakon"; 
     iModder = 76; 
     break; 
    } 
//$("#selLord").disabled = false; 
}); 

$("#selLord").change(function() { 
    isKing = $(this).val(); 
    switch (isKing) { 
    case 1: 
     curTitle = aTitles[iFaction]; 
     // switch(iFaction){ 
       // case 1: 
       // curTitle = aTitles[1]; 
       // break; 
       // case 2: 
       // curTitle = aTitles[2]; 
       // break; 
       // case 3: 
       // curTitle = aTitles[3]; 
       // break; 
       // case 4: 
       // curTitle = aTitles[4]; 
       // break; 
      //} 
     break; 
    case 2: 
     var temp = iFaction + 4; 
     curTitle = aTitles[temp]; 
      // switch(iFaction){ 
       // case 1: 
       // curTitle = aTitles[5]; 
       // break; 
       // case 2: 
       // curTitle = aTitles[6]; 
       // break; 
       // case 3: 
       // curTitle = aTitles[7]; 
       // break; 
       // case 4: 
       // curTitle = aTitles[8]; 
       // break; 
      // } 
     break; 
    } 
//$("#lordbtn").disabled = false; 
}); 

function createlord() { 
//get lord name 
    var temp2 = Math.floor(curWealth/100) + 1, temp3 = Math.floor(((Math.random() * 10) + temp2)/2), temp4 = Math.floor((Math.random() * 25) + iModder); 
    curInfluence = temp3; 
    curLoyalty = Math.floor((Math.random() * 10) + 1); 
    curWealth = Math.floor((Math.random() * 800) + 200); 
    curLordname = aNames[temp4]; 
//display result 
    var supastring = "<h3>" + curTitle + " " + curLordname + " of " + curNation + ". </h3>" + "<br /> <p>His wealth is " + curWealth + "Gp, His influence is " + curInfluence + " and his loyalty is " + curLoyalty + ".</p>" + "<br /> <button class='btn btn-large' type='button' onclick='resetlord()'>Create another Lord</button>"; 
//$("#lordresult").innerHTML = supastring; 
    document.getElementById("lordresult").innerHTML = supastring; 
} 
function resetlord() { 
// $("#lordresult").innerHTML 
//document.getElementById("lordresult").innerHTML 

     document.getElementById("lordresult").innerHTML = "<h2>Broken History: Lord Creator tool</h2> <small>This tool is designed to help you when playing Broken History 1.0 the CWDC Expansion, and is used to determine the name of a new NPC lords (on the death of a previous lord). Note: 'You' refers to the character being created.</small><br /> <p>Choose faction and rank then click button to generate the new NPC.<br /></p><label>What faction are you from?</label><select id='selNation'><option selected='selected' disabled='disabled'>Select faction:</option><option value='Leontrois' >Leontrois</option><option value='Saxathia'>Saxathia</option>  <option value='Hellios'>Hellios</option><option value='Drakon'> Drakon</option>  </select><label>Are you the Monarch of a faction?</label><select id='selLord'> <option selected='selected'>Choose rank:</option><option value='1' >Monarch</option><option value='2'>Vassal</option> </select> <button class='btn btn-large' type='button' id='lordbtn' onclick='createlord()'>Generate new lord</button></p>"; 
    } 

下面是所得屏幕截图: Screenshot

+0

你就不是偶然有这个的jsfiddle? – zero

+0

Here Zero:http://jsfiddle.net/CYe8J/但我不认为它在那里正常工作,似乎没有工作,以及在我的测试网页:/ –

回答

1

把你的事件处理文档准备事件的内部代码:

$(document).ready(function() { 
    $("#selNation").change(function() { 
     .... 
    }); 

    $("#selLord").change(function() { 
     ..... 
    }); 
}); 

iFaction = $(this).val();但你相对于一个号码,你应该强制类型转换这是一个数:

iFaction = Number($(this).val()); // parseInt also will work 

最后,您使用curWealth而未定义。

更改这一部分:

var temp2 = Math.floor(curWealth/100) + 1, temp3 = Math.floor(((Math.random() * 10) + temp2)/2), temp4 = Math.floor((Math.random() * 25) + iModder); 
    curInfluence = temp3; 
    curLoyalty = Math.floor((Math.random() * 10) + 1); 
    curWealth = Math.floor((Math.random() * 800) + 200); 

这样的:首先在使用它之前定义的值

curWealth = Math.floor((Math.random() * 800) + 200); 

var temp2 = Math.floor(curWealth/100) + 1, temp3 = Math.floor(((Math.random() * 10) + temp2)/2), temp4 = Math.floor((Math.random() * 25) + iModder); 
    curInfluence = temp3; 
    curLoyalty = Math.floor((Math.random() * 10) + 1); 

这里是你的工作fiddle

+0

真棒,但标题选择仍然不起作用,现在它只是说'国王'时,它应该根据排名/派系 –

+0

改变,它只是从ifaction = 1的名称/标题没有任何其他:/ –

+0

东西我想过,是否只有在DOM就绪触发器发生时才会执行'$(document).ready()'中的所有内容?这是否意味着一旦执行它不能再做?如果是这样,也许我可以调用文档中的函数。准备好,而不是用一些方法在那里写出来? –

1

selNation变化然后

$("#selNation").change --> set variable "var curNation, iModder, iFaction" 

selLord改变然后

$("#selLord").change --> set variable "var curTitle" 

Createlord功能

function createlord() { 


    curLoyalty = Math.floor((Math.random() * 10) + 1); 
    curWealth = Math.floor((Math.random() * 800) + 200); 

    // selNation change set 
    curNation = "Leontrois"; 
    iModder = 1; 
    iFaction = 1; 

    // selLord change 
    curTitle = aTitles[iFaction]; 




    var temp2 = Math.floor(curWealth/100) + 1, 
     temp3 = Math.floor(((Math.random() * 10) + temp2)/2), 
     temp4 = Math.floor((Math.random() * 25) + iModder); 



    curInfluence = temp3; 
    curLordname = aNames[temp4]; 

    var supastring = curTitle + " " + curLordname + " of " + curNation + curWealth + curInfluence + curLoyalty; 

} 

createlord(); 
+0

不知道你明白的目的这个脚本 –

1

一个错误是错误iFaction变量,您的以下代码:

switch (isKing) { 
    case 1: 
     curTitle = aTitles[iFaction]; 

应该是这样的一个:

switch (isKing) { 
    case 1: 
     curTitle = aTitles[isKing]; 
+0

不是这样,这个开关的设计是根据派别/等级给出不同的结果,开关(isKing){'通过案例1(王)和2(附属)排序,这些内容开关就像这样ifaction = 1到4取决于选择的阵营。 –

+0

在aTitles数组中有条目1到8,所以如果char是国王,他的头衔将等于代表他的派系的数字(即1 = leontrois = King,2 = Saxathia =高级酋长),第二种情况是特定的开关使用这个代码'var temp = iFaction + 4; \t \t curTitle = aTitles [temp];'这在理论上的工作就好像是行动+4 =第5-8位(封臣职位) –

相关问题