Actionscript 3 AS3 removeChild和冲突:问题
我正在用Flash和AS3做一个射击游戏。我还在开始使用课堂文档,但多亏了教程,我才理解了。 下面是它的工作原理: 当敌人的电影剪辑击中由用户射击的子弹时,我使用了HitTestObject:Actionscript 3 AS3 removeChild和冲突:问题,actionscript-3,flash,removechild,Actionscript 3,Flash,Removechild,我正在用Flash和AS3做一个射击游戏。我还在开始使用课堂文档,但多亏了教程,我才理解了。 下面是它的工作原理: 当敌人的电影剪辑击中由用户射击的子弹时,我使用了HitTestObject: //checking if it is touching any bullets //we will have to run a for loop because there will be multiple bullets for(var i:int = 0;i<_root.bulletConta
//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
//numChildren is just the amount of movieclips within
//the bulletContainer.
//we define a variable that will be the bullet that we are currently
//hit testing.
var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);
//now we hit test
if(hitTestObject(bulletTarget)){
hit++;
if(hit==4)
{
gotoAndPlay(4); //the Enemy MC is removed after 4 hits(parent.removeChild(this))
removeEventListener(Event.ENTER_FRAME, eFrame);
//the Bullet MC is removed with its Listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
}
if(hit<4)
{
gotoAndPlay(2);
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
}
if(hit > 4)
{
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
hit = 0;
}
}
}
我还发出了一个命令,检测敌方MC和玩家之间的碰撞(我给了“craft”实例名称):
因此,如果敌人的MC击中飞船MC(玩家),它也会被移除。但我遇到了一个问题:当敌人的MC被子弹击中时(被移除它的用户“射击”了4次)但也被玩家碰撞时,它没有被移除,因为它应该被移除。。。
相反,它仍然显示在屏幕上,并且是静态的:它既不能被子弹MC击中,也不能被玩家MC击中
我认为这是因为敌人MC的gotoAndPlay(4)
执行的parent.removeChild(这个)
,导致了这个问题:它被执行了两次(因为敌人MC被子弹MC和玩家摧毁)。。。
有人有办法解决removeChild的双重执行问题吗?
如果您想了解更多信息,可以下载源代码:
谢谢你的帮助。将你的敌人放在一个容器内(可能只是一个精灵对象),并放在动画调用的第4帧
parent.parent.removeChild(this.parent)代码>
你可能需要改变一些敌人的移动代码,但你可以找到答案
其基本思想是将动画隐藏在非动画精灵中。在代码中间更改帧会把事情搞砸,除非它嵌套得比调用的对象更深。我希望这是有道理的 我并没有读完整的内容,但这闻起来像是一个糟糕的案例,在主时间线中切换帧时编码。最好一起避免这种做法。在我看来,主要的时间线是为动画师而不是程序员准备的。您好,我在那里写的所有代码都在Actionscript类文档中。只有父.removeChild(this)
在MovieClip的时间线上…你可以尝试添加一个条件检查,比如if(敌方){remove敌方},这样如果另一个碰撞已经发生,它就不会尝试删除它。嗨,我在碰撞检查之前添加了一个类似于if(hit!=4)
的检查…但是我遇到了同样的问题,尽管用户拍摄了4次,但与玩家的碰撞仍然有效。。。谢谢你的帮助,打吧=4不起作用。因为hit=4。我不知道我是否完全按照你说的做了,但我创建了一个空的Movieclip作为容器,并将敌人的Movieclip放在里面。我在里面写了你的代码(在敌人的电影里而不是在容器里),在第四帧动画结束时,我把你的代码行放进去。我用一个“e”实例名调用容器内的敌人Movieclip。在类文档中,为了访问容器中的Movieclip,我写了这样一句话:[“e”]。gotoAndPlay(4)代码>但它不起作用:当电影唇被玩家摧毁而没有被碰撞摧毁时,它会冻结……只有一个敌人?它有一个名为“e”的实例名?当它在容器内时,是的。你说“当它在容器内时”是什么意思?它不是从创建的那一刻起就在容器中吗?是的,有一个“e”实例名。
private function eFrame (event:Event):void{
x+=speed;
if(this.x > stage.stageWidth + 50){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
}
if(hitTestPoint(_root.craft.x -203.25, _root.craft.y - 44.9, collide))
{
_root.dmg.width+=8;//reduce the life bar
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -210.6, _root.craft.y - 32.9, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -210.6, _root.craft.y - 6, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
if(hitTestPoint(_root.craft.x -211.4, _root.craft.y + 20, collide))
{
_root.dmg.width+=8;
removeEventListener(Event.ENTER_FRAME, eFrame);
gotoAndPlay(4);
}
...//There is a lot of other points with the same results...