AS3层顺序

发布于 2024-12-11 17:19:56 字数 921 浏览 0 评论 0原文

我创建了两个空的 Sprite 作为图层,bottom_spr 和 top_spr

单击按钮时,会出现一个 MovieClip 并跟随您的鼠标,直到您单击,那么它的位置就固定了。

单击该按钮后,我会将 MovieClip 添加到正确的 Sprite 中。

不幸的是,图层系统不起作用,因为它们按照我放置的顺序分层,精灵似乎不会影响它。

这怎么可能?

private var ground_spr:Sprite;
private var units_spr:Sprite;

public function Game() {
addEventListeners();

ground_spr = new Sprite();
units_spr= new Sprite();

addChild(ground_spr);
addChild(units_spr);
}

private function addEventListeners(){
groundBtn.addEventListener(MouseEvent.CLICK, clickGroundBtn);
unitBtn.addEventListener(MouseEvent.CLICK, clickUnitBtn);
}

private function clickGroundBtn(event:MouseEvent){
    var ground = new Ground_mc();
follow();
ground_spr.addChild(ground);
}

private function clickUnitBtn(event:MouseEvent){
    var unit = new Unit_mc();
follow();
units_spr.addChild(unit);
}

I created two empty Sprites to serve as layers, bottom_spr and top_spr

When clicking a button, a MovieClip appears and follows your mouse, untill you click, then its position is fixed.

As soon as the button is clicked, I addChild the MovieClip to the correct Sprite.

Unfortunately, the layer system doesn't see to work, because they are layered in the order I place them, the Sprites don't seem to influence it.

How is this possible?

private var ground_spr:Sprite;
private var units_spr:Sprite;

public function Game() {
addEventListeners();

ground_spr = new Sprite();
units_spr= new Sprite();

addChild(ground_spr);
addChild(units_spr);
}

private function addEventListeners(){
groundBtn.addEventListener(MouseEvent.CLICK, clickGroundBtn);
unitBtn.addEventListener(MouseEvent.CLICK, clickUnitBtn);
}

private function clickGroundBtn(event:MouseEvent){
    var ground = new Ground_mc();
follow();
ground_spr.addChild(ground);
}

private function clickUnitBtn(event:MouseEvent){
    var unit = new Unit_mc();
follow();
units_spr.addChild(unit);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗼ふ静 2024-12-18 17:19:56

你的问题很模糊,但我会尽力回答。如果您明确说明您希望如何准确订购物品,我可以更好地回答您的问题。如果需要的话,可以在深度处理上添加抽象层。在这种情况下,我认为没有太大必要,但我会向您展示一些您可以做的事情。

将对象添加到屏幕背面:

this.setChildIndex(objName, 0);

将对象添加到屏幕正面:

this.setChildIndex(objName, this.numChildren - 1);

交换两个对象的深度:

this.swapChildren(objA, objB);
//Note this is expecting two DisplayObjects so you may have to do:
this.swapChildren(objA as DisplayObject, objB as DisplayObject);

Your question is very vague but I will attempt to answer it. If you clarify how you want your objects ordered exactly, I can better answer your question. It is fine to add layer of abstraction over the depth handling, if you need it. In this case I dont see much of a need, but I will show you a few things you could do.

To add an object to the back of the screen:

this.setChildIndex(objName, 0);

To add an object to the front of the screen:

this.setChildIndex(objName, this.numChildren - 1);

To swap the depths of two objects:

this.swapChildren(objA, objB);
//Note this is expecting two DisplayObjects so you may have to do:
this.swapChildren(objA as DisplayObject, objB as DisplayObject);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文