我有一个文件夹(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名称,它与我创建的类有链接,它有效,我的对象firstobj随着计时器而下降,但我想与第二个对象(MovieClip-letterPanel)进行交互 - 如果(this.hitTestObject(letterPanel)){trace(“Coincidens”); } 如果我在我的类中编写此代码,则此输出错误(letterPanel)未定义,如何在一个类中对这两个对象的交互做什么?
答案 0 :(得分:0)
您必须分配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( );
答案 1 :(得分:0)
我最初的回答(除了困惑,因为你的项目解释有点难以理解)是你对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