网格 AS3 中的平铺对象
我想将一组对象放入网格中。我知道代码很简单,但不知何故我没有得到我想要的。
代码:
const PADDING:Number = 10:
const COL:Number = Math.floor(Math.sqrt(tiles.length);
const ROW:Number = Math.floor(Math.sqrt(tiles.length);
for(var i:int = 0; i< COL; i++)
{
var tile:TileSprite = tiles[i];
tile.x = i * tile.width + PADDING;
for(var j:int = 0; j < ROW; j++)
{
tile.y = j * tile.height + PADDING;
}
}
注释:
- 数组在其他地方声明为公共变量
- 这是我正在构建的游戏引擎的一部分,所以我希望它尽可能灵活
- 磁贴读取 XML 文件的属性。但我不想使用 XML 对图块的位置进行硬编码。
预先感谢您的帮助。如果您需要更多说明,请告诉我。
I want to put an array of objects into a grid. I know the code is simple but somehow I'm not getting what I want.
Code:
const PADDING:Number = 10:
const COL:Number = Math.floor(Math.sqrt(tiles.length);
const ROW:Number = Math.floor(Math.sqrt(tiles.length);
for(var i:int = 0; i< COL; i++)
{
var tile:TileSprite = tiles[i];
tile.x = i * tile.width + PADDING;
for(var j:int = 0; j < ROW; j++)
{
tile.y = j * tile.height + PADDING;
}
}
Notes:
- The array is declared else where as a public variable
- This is to be part of a game engine I'm building so I want it as flexible as possible
- The tiles as reading their properties for an XML file. But I don't want to use the XML to hard code the tiles' positions.
Thanks in advance for your help. Let me know if you need any more clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议研究模运算符而不是嵌套循环。
这篇博文概述了如何做到这一点:
http://www. davidpett.com/actionscript-3-dynamic-rows-and-columns/
使用 David 的示例作为起点,您的代码将如下所示:
I would suggest looking into the modulus operator rather than nesting loops.
This blog post outlines how to do just that:
http://www.davidpett.com/actionscript-3-dynamic-rows-and-columns/
Using David's example as a starting point, your code would then look like this: