英文:
In Java: how to 'pickup' an element of a container and 'place' it in another container?
问题
我在探索如何创建一个基于文本的游戏,并且我正在尝试处理在房间之间移动的部分。我已经解决了这一部分,并且创建了一个非常详细的地图,你可以输入“go north”等命令。唯一的问题是,游戏非常枯燥,我想加入物品的概念。为了创建房间,我做了以下的代码:
```javascript
var rooms = {
"room0": {
"description": "这里是描述",
"directions": {
"direction": "新房间"
},
"items": {
"item1": "物品1",
"item2": "物品2"
}
}
}
因此,我想如果我创建一个名为“items”的单独元素,然后在房间中列出物品,就像我在“directions”中所做的一样。我编写了以下代码将物品添加到玩家的物品栏中:
function grabItem(item) {
if (inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
inventory.append(item);
//在此处插入代码以从房间中移除物品
}
}
所以!重要的问题是,我可以使用什么方法来移除单个物品?
<details>
<summary>英文:</summary>
I'm exploring how to create a text-based game, and I was tinkering around with moving between rooms. I have this part down and a very elaborate map created where you can type, "go north" etc. The only thing is that the game is very dry, and I wanted to incorporate items. To make the rooms I did this:
```javascript
var rooms = {
"room0":{
"description": "description here",
"directions":{
"direction": "newRoom"
}
}
}
So, I was thinking if I made a separate element called "items" and then list the items in that room, the way I did with "directions". I wrote this code to add items to the player inventory:
function grabItem(item){
if(inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
inventory.append(item);
//INSERT CODE HERE TO REMOVE THE ITEM FROM THE ROOM
}
}
SO! The big question is, what method can I use to remove an individual item?
答案1
得分: 0
Sure, here's the translated code:
function grabItem(item){
if(inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
inventory.append(rooms[currentRoom].items[item]);
rooms[currentRoom].items[item] = undefined;
}
}
英文:
function grabItem(item){
if(inventory.length < 12 && rooms[currentRoom].items[item] !== undefined) {
inventory.append(rooms[currentRoom].items[item]);
rooms[currentRoom].items[item] = undefined;
}
}
NB java!==javascript
专注分享java语言的经验与见解,让所有开发者获益!
评论