嘿所有人所以我一直在为此而自杀,这可能是我最后需要帮助才能最终完成这个项目。
所以我有一个计时器事件,它将数组中的Movie剪辑放到舞台上。 MovieClip名为mSquare
,从计时器事件我添加它如下:
private function addSquare(e:TimerEvent):void
{
mSquare = new mcSquare();
stage.addChild(mSquare);
mSquare.x = (stage.stageWidth / 2);
mSquare.y = (stage.stageHeight / 2) + 450;
aSquareArray.push(mSquare);
trace(aSquareArray.length);
}
现在我想要实现的是当用户点击或将鼠标放在对象上时,我希望用户点击的对象从舞台中删除。
现在,当用户点击添加到舞台的数组中的一个MovieClip时,我无法使其正常运行。或者如果我这样做,那么当前点击的MovieClip并没有被破坏,而是舞台上的另一个实例被破坏了。它真的很糟糕。
所以这就是我到目前为止设置的方式......
在我的构造函数中:
stage.addEventListener(MouseEvent.MOUSE_DOWN, movieClipHandler);
然后是movieClipHandler
MouseEvent:
private function movieClipHandler(e:MouseEvent):void //test
{
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, squareIsBeingClicked);
}
然后在squareIsBeingClicked
功能:
private function squareIsBeingClicked(e:MouseEvent):void
{
var square:DisplayObject = e.target as DisplayObject; // clicked square
var i:int = aSquareArray.indexOf(square); // index in the array
if (i < 0) //THIS IS WHERE I GET CONFUSED
{
// the MC is out of the array
trace("Clicked");
mouseIsDown = true;
checkSquareIsClicked();
} else
{
// the MC is in the array
}
}
最后我有一个名为checkSquareIsClicked
的函数来完成所有工作:
private function checkPopIsBeingClicked():void
{
//Loop through all pops
for (var i:int = 0; i < aPopArray.length; i++)
{
//Get current pops in i loop
var currentpop:mcPop = aPopArray[i];
if (mouseIsDown)
{
trace("Current Pop Destroyed")
aPopArray.splice(i, 1);
currentpop.destroyPop();
nScore ++;
updateHighScore();
updateCurrentScore();
mouseIsDown = false;
//Add Explosion Effect
popExplode = new mcPopExplode();
stage.addChild(popExplode);
popExplode.y = currentpop.y;
popExplode.x = currentpop.x;
}
}
}
我知道我正在做一些非常错误的事情,因为它不应该用这么多函数来启动这个鼠标事件数组。也有人在这里帮助我使用显示对象和idex,如上所示,但不知道如何真正实现它。我已经筋疲力尽了所有想法。任何帮助将不胜感激或任何指示。谢谢!
答案 0 :(得分:2)
正如 @Rajneesh 回答所示,将鼠标事件监听器添加到MovieClip
而不是使用舞台鼠标事件来添加监听器,是正确的方法。这意味着不要这样做......:
stage.addEventListener(MouseEvent.MOUSE_DOWN, movieClipHandler);
private function movieClipHandler(e:MouseEvent):void {
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, squareIsBeingClicked);
}
在addSquare函数中替换:
private function addSquare(e:TimerEvent):void {
mSquare = new mcSquare();
stage.addChild(mSquare);
mSquare.x = (stage.stageWidth / 2);
mSquare.y = (stage.stageHeight / 2) + 450;
aSquareArray.push(mSquare);
//look here
mSquare.addEventListener(MouseEvent.MOUSE_DOWN, squareIsBeingClicked);
trace(aSquareArray.length);
}
现在您可以更改删除功能。我将改变一些代码来帮助您优化它并仍然达到预期的效果。让我们从squareIsBeingClicked开始:
private function squareIsBeingClicked(e:MouseEvent):void {
var square:DisplayObject = e.target as DisplayObject; // clicked square
var i:int = aSquareArray.indexOf(square); // index in the array
//this is where you will no longer be confused :)
if (i != -1) {
trace("Clicked");
mouseIsDown = false;
onSquareIsClicked( square ); //this is in place of your checkSquareIsClicked
square.parent.removeChild( square );
aSquareArray.splice( i, 1 ); //remove element from the array
//remove the listener, very important
square.removeEventListener( MouseEvent.CLICK, squareIsBeingClicked );
square = null; //null it if we are not using it again, ready to be garbage collected
}
}
您现在不再需要先前checkSquareIsClicked中的所有删除部分。您可能已经注意到我添加了一个函数onSquareIsClicked()
,这有助于您在发生此事件时可能需要执行的任何功能。由于您在选择对象时添加了“爆炸”,因此onSquareIsClicked()
的内容如下所示:
private function onSquareIsClicked( square:DisplayObject ):void {
popExplode = new mcPopExplode();
stage.addChild(popExplode);
popExplode.y = currentpop.y;
popExplode.x = currentpop.x;
//square.destroyPop(); //obviously this function will not work on a type displayObject
//in which case you just need to cast square to the object containing the method
nScore ++;
updateHighScore();
updateCurrentScore();
}
答案 1 :(得分:1)
将鼠标单击事件添加到要删除的对象并获取目标并将其删除,如下所示:
(注意:此外,无需将MovieClips
添加到数组中(仅在必要时添加))
private function addSquare(e:TimerEvent):void
{
mSquare = new mcSquare();
mSquare.addEventListener(MouseEvent.CLICK, removeSquare);
stage.addChild(mSquare);
//Position here mSquare
}
private function removeSquare(e:MouseEvent):void
{
var tempObject:MovieClip = MovieClip(e.currentTarget); //get the square object
removeChild(tempObject);
tempObject = null;
}