关于 ActionScript 中的类
我有一个文件夹(Projects),其中包含 2 个文件:FirstPro.fla、FirstClass.as 我的问题的目标是与我自己创建的类中的两个不同对象(MovieClip)进行交互,我只有一个类,并且它仅针对一个MovieClip,我的目标是交互,对于第二个MovieClip,其名称为(letterPanel) ) 没有类,它是场景中唯一的 MovieClip;
在 FirstClass.as 中,我有下一个代码:
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class FirstClass extends MovieClip
{
public function FirstClass()
{
var NewMyTimer:Timer = new Timer(100);
NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
NewMyTimer.start();
}
public function hm(TimerEvent):void
{
this.y += 5;
if(this.hitTestObject(letterPanel))
{
trace("Coincidens");
}
}
}
}
在 FirstPro.fla 文件中,我在场景中有两个 MovieClip(图像),其中一个具有 firstobj 名称,它与我创建的类有链接,并且它可以工作,我的对象第一个对象随着计时器下降,但我想与第二个对象(MovieClip-letterPanel)交互 - if(this.hitTestObject(letterPanel)) { trace("Coincidens"); } 如果我在我的类中编写此代码,则此输出错误(letterPanel)不是未定义的,我可以如何处理一个类中这两个对象的交互?
I have one folder(Projects), which contains 2 files: FirstPro.fla, FirstClass.as
The goal of my question is interaction with two different objects (MovieClip) from class, which i created by myself, i have only one class, and it class for only one MovieClip, my goal is interaction, for MovieClip second which has name(letterPanel) has no class, its only MovieClip on scene;
In FirstClass.as i have the next code:
package
{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
import flash.events.*;
import flash.display.Stage;
public class FirstClass extends MovieClip
{
public function FirstClass()
{
var NewMyTimer:Timer = new Timer(100);
NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
NewMyTimer.start();
}
public function hm(TimerEvent):void
{
this.y += 5;
if(this.hitTestObject(letterPanel))
{
trace("Coincidens");
}
}
}
}
And in FirstPro.fla file i have on the scene i have two MovieClip (Images), one of them has firstobj name, which has link with my created class, and it works, my object firstobj going down with the timer, but i would like to interaction with the second object(MovieClip-letterPanel) - if(this.hitTestObject(letterPanel)) { trace("Coincidens"); }
If i write this code in my class this output mistake (letterPanel) is not undefined, what can i do with interactions of those two object in one class,?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须分配 MovieClip,并且在设置 var 之前也无法启动计时器
You have to assign the MovieClip and you also can not start the timer untill that var has been set
我最初的反应(除了感到困惑,因为你对项目的解释有点难以理解)是你对 OOP 没有很好的掌握。这个问题之前有一个答案,我认为是正确的。如果我没记错的话,您拒绝它只是因为它没有利用类。 OOP 是关于了解如何以及何时实现 OOP 概念,例如抽象、封装、继承、多态性等。
在您的情况下,我认为您问题的简单答案如下:
但是,如果您觉得需要真正使用类,我已经创建了一个类似的 Flash 应用程序来演示您将如何进行操作。它在舞台上基本上有 3 个可拖动的红色、绿色和蓝色圆形显示对象,当您拖动圆形显示对象并且它与另一个圆形显示对象重叠/相交时,会产生一条轨迹来描述碰撞。您只需将以下代码复制并粘贴到文档类中即可运行该应用程序:
My initial response(other than being confused, since your explaination of your project is a bit hard to understand) is that you don't have a good grasp on OOP. There was an answer for this question before that I believed to be correct. If I remember correctly you rejected it simply because it didn't utilize classes. OOP is about knowing how to as well as when to implement OOP concepts such as abstraction, encapsulation, inheritance, polymorphism etc.
In your case I think the simple answer to your question is the following:
However if you feel the need to truely use classes, I've created a similar flash application to demonstrate how you would go about it. Its basically 3 draggable red, green and blue circle display objects on the stage and when you drag a circle display object and it overlaps/intersects another circle display object a trace is made to describe the collision. You can run the application by simply copying and pasting the following code into your document class: