Actionscript 3 RemoveChild与错误2025
Actionscript 3 RemoveChild与错误2025,actionscript-3,null,Actionscript 3,Null,我想通过另一个类删除一个孩子(背景)。我似乎无法瞄准它!它总是返回null或error 2025之类的内容。。。呵呵 我在我的班级里有创作对象的背景: package cem{ import flash.display.Sprite; public class creationBackground extends Sprite{ public function creationBackground() { switch(monterJe
我想通过另一个类删除一个孩子(背景)。我似乎无法瞄准它!它总是返回null或error 2025之类的内容。。。呵呵 我在我的班级里有创作对象的背景:
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!我怎样才能了解我的背景?
我试过了
creationbjets.\u creationBackground.parent.removeChild(creationbjets.\u creationBackground)代码>
我真的不知道如何访问它 我不确定是否正确理解了您的问题,但是:
记住,为了移除孩子,你需要进入孩子的舞台。如果actionObjects类是Movieclip或sprite,则它将具有一个只读变量,该变量将引用该阶段(该阶段可能与您添加的阶段相同,也可能与您添加的阶段不同。\u creationBackground)
例如:
stage.removeChild(_creationBackground);
如果ActionObjects与您添加的creationBackground具有相同的阶段,则应该可以正常工作
如果ActionObjects没有相同的舞台或根本没有舞台(可能它不是精灵或电影唇?),您可以进入添加了_creationBackground的舞台
即:
包装{
import flash.display.Stage;
public class actionObjets {
private var myStage:Stage;
public function actionObjets(s:Stage) {
myStage = s;
}
}
}
然后尝试:
myStage.removeChild(_creationBackground);
当然,这是假设您可以访问ActionObjects中的_creationBackground剪辑
不确定这是否解决了您的问题,
祝你好运。以下任何一项都足够了:
creationObjets.removeChild(creationObjet.getChildAt(0));
或
或
使用removeChildAt()或getChildAt()时,必须指定显示对象(要获取或移除)的索引位置。索引位置是显示对象在显示对象容器的显示列表中的位置(我假设其为0)
另外,在使用getChildByName()时,必须指定要获取的显示对象的名称。请注意,必须首先设置显示对象的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中删除
我不得不对你的flash应用程序/电影做一些假设,但这应该能让你大致了解如何实现我之前提出的建议
我希望这会有所帮助:)排除最后一行代码(removeChild),这是正确的(尽管是一种丑陋的方式)。我怀疑是其他原因导致了这个问题。因为我只想调用类,然后里面有一个变量或函数(会更容易),但似乎无法使它工作!呵呵,我试过了!它没有相同的stage(它是在另一个类中创建的)第199 1061行:通过静态类型类的引用调用可能未定义的方法removeChild。第199 1120行:访问未定义的属性\u creationBackground。以下是我遇到的两个错误:creationobjects.removeChild(_creationBackground);(creationObjets是我添加背景的类)这显然是一个更好的方法,但我认为Elggetto的代码结构非常不同。从他/她的代码看,我怀疑ActionObjet没有对CreationObject的引用,这就是creationBackground实例是静态的原因,因此Elgetto可以从任何类调用类CreationObject(而不是实例),然后访问_creationBackground变量。然后通过调用父对象并删除子对象,应该删除它。所以我想问题可能在他的代码中的其他地方…哇!那是一大堆文字!谢谢我有点理解你说的,但不是全部。。。这里你说:var creationObjet:creationObjet=newcreationobjet();但它所做的只是把孩子带走。。不?艾伦。。。是的,你明白了。那么你认为问题出在别处了?我不知道在哪里。awww:(@Taurayi我也认为,以你的例子来说,你最好在CreationObjet中为creationBackground创建一个getter,这样在ActionObjet中,而不是在CreationObjet.removeChild(_CreationObjet.getChildAt(0))中,你可以改为执行CreationObjet.removeChild(_CreationObjet.creationBackground)。我认为这种方式会更安全一些,因为在某个时候,\ u creationBackground可能不再位于索引0。@Allan哦,我读过这方面的内容,但我真的不知道如何做到这一点。而且,我的代码写得太差,以至于很难尝试重新安排内容。我对getter的理解是,它只是一个返回某些内容的函数。我只是不知道我需要把它放在哪里,如何访问它。
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
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
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