AS3 - removeChild() 从舞台上删除所有内容
我正在为学校开发一个简单的 Flash 游戏。在一个关卡中,会产生多个敌人,玩家需要射击它们。我使用removeChild()来摆脱被射击的敌人,但是当我点击(击中)敌人时,舞台上的所有东西都会被移除;它完全一片空白。
用敌人填充我的舞台的函数如下:
private function Game2():void{
for (var i:uint=0; i<50; i++) {
var man:MovieClip = new man_mc();
man.x=Math.random()*750;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);
man.addEventListener(MouseEvent.CLICK, getroffen);
}
函数“getroffen”检查敌人是否被击中:
public function getroffen(evt:MouseEvent):void{
trace("hit");
this.parent.removeChild(this);
}
这里有点困惑为什么它会删除舞台上的所有内容,而不是只删除我点击的敌人。有什么帮助吗?多谢。
I'm working on a simple Flash game for school. In one level, multiple enemies spawn and the player is supposed to shoot them. I used removeChild() to get rid of the enemy that got shot, but when I click (hit) an enemy, everything on my stage gets removed; it goes completely blank.
The function to populate my stage with enemies is the following:
private function Game2():void{
for (var i:uint=0; i<50; i++) {
var man:MovieClip = new man_mc();
man.x=Math.random()*750;
man.y=Math.floor(Math.random()*(70))+350;
addChild(man);
man.addEventListener(MouseEvent.CLICK, getroffen);
}
function 'getroffen' checks if an enemy got hit:
public function getroffen(evt:MouseEvent):void{
trace("hit");
this.parent.removeChild(this);
}
Kind of confused here as to why it removes everything on the stage instead of removing just the enemy I click on. Any help? Thanks alot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据
getroffen()
的位置,您要删除该类(this
指向当前作用域对象),所以我猜测它可能是Main类。
您需要在删除功能中执行类似的操作:
depending on where
getroffen()
is, you're removing that class (this
points to the current scope object), so I'm guessing it's probably theMain
class.You need to do something like in your remove function:
而不是
this.parent.removeChild(this)
尝试:这应该可以解决问题!
事件 (mouseEvent) 是敌人之一的“子事件”,因此移除 mouseEvent 的目标将会移除敌人。
Instead of
this.parent.removeChild(this)
try:That should do the trick!
The event (mouseEvent) is a 'child' of one of your enemies, so removing the target of your mouseEvent will remove the enemy.