我想通过另一堂课把一个孩子(背景)移除。我似乎不能瞄准它!它总是给我2025年的零或错误之类的.哈哈。
我有我班上的背景creationObjets
package cem{
import flash.display.Sprite;
public class creationBackground extends Sprite{
public function creationBackground() {
switch(monterJeu._Difficulte){
case 0:
backgroundFacile();
break;
case 1:
backgroundMoyen();
break;
case 2:
backgroundDifficile();
break;
}
}
private function backgroundFacile():void{
var backgroundStage:Sprite = new Sprite();
backgroundStage.graphics.beginFill(0x8FCCA8);
backgroundStage.graphics.moveTo(0,0);
backgroundStage.graphics.lineTo(750,0);
backgroundStage.graphics.lineTo(750,450);
backgroundStage.graphics.lineTo(0,450);
backgroundStage.graphics.lineTo(0,0);
backgroundStage.graphics.endFill();
this.addChild(backgroundStage);
}
private function backgroundMoyen():void{
var backgroundStage:Sprite = new Sprite();
backgroundStage.graphics.beginFill(0x8F3378);
backgroundStage.graphics.moveTo(0,0);
backgroundStage.graphics.lineTo(750,0);
backgroundStage.graphics.lineTo(750,450);
backgroundStage.graphics.lineTo(0,450);
backgroundStage.graphics.lineTo(0,0);
backgroundStage.graphics.endFill();
this.addChild(backgroundStage);
}
private function backgroundDifficile():void{
var backgroundStage:Sprite = new Sprite();
backgroundStage.graphics.beginFill(0x233378);
backgroundStage.graphics.moveTo(0,0);
backgroundStage.graphics.lineTo(750,0);
backgroundStage.graphics.lineTo(750,450);
backgroundStage.graphics.lineTo(0,450);
backgroundStage.graphics.lineTo(0,0);
backgroundStage.graphics.endFill();
this.addChild(backgroundStage);
}
}
}
public static var _creationBackground:creationBackground = new creationBackground();在下面,我补充如下:
addChild(_creationBackground);然后,我想从另一个类actionObjets中删除它!我怎样才能了解我的背景?我试过了
creationObjets._creationBackground.parent.removeChild(creationObjets._creationBackground);
removeChild(creationObjets._creationBackground);我真的不知道如何访问它!
发布于 2010-12-10 01:23:40
我不确定我是否正确地理解了你的问题,但是:
记住,为了移除一个孩子,你需要进入孩子的舞台。如果您的actionObjects类是Movieclip或sprite,那么它将有一个只读变量来引用这个阶段(这可能与您添加_creationBackground的阶段相同,也可能不相同)。
例如:
stage.removeChild(_creationBackground);如果actionObjets具有与添加_creationBackground的位置相同的阶段,那么应该可以正常工作。
如果actionObjets没有相同的舞台,或者根本没有(也许它不是一个精灵或电影?)您可以通过添加_creationBackground的阶段。
IE:
包{
import flash.display.Stage;
public class actionObjets {
private var myStage:Stage;
public function actionObjets(s:Stage) {
myStage = s;
}
}}
然后试着:
myStage.removeChild(_creationBackground);当然,这是假定您可以访问_creationBackground剪辑在actionObjets中。
不知道这是否解决了你的问题,祝你好运。
发布于 2010-12-10 01:25:26
以下任何一项都应足够:
creationObjets.removeChild(creationObjet.getChildAt(0));或
creationObjets.removeChild(creationObjet.getChildByName("creationBackground"));或
creationObjets.removeChildAt(0);当使用removeChildAt()或getChildAt()时,必须指定显示对象的索引位置(希望获取或删除)。索引位置是显示对象容器显示列表中显示对象的位置(我假设它是0)。
此外,在使用getChildByName()时,必须指定要获取的显示对象的名称。注意,必须首先设置display对象的name属性。
下面是一个基于flash应用程序/电影的工作示例:
package
{
import cem.CreationObjet;
import cem.ActionObjet;
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main():void
{
init();
}// end function
private function init():void
{
var creationObjet:CreationObjet = new CreationObjet();
addChild(creationObjet);
var actionObjet:ActionObjet = new ActionObjet(creationObjet);
}// end function
}// end class
}// end package在文档类Main中,导入第一个CreationObjet和ActionObjet。接下来,声明CreationObjet的一个实例,并将其实例化并添加到这个阶段。最后,声明、实例化ActionObjet的一个实例,并将CreationObjet实例解析为它的唯一参数。
package cem
{
import cem.CreationBackground;
import flash.display.Sprite;
public class CreationObjet extends Sprite
{
private var _creationBackground:CreationBackground;
public function CreationObjet():void
{
_creationBackground = new CreationBackground();
addChild(_creationBackground);
}// end function
}// end class
}// end package在CreationObjet类中,CreationBackground的实例被添加到CreationObjet显示对象中。
package cem
{
import cem.CreationObjet;
public class ActionObjet
{
private var _creationObjet:CreationObjet;
public function ActionObjet(p_creationObjet:CreationObjet):void
{
_creationObjet = p_creationObjet;
_creationObjet.removeChild(_creationObjet.getChildAt(0));
// or _creationObjet.removeChild(_creationObjet.getChildByName("creationBackground"));
// or _creationObjet.removeChildAt(0);
}// end function
}// end class
}// end package最后,在ActionObjet类中,CreationBackground显示对象将从CreationObjet中删除。
我不得不对你的闪存应用/电影做了很多假设,但这应该让你对如何实现我之前的建议有一个大致的认识。
我希望这能有所帮助:)
https://stackoverflow.com/questions/4404483
复制相似问题