且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 AS3 中修复舞台上重叠的对象

更新时间:2023-01-24 10:26:52

使用 setChildIndex 的另一个答案肯定会起作用,但是,我认为您应该采取不同的设计方法来删除完全头疼.

The other answer using setChildIndex will definitely work, however, I think a different design approach is really what you should be doing to remove the headache altogether.

例如在游戏中我可能有不同的层,例如:

For example in a game I might have different layers such as :

backgroundLayer
gameLayer
interfaceLayer

这 3 个 Sprite 层将按该顺序添加到舞台上.然后我会将显示对象添加到适当的图层.因此,我添加到 backgroundLayer 或 gameLayer 的任何内容都将始终位于 interfaceLayer 上我的用户界面的后面".

Those 3 Sprite layers would get added to the stage in that order. I would then add display objects to the appropriate layers. So anything I added to the backgroundLayer or gameLayer would ALWAYS be 'behind' my user interface on the interfaceLayer.

这让您不必担心不断分层.setChildIndex 的答案将在那一刻解决问题,但如果向容器中添加其他内容,它会与您的文本框重叠,这是我不认为您想要的.

That allows you to not have to worry about the layering constantly. The answer with setChildIndex will fix the problem for that moment, but should something else be added to the container it will overlap your textbox, which is something I don't assume you want.

这是一个例子:

var backgroundLayer:Sprite = new Sprite;
var gameLayer:Sprite = new Sprite;
var interfaceLayer:Sprite = new Sprite;

addChild(backgroundLayer);
addChild(gameLayer);
addChild(interfaceLayer);

现在,无论您向 interfaceLayer 添加什么,都将始终位于您添加到 gameLayer 或 backgroundLayer 的对象之上.

now, whatever you add to interfaceLayer, will ALWAYS be on top of objects you add to gameLayer or backgroundLayer.

因此,就您的文本框而言,只需将其添加到 interfaceLayer 以及您想要在其后面的任何其他对象,添加到 gameLayer 或 backgroundLayer.

So in the case of your text box, just add it to your interfaceLayer and any other objects you want behind it, you add to the gameLayer or backgroundLayer.