在Java中:如何从容器中“拾取”一个元素并将其“放置”在另一个容器中?

huangapple 未分类评论67阅读模式
英文:

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&#39;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, &quot;go north&quot; 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 = {
        &quot;room0&quot;:{
               &quot;description&quot;: &quot;description here&quot;,
               &quot;directions&quot;:{
               &quot;direction&quot;: &quot;newRoom&quot;
               }
         }
     }

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 &lt; 12 &amp;&amp; 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 &lt; 12 &amp;&amp; rooms[currentRoom].items[item] !== undefined) {
            inventory.append(rooms[currentRoom].items[item]);

            rooms[currentRoom].items[item] = undefined;
        }
      }

NB java!==javascript

huangapple
  • 本文由 发表于 2020年4月7日 05:29:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61069279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定