使用FlashDevelop和动态网格动作脚本2.0
我是动作脚本的新手。我想做的是模拟 2 车道交叉口附近的交通流,遵循 Wolfram 规则 184。首先,我尝试创建一个网格(8x8,其中交叉点位于中间两行和中间两行之间)列(如加号),其单元格具有以下属性:
color = white;
car = false;
when clicked:
color = red;
car = true (a car is present);
因此,在用户单击单元格以初始定位汽车并按下开始按钮后,模拟将开始。
到目前为止,这是我的代码(对格式不正确表示歉意):
class Main
{
private var parent:MovieClip;
public static function main(mc:MovieClip)
{
var app = new Main(mc);
}
public function Main(mc:MovieClip)
{
this.parent = mc;
//grid settings
var Cell:MovieClip = mc.createEmptyMovieClip("cell", mc.getNextHighestDepth());
var x:Number = 0;
var y:Number = 0;
var color:Number = 0xffffff;
var car:Boolean = false;
for (y = 0; y < 3 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8) - 5, (Stage.height / 8) - 5, color, 100);
}
}
for (y = 3*Stage.height/8; y < 5 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 0; x < Stage.width; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
for (y = 5*Stage.height/8; y < Stage.height; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
Cell.onMouseDown()
{
Cell.color = UI.RED;
Cell.car = true;
}
}
}
我知道这里有很多问题。首先,鼠标按下时单元格颜色不会改变。我需要为 for 循环中的每个单元格制作影片剪辑吗?我认为制作具有给定属性的对象网格会更容易,但我不知道该怎么做。如果有人帮助我,我将非常感激。
I'm new to actionscript. What I'm tryin to do is simulate traffic flow near a 2 lane intersection, following Wolfram's rule 184. To begin with, I'm trying to create a grid (8x8 of which the intersection is between the middle two rows and the middle two columns, like a plus sign) whose cells have the following attributes:
color = white;
car = false;
when clicked:
color = red;
car = true (a car is present);
So, after the user clicks cells to position the cars initially and presses the start button, the simulation will begin.
Here's my code so far (apologies for incorrect formatting):
class Main
{
private var parent:MovieClip;
public static function main(mc:MovieClip)
{
var app = new Main(mc);
}
public function Main(mc:MovieClip)
{
this.parent = mc;
//grid settings
var Cell:MovieClip = mc.createEmptyMovieClip("cell", mc.getNextHighestDepth());
var x:Number = 0;
var y:Number = 0;
var color:Number = 0xffffff;
var car:Boolean = false;
for (y = 0; y < 3 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8) - 5, (Stage.height / 8) - 5, color, 100);
}
}
for (y = 3*Stage.height/8; y < 5 * Stage.height / 8; y += Stage.height / 8)
{
for (x = 0; x < Stage.width; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
for (y = 5*Stage.height/8; y < Stage.height; y += Stage.height / 8)
{
for (x = 3*Stage.width/8; x < 5*Stage.width/8; x+=Stage.width/8)
{
UI.drawRect(Cell, x, y, (Stage.width / 8)-5, (Stage.height / 8)-5, color, 100);
}
}
Cell.onMouseDown()
{
Cell.color = UI.RED;
Cell.car = true;
}
}
}
I know there's quite a few things gone wrong here. First of all, the cell color doesn't change on mouse down. Do i need to make movie clip for each cell in the for loops? I think it would be easier to make a grid of objects with given attributes, but i don't know how to do that. Would really appreciate if someone helps me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您当前方法的问题在于,使用drawRect()实际上是将像素绘制到舞台上,这意味着您在未来的帧中将无法引用这些形状。现在,您已经有了一个已绘制多次的 MovieClip。您需要的是大量的影片剪辑,以便您可以引用每个单元格来更新/编辑每个帧。
最好的选择是执行以下操作(我只提供伪值,因为我对 AS2 语法有点不稳定):
A) 创建一个数组来保存所有单元格。称之为:
B) 在构造函数循环的每个步骤中,执行 4 件事。
1) 创建一个新的MovieClip `var tempCell:MovieClip = new MovieClip();
2) 在每个 MovieClip 上绘制一个矩形:AS2 中图形 API 的教程 http://www.actionscript.org/resources/articles/727/1/Drawing-shapes-with-AS2/Page1.html
3) 为每个影片剪辑添加一个事件监听器它指向一个公共事件处理程序。此侦听器侦听该 MovieClip 上的鼠标单击(或 MOUSE_DOWN)
4) 并使用 Cells.push(tempClip) 将新的 MovieClip 添加到您的数组中,这样您现在就拥有了一个包含对所有内容的引用的对象你的细胞。
C) 创建一个单击事件处理程序来重绘已单击的单元格。尝试
MouseEvent.target
您还有另一种选择使用图形 API 绘制矩形,那就是简单地在 Flash 库中添加和删除库存图形。您必须在 Flash 中绘制这些图形,然后“导出为 Actionscript”才能调用它们。
希望这能为您指明正确的方向!
J
From what I can tell, issue with your current approach is that using drawRect() literally draws pixels on to the stage, which means you'll have no reference to those shapes in future frames. right now, you've got one MovieClip that has been drawn many times. What you need is a lot of MovieClips so you have a reference to each cell that you can update/edit every frame.
Your best bet is to do the following (I'll just provide pseudo because I'm a bit shaky on AS2 syntax):
A) Create an array to hold all of the Cells. Call it:
B) During each step of the loops in your constructor, do 4 things.
1) Create a new MovieClip `var tempCell:MovieClip = new MovieClip();
2) Draw a rectangle on to each MovieClip: A tutorial for the graphics API in AS2 http://www.actionscript.org/resources/articles/727/1/Drawing-shapes-with-AS2/Page1.html
3) Add an event listenerto each MovieClip that points to a common event handler. This listener listens for mouse clicks on that MovieClip (or MOUSE_DOWN)
4) and use
Cells.push(tempClip)
to add that new MovieClip to your array so you now have one object that contains a reference to all of your cells.C) Create an click event handler that redraws the cell that has been clicked. Try
MouseEvent.target
You have another option to using the graphics API to draw rectangles, and that is to simply add and remove stock graphics from your Flash library. You'll have to draw these graphics in Flash and then 'Export for Actionscript' to call them up.
Hope this points you in the right direction!
J