如何在 ActionScript 3 - flash 中处理多种游戏模式
我是 Actionscript Flash 3.0 的新手。不管怎样,我已经为包括角色在内的整个游戏关卡编写了代码。现在是否可以创建一个类,当我按“Level 1”时,第一个级别开始?(创建该类的对象)当我按退出时,卸载整个级别。这在闪存中怎么可能呢?有教程吗?谢谢。
I'm new to actionscript flash 3.0. Anyways, i have written code for one whole game level including the character. Is it now possible to create a class, and when i press for e.g. 'Level 1' the first level starts?(create an object of the class) When i press quit, to unload the whole level. How is this possible in flash? are there any tutorials? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OOP 的乐趣。
每当您使用面向对象语言进行编码时,您都需要开发类以实现可重用性。毕竟这是 OOP 设计的主要目标之一。您没有发布任何代码,因此我假设您以程序方式编写游戏,并且除了 flash 包提供给您的基类之外,没有使用 和 类。
举个例子,既然我们谈论游戏,就想到子弹。您可以创建一个子弹类来控制任何单个子弹的所有方面(从击中目标到其轨迹再到飞入太空。因此,当用户发射一轮子弹时,您将创建子弹的新实例并分配一个事件侦听器对于像“HIT”这样的事情,当然你必须在项目符号类中调度该事件,但此时你唯一需要担心的是如果发生了命中事件,项目符号类会处理其他所有事情.
基本上,正如您所看到的,这样做封装了除 HIT 事件之外的任何带有项目符号的操作。
另一个例子是按钮。这个类附带了 flash 库,我相信你以前用过它。将按钮拖到舞台上,添加事件侦听器,创建 onclick 函数,然后就完成了。 Button 类的内部方法都不需要使用,它都为您很好地封装了。
Kirupa 始终是学习的好地方LINK
准备好为此更改重写整个游戏。将程序代码移植到类中可能是一件令人头疼的事情
The joys of OOP.
ANY time you code in an object orientated language you need to develop your classes for re-usability. After all that is one of the main goals for OOP design. You did not post any code so I am going to assume you wrote your game in a procedural way and did not use and classes besides the base classes supplied to you by flash packages.
As an example Since we are talking about games think of a bullet. You can make a bullet class that will control all aspects of any single bullet( from hitting a target to its trajectory to flying off into space. so when the user fires a round you would create a new instance of the bullet and assign an event listener to it for something like "HIT" of course you would have to dispatch the event in the bullet class, but at this point the only think you would need to worry about is if a hit event happened, the bullet class takes care of everything else.
Basically, as you can see doing it this way encapsulates any action with bullet aside from the HIT event.
An other example would be a Button. This class comes with the flash libraries and I am sure you have used it before. Drag a button onto stage add an eventlistener make an onclick function and you are done. None of the internal methods of the Button class need to be played with, it's all encapsulated nicely for you.
Kirupa is always a good place to learn LINK
Be prepared to rewriting your entire game for this change. Porting procedural code into classes can be a hair pulled experience
简短的回答:是的。
一种简单的方法(不一定是最好的方法)是将 mousedown 事件处理程序添加到按钮对象,以触发构造级别的函数(通过创建类的新对象实例)。然后您可以通过其引用(即变量名称)访问该对象。
完成关卡后,您可以将对象引用设置为 null,这将使垃圾收集器清理您的内存。
有很多更优雅的方法可以做到这一点,但请尝试将此作为基本起点。祝你好运。
Short answer: yes.
A simple way (not necessarily the best way) is to add mousedown event handlers to your button objects that trigger a function which constructs your level (by creating a new object instance of your class). You can then access this object through its reference (i.e. variable name).
After you're done with the level, you can set the object reference to null, which will let the garbage collector clean up your memory.
There are much more elegant ways to do this, but try this as a basic starting point. Good luck.