如何使用 Action Script 3.0 使随机放置的符号飞过?
我正在尝试使用Flash CS4和Action Script 3.0制作一个简单的动画,使许多符号不断地从右向左飞过。我想要的是,一旦一个符号到达屏幕的末尾,它就会被销毁,另一个符号会被放置在起始位置。
我打算给每个符号一个随机的速度,并在每次“摧毁”一个符号时创建一个随机符号。有什么线索我可以从哪里开始吗?
I'm trying to make a simple animation with Flash CS4 and Action Script 3.0 to make a number of Symbols fly by from right to left constantly. What I want is that once a symbol has reached the end of the screen it is destroyed and another one is placed at the start position.
I intend to give each symbol a random speed and create a random symbol each time one is 'destroyed'. Any clues where I can start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您似乎对 Flash 作为一个平台很陌生,所以我认为编写类不应该是您学习 ActionScript 时的第一个选择。现在肯定只是在时间轴上玩一下并学习基础知识。作为一个非常简单的解决方案,我建议在库中创建一个 MovieClip,其类名如“MyBall”...然后将其粘贴到主时间线的第一帧上,瞧。
As you seem new to flash as a platform I would think writing classes shouldn't be your first port of call when learning ActionScript. Definitely just play about on the timeline for now and learn the basics. As very simple solution to this, I would suggest creating a MovieClip in the library with a class name like 'MyBall'... then paste this onto the first frame of the main timeline et voila.
首先,将符号转换为影片剪辑。然后为您的符号创建一个基类 MySymbol.as,类似于:
然后确保您的影片剪辑导出为 AS3(库中项目的“链接”选项)。使每个项目的类名称唯一(例如MySymbol1、MySymbol2),并将基类设置为MySymbol。
您的文档类可能看起来像这样:
如果不是销毁并重新创建一个飞离舞台的对象,而是重新使用现有的对象并将其移回到右侧,那么效率会高得多。但这是一种优化,如果事情变得很慢,您可以稍后实施。
请注意,上面的所有代码都未经测试,而且我已经有一段时间没有编写 AS3 代码了,因此其中可能至少存在一些错误。希望它将作为一个足够好的起点。
First, turn your symbols into MovieClips. Then create a base class MySymbol.as for your symbols, something like:
Then make sure your movie clips are exported for AS3 (the "linkage" option on the item in the library). Make the class name for each item unique (e.g. MySymbol1, MySymbol2), and set the base class to MySymbol.
Your document class might look something like this:
It is a lot more efficient if instead of destroying and re-creating an object that flies off-stage you re-use the existing one and move it back to the right. But this is an optimization you can implement later, if things become slow.
Note that all the code above is UNTESTED and I have not coded AS3 in a while, so there's likely at least a few bugs in it. Hopefully it will serve as a good enough starting point.
Circle
(符号)类,该类扩展 Sprite/Shape 并具有一个velocity
变量Math.floor(Math.random() * 0xffffff)
minVelocity + Math.floor(Math.random() *velocityRange)
Circle
类中创建一个start()
方法来注册输入帧handlerthis.y
,如果y
大于最大值,则调度'recycleMe'
事件。Circle
实例,addChild
它们,并调用它们的start()
方法。'recycleMe'
事件,并从处理程序重置y
的值。Circle
(symbol) class that extends Sprite/Shape and has avelocity
variableMath.floor(Math.random() * 0xffffff)
minVelocity + Math.floor(Math.random() * velocityRange)
start()
method inside theCircle
class that registers an enter frame handlerthis.y
inside the enter frame handler, and dispatch a'recycleMe'
event ify
is more than the max value.Circle
,addChild
them, and call theirstart()
methods.'recycleMe'
events on each of them, and reset the value ofy
from the handler.以下是一些帮助您入门的提示。
MovieClips
具有x
和y
属性。如果您随着时间的推移添加这些数字,您将看到MovieClip
沿着舞台
的 x 和/或 y 轴移动。考虑使用Event.ENTER_FRAME
来执行此操作,这将允许您在每次屏幕更新时更改值。您的
stage
将具有给定的宽度(stageWidth
属性)。您可能想要监视 MovieClip 的x
属性何时大于舞台的宽度。如果是删除 (removeChild
) 并添加一个新的 (addChild
),并将其放回起始x/y
位置。Here's a few prompts to get you started.
MovieClips
have anx
andy
property. If you were to add to these numbers over time you would see theMovieClip
move along the x and/or y axis of thestage
. Look into doing this using theEvent.ENTER_FRAME
which will allow you to change the values every time the screen is going to update.Your
stage
will have a given width (astageWidth
property). You probably want to monitor when your MovieClip'sx
property is greater than the width of your stage. If it is remove (removeChild
) it and add a new one (addChild
) and place it back at the startx/y
position.