作者:ABC13517626247 | 来源:互联网 | 2023-05-18 08:22
IhaveanarrayofMovieClipsthatareaddedtothestagebyatimerevent.ThisMovieClipiscall
I have an array of Movie Clips that are added to the stage by a timer event. This Movie Clip is called mSquare
.Now I wanted to set up a EventListener
for the Movie clip so whenever the user clicks on the Movie Clip then it is destroyed, But I am having trouble setting this up since there is an Array of them Added to the stage. I keep getting this Error:
我有一组电影剪辑,通过计时器事件添加到舞台上。这个影片剪辑被称为mSquare。现在我想为影片剪辑设置一个EventListener,所以每当用户点击影片剪辑然后它就被销毁了,但是我在设置它时遇到了麻烦,因为它们有一个数组被添加到舞台。我一直收到这个错误:
Cannot access a property or method of a null object reference.
无法访问null对象引用的属性或方法。
Here is what I got so far:
这是我到目前为止所得到的:
mSquare.addEventListener(MouseEvent.CLICK, mIsDown);
Now in the mIsDown
Function I know I have to go through the array so i tried to set up something like this:
现在在mIsDown函数中,我知道我必须通过数组,所以我尝试设置这样的东西:
private function mIsDown(e:MouseEvent):void
{
for (var i:int = 0; i
Also here is how my Square is added to the stage:
这里也是我的Square加入舞台的方式:
private function addSquare(e:TimerEvent):void
{
mSquare = new mcSquare();
stage.addChildAt(mSquare, 0);
mSquare.x = (stage.stageWidth / 2);
mSquare.y = (stage.stageHeight / 2) + 450;
aSquareArray.push(mSquare);
// trace(aSquareArray.length);
}
Any help would be appreciated on what i need to do in order for the user to be able to MOUSE.click or MouseDown for the array of movie clips thanks!
任何帮助将不胜感激我需要做什么,以便用户能够MOUSE.click或MouseDown的电影剪辑数组谢谢!
**************** Here is how I am doing it now***************
****************以下是我现在的表现***************
stage.addEventListener(MouseEvent.MOUSE_DOWN, movieClipHandler);
private function movieClipHandler(e:MouseEvent):void //test
{
mouseIsDown = true;
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, squareIsBeingClicked);
}
private function squareIsBeingClicked(e:MouseEvent):void
{
var square:DisplayObject = e.target as DisplayObject; // HERE is your clicked square
var i:int = aSquareArray.indexOf(square); // and HERE is your index in the array
if (i <0)
{
// the MC is out of the array
trace("Clicked");
checkSquareIsClicked();
} else
{
// the MC is in the array
}
}
private function checkSquareIsClicked():void
{
for (var i:int = 0; i
1 个解决方案