所以基本上我有一个名为Jug的MovieClip,当点击鸡蛋并将其拖到Jug时,我希望它消失,然后在它首次启动的地方重新添加。除此之外,我想要一个变量在值中加1。
我已经尝试了一段时间摆弄它并且我无法弄清楚,因为当我删除孩子时它会出错。这是代码:
var eggClickOffset:Point = null;
var egg:Egg = new Egg();
egg.x = 290;
egg.y = 330;
addChild(egg);
var eggAmount:TextField = new TextField();
eggAmount.defaultTextFormat = textFormat;
eggAmount.x = 250;
eggAmount.y = 60;
eggAmount.height = 18;
eggAmount.width = 100;
eggAmount.border = true;
eggAmount.text = "Incorrect Amount";
eggAmount.background = true;
eggAmount.backgroundColor = 0xff0000;
stage.focus = eggAmount;
addChild(eggAmount);
var eggs:int;
eggs = 0;
//Egg Event listeners:
egg.addEventListener(Event.ENTER_FRAME, eggAmountCounter);
egg.addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
egg.addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
egg.addEventListener(Event.ENTER_FRAME, dragEgg);
egg.addEventListener(Event.ENTER_FRAME, checkEggCollision);
//starting egg drag:
function startEggDrag(event:MouseEvent):void
{
eggClickOffset = new Point(event.localX,event.localY);
}
//Stopping the egg drag:
function stopEggDrag(event:MouseEvent):void
{
eggClickOffset = null;
}
//Egg Dragging:
function dragEgg(event:Event):void
{
if (eggClickOffset != null)
{// must be dragging
egg.x = mouseX - eggClickOffset.x;
egg.y = mouseY - eggClickOffset.y;
}
}
//When egg hits jug:
function checkEggCollision(event:Event):void
{
if (jug.hitTestObject(egg))
{
eggs + 1;
egg.removeEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
egg.removeEventListener(Event.ENTER_FRAME, dragEgg);
removeChild(egg);
addChild(egg);
egg.x = 300;
egg.y = 300;
}
}
//How many eggs:
function eggAmountCounter(event:Event):void {
if(eggs == 3){
eggAmount.text = "Corrent Amount";
}
}
答案 0 :(得分:2)
因此,就工具包重绘自身所需的内容而言,从容器中添加和删除内容实际上非常昂贵。并且通常会出现这样的问题,即您所遇到的问题与广告宣传不同。因此,当您想要控制可见性时,我的建议是永远不会添加/删除组件。只需将它们标记为visible = false / true,并可选择记住使用includeInLayout = true / false。由于你有影片剪辑,因此= true / false应该足够好。
如果你只想让鸡蛋重新回到原来的位置,只需直接修改它的x,y位置即可。我创建了一个简单的方法,它接收一个egg并设置初始状态的所有属性。在你的stopEggDrag方法中,只需调用该函数传递屏幕上正在吸毒的蛋。 Viola它会回到原来的位置。
这里的诀窍是,如果你永远不删除对象,就不必解决删除问题。
答案 1 :(得分:1)
抱歉,我在removeChild上没有错误,但我暗示不能再移动蛋了,因为:
egg.removeEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
顺便说一下,我确定在这种情况下你不需要3x Event.ENTER_FRAME。
我建议你采用不同的方法吗?也许你可以尝试更像这样的东西:
var eggStartPos:Point;
egg.addEventListener(MouseEvent.MOUSE_DOWN, startEggDrag);
egg.addEventListener(MouseEvent.MOUSE_UP, stopEggDrag);
//starting egg drag:
function startEggDrag(event:MouseEvent):void
{
eggStartPos = new Point(egg.x, egg.y);
egg.addEventListener(Event.ENTER_FRAME, handle_enterFrame);
}
//Stopping the egg drag:
function stopEggDrag(event:MouseEvent):void
{
egg.removeEventListener(Event.ENTER_FRAME, handle_enterFrame);
eggStartPos = null;
}
//Handle egg moving and hittest on enter frame
function handle_enterFrame(event:Event):void
{
egg.x = mouseX;
egg.y = mouseY;
if( collisionWithJug(egg) )
{
egg.removeEventListener(Event.ENTER_FRAME, handle_enterFrame);
collisionOccur();
}
}
function collisionWithJug(target:DisplayObject):Boolean
{
if (jug.hitTestObject(target))
return true;
else
return false;
}
//Got collision
function collisionOccur():void
{
eggs += 1;
egg.x = eggStartPos.x;
egg.y = eggStartPos.y;
eggAmountCounter();
}
//How many eggs:
function eggAmountCounter():void
{
if(eggs == 3)
{
eggAmount.text = "Corrent Amount";
}
}