简体   繁体   中英

Using string as name of variable

Hey I'm relatively new to javascript programming and I'm working on a javascript game and I need help with this script:

a = ();          //a is the number of the quest completed e.g. 3 would be Quest3 
Quest1 = "Hello1"
Quest2 = "Hello2"
Quest3 = "Hello3"
Quest4 = "Hello4"
Quest5 = "Hello5"
Quest6 = "Hello6"
Quest7 = 0         //This is just a placeholder for the end of the list

This is a script for a list of quests in chronological order of when they were recieved. That the player needs to complete. When a quest is completed it is removed from the list and the list is moved up to fill in the space The way I was doing this was, even though there is probably a better way to do it (If anyone knows of a better way let me know). So say if Quest3 was completed then the following code would execute to make the list move up starting at the quest completed:

Quest3 = Quest4;
Quest4 = Quest5;
Quest5 = Quest6;

That script is starting at Quest3, but I need that script to be able to start at any point and I wanted to use 'a' for that. So if a = 4 then the script would start at Quest4:

Quest4 = Quest5
Quest5 = Quest6

This script would go backwards if a new quest was added so, since new quests added will always go to Quest1 then the following script would execute:

Quest6 = Quest7  //Get rid of quest 6
Quest5 = Quest4
Quest4 = Quest3
Quest2 = Quest1
Quest1 = "The New Quest"

So the maximum amount of quests the player could have could be 6 (much higher in the actual game so the player never reaches the limit.

So if anyone knows how to have the starting point for the script to add a new quest, dynamic and or a better way to move the list up of down to fill in the gap, let me know.

you should consider using an array for storing the quests instead assigning variables one by one.

use shift to remove a task at the beginning of the array and unshift to append the task at the beginning of the array.

you may refer https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array for the usage of array objects.

This sure sounds like you should put all the quests into an array and use the index of the array to move forward or backward in the list of quests.

There is no reason to use multiple numbered variables like you have. An array will lead to much more efficient code.

You can either move items up/down in the array with array.shift() or array.unshift() or you can just keep track of an index of the array as your current set point and increment or decrement the index to move around.

You should use Arrays no matter what language you use in this case:

This is example in javascript:

var maxQuests = 6;
var quests = new Array();
var activeQuests = 0;

function addQuest(questName) {
    if (activeQuests >= maxQuests) return;
    activeQuests++;
    quests.push(questName);
}

function completeQuest(questName) {
    if (!quests.contains(questName)) return;
    activeQuests--;
    quests.splice(quests.indexOf(questName), 1);
}

http://jsfiddle.net/xguwk/

It's the way it should be programmed. You can change the number of quests as a parameter, you don't need to rewrite your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM