关于 ActionScript 中的类

发布于 2024-11-04 11:24:18 字数 1165 浏览 2 评论 0原文

我有一个文件夹(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2024-11-11 11:24:18

您必须分配 MovieClip,并且在设置 var 之前也无法启动计时器

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{
    private var _letterPanel:MovieClip;
    public function FirstClass(){

    }
    public function startConter( ):void{
      var NewMyTimer:Timer=new Timer(100);
      NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
      NewMyTimer.start();
    }
    public function hm(TimerEvent):void{
      this.y+=5;
      if(this.hitTestObject(this._letterPanel)){
        trace("Coincidens");
       }
    }
    public function set letterPanel( val:MovieClip ):void{
      this._letterPanel = val;
    }
  }
}


var firstClass:FirstClass = new FirstClass( );
firstClass.letterPanel = letterPanel;
firstClass.startCounter( );

You have to assign the MovieClip and you also can not start the timer untill that var has been set

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{
    private var _letterPanel:MovieClip;
    public function FirstClass(){

    }
    public function startConter( ):void{
      var NewMyTimer:Timer=new Timer(100);
      NewMyTimer.addEventListener(TimerEvent.TIMER, hm);
      NewMyTimer.start();
    }
    public function hm(TimerEvent):void{
      this.y+=5;
      if(this.hitTestObject(this._letterPanel)){
        trace("Coincidens");
       }
    }
    public function set letterPanel( val:MovieClip ):void{
      this._letterPanel = val;
    }
  }
}


var firstClass:FirstClass = new FirstClass( );
firstClass.letterPanel = letterPanel;
firstClass.startCounter( );
戏剧牡丹亭 2024-11-11 11:24:18

我最初的反应(除了感到困惑,因为你对项目的解释有点难以理解)是你对 OOP 没有很好的掌握。这个问题之前有一个答案,我认为是正确的。如果我没记错的话,您拒绝它只是因为它没有利用类。 OOP 是关于了解如何以及何时实现 OOP 概念,例如抽象、封装、继承、多态性等。

在您的情况下,我认为您问题的简单答案如下:

stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    if(displayObject1.hitTestObject(displayObject2)
    trace("hit");

}// end function

但是,如果您觉得需要真正使用类,我已经创建了一个类似的 Flash 应用程序来演示您将如何进行操作。它在舞台上基本上有 3 个可拖动的红色、绿色和蓝色圆形显示对象,当您拖动圆形显示对象并且它与另一个圆形显示对象重叠/相交时,会产生一条轨迹来描述碰撞。您只需将以下代码复制并粘贴到文档类中即可运行该应用程序:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var redCircle:Circle = new Circle(Color.RED, 50);
            redCircle.name = "redCircle";
            redCircle.x = 100;
            redCircle.y = 100;
            addChild(redCircle);

            var greenCircle:Circle = new Circle(Color.GREEN, 50);
            greenCircle.name = "greenCircle";
            greenCircle.x = 300;
            greenCircle.y = 100;
            addChild(greenCircle);

            var blueCircle:Circle = new Circle(Color.BLUE, 50);
            blueCircle.name = "blueCircle";
            blueCircle.x = 500;
            blueCircle.y = 100;
            addChild(blueCircle);

            var collision:Collision = new Collision(stage, redCircle, greenCircle, blueCircle);
            collision.addEventListener(CollisionEvent.COLLISION, onCollision);

        }// end function

        private function onCollision(e:CollisionEvent):void
        {
            var collision:Collision = Collision(e.target);
            collision.removeEventListener(CollisionEvent.COLLISION, onCollision);

            trace("There was a collision between " + e.displayObject1.name + " and " + e.displayObject2.name);

        }// end function

    }// end class

}// end package

internal class Color
{
    public static const RED:uint = 0xFF0000;
    public static const GREEN:uint = 0x00FF00;
    public static const BLUE:uint = 0x0000FF;

}// end class

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

internal class Circle extends Sprite
{
    public function Circle(color:uint, radius:Number)
    {
        graphics.beginFill(color);
        graphics.drawCircle(0,0,radius);
        graphics.endFill();

        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);

    }// end function

    private function onMouseDown(e:MouseEvent):void
    {
        parent.setChildIndex(this, parent.numChildren - 1);
        startDrag();

        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)

    }// end function

    private function onMouseUp(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);

        stopDrag();

    }// end function

}// end class

internal class Collision extends EventDispatcher
{
    private var _stage:Stage;
    private var _doVector:Vector.<DisplayObject>; 

    public function Collision(stage:Stage, ...displayObjects)
    {
        _stage = stage;
        _doVector = getDOVector(validateArgs(displayObjects, DisplayObject));

        init();

    }// end function

    private function init():void
    {   
        _stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

    }// end function

    private function onEnterFrame(e:Event):void
    {
        for(var i:uint = 0; i < _doVector.length; i++)
        {
            for(var j:uint = 0; j < _doVector.length; j++)
            {
                if (_doVector[i] != _doVector[j])
                {
                    if (_doVector[i].hitTestObject(_doVector[j]))
                    dispatchEvent(new CollisionEvent(CollisionEvent.COLLISION, _doVector[i], _doVector[j]));

                }// end if

            } // end for

        }// end for

    }// end function

