AS3战平鼠标和擦除线时hitTestObject达到鼠标、战平、擦除、hitTestObject

2023-09-08 15:02:15 作者:断线的风筝

我想创建一个简单的hitTestObject,看到正在屏幕上打一个球绘制线条。如果这样做,将删除所有行并重新开始。我很接近。当将CheckIt()函数是其中code需要发生和结算行,但不能使一个新的形状。

如何删除鼠标画线并重新开始当达到命中测试?

  MyShape的变种:形状;


= MyShape的新的Shape();
myshape.graphics.lineStyle(12,0xC807DE);
VAR alreadyDrawn:形状;
alreadyDrawn =新形状();

stage.addEventListener(的MouseEvent.MOUSE_DOWN,activateDraw);
功能activateDraw(事件:MouseEvent)方法:无效
{
    myshape.graphics.moveTo(mouseX,mouseY的);
    的addChild(MyShape的);

    stage.addEventListener(的MouseEvent.MOUSE_MOVE,画线);
    功能画线(myevent:MouseEvent)方法:无效
    {
        stage.addEventListener(侦听MouseEvent.MOUSE_UP,stopDraw);
        功能stopDraw(ENDEVENT:MouseEvent)方法:无效
        {
            alreadyDrawn = MyShape的;
            stage.removeEventListener(的MouseEvent.MOUSE_MOVE,画线);

        }


        myshape.graphics.lineTo(mouseX,mouseY的);
        myevent.updateAfterEvent();
        核实();
    }


}

功能checkIt()
{
    如果(alreadyDrawn.hitTestObject(球)==真)
    {
        TRACE(打的球);
        myshape.graphics.clear();
        myshape.graphics.lineStyle(12,0xC807DE);


    }

}
 

解决方案

您确实有一个形状,而不是两个,因为一旦你 alreadyDrawn = MyShape的你失去了什么对象你连接在那里。你应该做的,而不是 alreadyDrawn.graphics.copyFrom(myshape.graphics); 这将复制所有的笔画从参数不失其它实例对象

接下来,貌似还需要停止绘制如果你发现一个打击VS alreadyDrawn ,对于这一点,调用 stopDraw(空); checkIt()

而接下来,你是不是清除了所有的听众起飞阶段,同时增加越来越多。你看,如果你画临门(preSS拖动释放)几行中, stopDraw 监听器被添加多次,并不会被删除,甚至一次。要做到这一点,使用 removeEventListener(侦听MouseEvent.MOUSE_UP,stopDraw); stopDraw 功能和移动将行成 activateDraw ,因为当你拖动鼠标,而另一个侦听器处理该事件的每一次加有多个鼠标移动事件。

最后,请不要嵌套函数没有实际需要!

固定code应该是这样的:

  MyShape的变种:形状;
= MyShape的新的Shape();
myshape.graphics.lineStyle(12,0xC807DE);
VAR alreadyDrawn:形状;
alreadyDrawn =新形状();

stage.addEventListener(的MouseEvent.MOUSE_DOWN,activateDraw);
功能activateDraw(事件:MouseEvent)方法:无效
{
    myshape.graphics.moveTo(mouseX,mouseY的);
    的addChild(MyShape的);

    stage.addEventListener(的MouseEvent.MOUSE_MOVE,画线);
    stage.addEventListener(侦听MouseEvent.MOUSE_UP,stopDraw);
}

功能画线(事件:MouseEvent)方法:无效
{
    myshape.graphics.lineTo(mouseX,mouseY的);
    event.updateAfterEvent();
    核实();
}
功能stopDraw(事件:MouseEvent)方法:无效
{
    alreadyDrawn.graphics.copyFrom(myshape.graphics);
    stage.removeEventListener(的MouseEvent.MOUSE_MOVE,画线);
    stage.removeEventListener(侦听MouseEvent.MOUSE_UP,stopDraw);
}

功能checkIt()
{
    如果(alreadyDrawn.hitTestObject(球)==真)
    {
        TRACE(打的球);
        myshape.graphics.clear();
        myshape.graphics.lineStyle(12,0xC807DE);
        alreadyDrawn.graphics.clear(); //清除这个太
        stopDraw(空); //中断活动的抽奖,如果有的话
    }
}
 
这篇字效教程,很 硬核

的copyfrom()手动

I am trying to create a simple hitTestObject and see the lines that are being drawing on screen hit with a Ball. If it does it will erase all lines and start fresh. I am very close. The checkIt() function is where the code needs to happen and its clearing the lines but not making a new shape.

How can I erase the lines the mouse drew and start fresh when a hit test is reached?

var myshape:Shape;


myshape = new Shape();
myshape.graphics.lineStyle(12, 0xC807DE);
var alreadyDrawn:Shape;
alreadyDrawn = new Shape();

stage.addEventListener(MouseEvent.MOUSE_DOWN, activateDraw);
function activateDraw(event:MouseEvent):void
{
    myshape.graphics.moveTo(mouseX,mouseY);
    addChild(myshape);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    function lineDraw(myevent:MouseEvent):void
    {
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
        function stopDraw(endevent:MouseEvent):void
        {
            alreadyDrawn = myshape;
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, lineDraw);

        }


        myshape.graphics.lineTo(mouseX,mouseY);
        myevent.updateAfterEvent();
        checkIt();
    }


}

function checkIt()
{
    if (alreadyDrawn.hitTestObject(Ball) == true)
    {
        trace("Hit The Balls");
        myshape.graphics.clear();
        myshape.graphics.lineStyle(12, 0xC807DE);


    }

}

解决方案

You actually have ONE shape instead of two, because once you do alreadyDrawn=myshape you lose whatever object you attached there. You should instead do alreadyDrawn.graphics.copyFrom(myshape.graphics); this will copy all the strokes from argument to object without losing the other instance.

Next, you seemingly need to also stop drawing if you've detected a hit vs alreadyDrawn, for this, call stopDraw(null); from checkIt().

And next, you are not clearing all the listeners off stage while adding more and more. See, if you draw several lines in quick succession (press-drag-release), the stopDraw listener is added multiple times and not removed even once. To do this, use removeEventListener(MouseEvent.MOUSE_UP, stopDraw); inside stopDraw function and move the adding line into activateDraw, because there are multiple "mouse move" events while you drag the mouse, and another listener is added each time you process the event.

And finally, PLEASE don't nest functions without actual need!

The fixed code should be this:

var myshape:Shape;
myshape = new Shape();
myshape.graphics.lineStyle(12, 0xC807DE);
var alreadyDrawn:Shape;
alreadyDrawn = new Shape();

stage.addEventListener(MouseEvent.MOUSE_DOWN, activateDraw);
function activateDraw(event:MouseEvent):void
{
    myshape.graphics.moveTo(mouseX,mouseY);
    addChild(myshape);

    stage.addEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function lineDraw(event:MouseEvent):void
{
    myshape.graphics.lineTo(mouseX,mouseY);
    event.updateAfterEvent();
    checkIt();
}
function stopDraw(event:MouseEvent):void
{
    alreadyDrawn.graphics.copyFrom(myshape.graphics);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, lineDraw);
    stage.removeEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function checkIt()
{
    if (alreadyDrawn.hitTestObject(Ball) == true)
    {
        trace("Hit The Balls");
        myshape.graphics.clear();
        myshape.graphics.lineStyle(12, 0xC807DE);
        alreadyDrawn.graphics.clear(); // clear this too
        stopDraw(null); // stop active draw, if any
    }
}

copyFrom() manual reference