AS3 - 不确定它叫什么

发布于 2024-12-20 23:37:46 字数 306 浏览 0 评论 0原文

我正在编写一个涉及货物的游戏,并且我计划拥有大量的货物类型。目前我有一个 Cargo 类,一艘运载货物的船有一个它所持有的 Cargo 数组。

我真的不想让每艘船都装有一堆物体,因为我真正想知道的是每艘船有多少货物。尤其是当这些船只被大量创建和废弃时。

我确信我正在寻找的东西是如此基本,即使询问也会显得很愚蠢,但我知道有一些类似于包含对象和相关值的数组的东西。我想用它来引用静态数组中的 Cargo 类型,并保存数量。

它叫什么?我将如何使用它(即它有哪些常用功能)?一些代码片段和术语是理想的。

I'm writing a game that involves cargo, and I plan to have a large number of cargo types. Currently I have a Cargo class, and a ship carrying cargo has an array of the Cargos it is holding.

I'd really rather not have each ship with a bunch of objects when all I really want to know is how much of which cargoes each ship has. Especially when these ships will be created and discarded a lot.

I'm sure the thing I'm looking for is so basic I'll look dumb for even asking, but I know there's something similar to an array that holds an object and a related value. I want to use that to reference the Cargo type from the static array, and hold the quantity.

What's it called? How would I use it (ie what are common functions used for it)? Some code snippets and terminology would be ideal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-12-27 23:37:46

只需让您的货物类别包含 Quantity 属性即可。

或者您可以有一个 CargoType 类,其中包含有关货物类型的信息(即是否是一些食物、金钱或枪支)。然后您的 Cargo 类将具有 CargoType 属性和 Quantity 属性。

这样,您的船舶货物数组将只包含每种类型的货物,并且数量属性用于指示数量。

Just make your cargo class contain a Quantity property.

Or you could have a CargoType class, which contains information about the type of Cargo(i.e. whether its some food, or money, or guns). Then you Cargo class would have a CargoType property and a Quantity property.

This way your array for the ship's cargo would only have a Cargo of each type and the Quantity property is used to indicate how many.

懷念過去 2024-12-27 23:37:46

您可能正在寻找字典 类。它类似于数组(使用整数索引)或对象(可以用作基于字符串的关联数组)。字典使用对象作为映射到单个值的唯一键。

我认为你不需要一个。全局变量很少是一个好主意。我不会不必要地使这个复杂化,只是让每艘船处理自己的货物,也许使用 AaronLS 建议。但不要太担心这里的性能,即使你每帧创建和摧毁数千艘船,渲染它们也会比处理数组花费更多的时间。

无论如何,以下是如何使用字典以及需要考虑的一些事项。它没有太多特殊的方法,几乎​​像数组一样使用。

var dict = new Dictionary();
var key:MyClass = new MyClass(); // a key can be of any class 

dict[key] = "foo"; // set a value
trace( dict[key] ); // traces: foo

dict[key] = null; // set value to null, key is still there. It won't get garbage collected!
delete dict[key]; // remove the key

考虑使用新词典(true) 以避免垃圾收集问题。

You are probably looking for the Dictionary class. It's similar to arrays (which use integer indexes) or objects (which can be used as string based, associative arrays). A Dictionary uses objects as unique keys that get mapped to a single value.

I think you don't need one though. A global variable is rarely a good idea. I wouldn't unnecessarily complicate this and just let each ship handle it's own cargo, maybe by using a quantity as AaronLS suggested. But don't worry too much about performance here, even if you create and destroy thousands of ships each frame, rendering them will take significantly more time than the handling of arrays.

Anyway, here's how you use a dictionary and some things to consider. It doesn't have much special methods, it's used almost like an array.

var dict = new Dictionary();
var key:MyClass = new MyClass(); // a key can be of any class 

dict[key] = "foo"; // set a value
trace( dict[key] ); // traces: foo

dict[key] = null; // set value to null, key is still there. It won't get garbage collected!
delete dict[key]; // remove the key

Consider using new Dictionary(true) to avoid the garbage collection issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文