AndEngine - 有问题,安装一个新的Sprite实体实体、问题、AndEngine、Sprite

2023-09-04 23:25:28 作者:放下过去放过自己

我一直在做的试验和错误的时间,现在我还没有想出的东西的解决方案似乎很简单.... 我现在用的是

I've been doing trial and error for hours now and I have not yet come up with a solution for something that seems simple.... I am using the

public void onConfigurationChanged(Configuration newConfig)

方法来检测用户是否改变了他们的屏幕方向。 最终,它被发送到这个方法,其中该实体连接于场景:

method to detect if a user has changed their screen orientation. Eventually, it gets sent to this method where the entitys are attached to the scene:

public void BuildScene(final Scene scene){

       // Destroys current scene.
            scene.detachChildren();

        this.SpriteDayPortrait = new Sprite(-200, 0, 2000, 500, this.mParallaxLayerDayPortrait);
        this.SpriteDayLandscape = new Sprite(0, 0, 750, 500, this.mParallaxLayerDayLandscape);

    if (prefs.getString("frontImage", "1").equals("3"))
            {
                //Day
                if (orientationValue.equals("PORTRAIT"))
                {
                    Log.d("Orientation", "Portrait");
                    scene.detachChild(SpriteDayLandscape);

                    scene.attachChild(SpriteDayPortrait);

                }
                else if (orientationValue.equals("LANDSCAPE"))
                {
                    Log.d("Orientation", "Landscape");
                    scene.detachChild(SpriteDayPortrait);

                    scene.attachChild(SpriteDayLandscape);

                }
            }

}

该方法被调用来首次创建墙纸时,以及当用户更改屏幕方向。

This method is called to when the wallpaper is first created, and also when a user changes screen orientation.

我已经测试了这个在我的手机,它成功地显示了日志消息当我切换方向,这意味着它是做什么,我想要它做的。

I have tested this on my phone and it successfully displays the log messages when I switch orientations, which means that it is doing it what I want it to do.

问题 -

的精灵孩子不分离时,此方法被调用来。如果我在人像模式,并切换到风景,人像精灵依然存在,我希望它消失,反之亦然。

The sprite child does not detach when this method is called to. If I am in Portrait mode, and switch to Landscape, the portrait sprite remains and I would like it to disappear, and vice versa.

我会非常高兴,如果有人可以回答这个问题我一直有头疼了本作大概要20个小时。

I would be extremely happy if anyone could answer this I've been having a headache over this for probably 20 hours.

推荐答案

它看起来像这个问题可能是逻辑:你调用分行有关分离他们或安装之前重新分配SpriteDayPortrait和SpriteDayLanscape

It looks like the problem might be logic: You reassign the SpriteDayPortrait and SpriteDayLanscape before calling the branch about detaching them or attaching them.

所以每个分离脚本调用它指的是时间的新的精灵的实例,而不是要分离的旧实例。

So each time the detach script is called it is referring to a new instance of the sprite, rather than the old instance that you want to detach.

尝试将精灵的分配到另一个功能是创建时的情景,这只是所谓的:

try moving the assignment of the sprites into another function that is only called when the scene is created:

// Move this
this.SpriteDayPortrait = new Sprite(-200, 0, 2000, 500, this.mParallaxLayerDayPortrait);
        this.SpriteDayLandscape = new Sprite(0, 0, 750, 500, this.mParallaxLayerDayLandscape);