    private function validateArgs(args:Array, type:Class):Array
    {
        for each(var arg:* in args)
        if(!(arg is type))
        throw new ArgumentError("Argument must be of type " + type);

        return args;

    }// end function

    private function getDOVector(array:Array):Vector.<DisplayObject>
    {
        var doVector:Vector.<DisplayObject> = new Vector.<DisplayObject>();

        for each(var displayObject:DisplayObject in array)
        doVector.push(displayObject);

        return doVector;

    }// end function

}// end class

internal class CollisionEvent extends Event
{
    public static const COLLISION:String = "collision";

    private var _displayObject1:DisplayObject;
    private var _displayObject2:DisplayObject;

    public function get displayObject1():DisplayObject { return _displayObject1 };
    public function get displayObject2():DisplayObject { return _displayObject2 };

    public function CollisionEvent(type:String, 
                                   displayObject1:DisplayObject,
                                   displayObject2:DisplayObject,
                                   bubbles:Boolean = false,
                                   cancelable:Boolean = false):void
    {
        _displayObject1 = displayObject1;
        _displayObject2 = displayObject2;

        super(type, bubbles, cancelable);

    }// end function

    override public function clone():Event
    {
        return new CollisionEvent(type, displayObject1, displayObject2, bubbles, cancelable);

    }// end function

}// end class  

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:

stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void
{
    if(displayObject1.hitTestObject(displayObject2)
    trace("hit");

}// end function

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:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var redCircle:Circle = new Circle(Color.RED, 50);
            redCircle.name = "redCircle";
            redCircle.x = 100;
            redCircle.y = 100;
            addChild(redCircle);

            var greenCircle:Circle = new Circle(Color.GREEN, 50);
            greenCircle.name = "greenCircle";
            greenCircle.x = 300;
            greenCircle.y = 100;
            addChild(greenCircle);

            var blueCircle:Circle = new Circle(Color.BLUE, 50);
            blueCircle.name = "blueCircle";
            blueCircle.x = 500;
            blueCircle.y = 100;
            addChild(blueCircle);

            var collision:Collision = new Collision(stage, redCircle, greenCircle, blueCircle);
            collision.addEventListener(CollisionEvent.COLLISION, onCollision);

        }// end function

        private function onCollision(e:CollisionEvent):void
        {
            var collision:Collision = Collision(e.target);
            collision.removeEventListener(CollisionEvent.COLLISION, onCollision);

            trace("There was a collision between " + e.displayObject1.name + " and " + e.displayObject2.name);

        }// end function

    }// end class

}// end package

internal class Color
{
    public static const RED:uint = 0xFF0000;
    public static const GREEN:uint = 0x00FF00;
    public static const BLUE:uint = 0x0000FF;

}// end class

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

internal class Circle extends Sprite
{
    public function Circle(color:uint, radius:Number)
    {
        graphics.beginFill(color);
        graphics.drawCircle(0,0,radius);
        graphics.endFill();

        addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);

    }// end function

    private function onMouseDown(e:MouseEvent):void
    {
        parent.setChildIndex(this, parent.numChildren - 1);
        startDrag();

        stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)

    }// end function

    private function onMouseUp(e:MouseEvent):void
    {
        stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);

        stopDrag();

    }// end function

}// end class

internal class Collision extends EventDispatcher
{
    private var _stage:Stage;
    private var _doVector:Vector.<DisplayObject>; 

    public function Collision(stage:Stage, ...displayObjects)
    {
        _stage = stage;
        _doVector = getDOVector(validateArgs(displayObjects, DisplayObject));

        init();

    }// end function

    private function init():void
    {   
        _stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

    }// end function

    private function onEnterFrame(e:Event):void
    {
        for(var i:uint = 0; i < _doVector.length; i++)
        {
            for(var j:uint = 0; j < _doVector.length; j++)
            {
                if (_doVector[i] != _doVector[j])
                {
                    if (_doVector[i].hitTestObject(_doVector[j]))
                    dispatchEvent(new CollisionEvent(CollisionEvent.COLLISION, _doVector[i], _doVector[j]));

                }// end if

            } // end for

        }// end for

    }// end function

    private function validateArgs(args:Array, type:Class):Array
    {
        for each(var arg:* in args)
        if(!(arg is type))
        throw new ArgumentError("Argument must be of type " + type);

        return args;

    }// end function

    private function getDOVector(array:Array):Vector.<DisplayObject>
    {
        var doVector:Vector.<DisplayObject> = new Vector.<DisplayObject>();

        for each(var displayObject:DisplayObject in array)
        doVector.push(displayObject);

        return doVector;

    }// end function

}// end class

internal class CollisionEvent extends Event
{
    public static const COLLISION:String = "collision";

    private var _displayObject1:DisplayObject;
    private var _displayObject2:DisplayObject;

    public function get displayObject1():DisplayObject { return _displayObject1 };
    public function get displayObject2():DisplayObject { return _displayObject2 };

    public function CollisionEvent(type:String, 
                                   displayObject1:DisplayObject,
                                   displayObject2:DisplayObject,
                                   bubbles:Boolean = false,
                                   cancelable:Boolean = false):void
    {
        _displayObject1 = displayObject1;
        _displayObject2 = displayObject2;

        super(type, bubbles, cancelable);

    }// end function

    override public function clone():Event
    {
        return new CollisionEvent(type, displayObject1, displayObject2, bubbles, cancelable);

    }// end function

}// end class  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文