Question

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 Problem--

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.

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

Was it helpful?

Solution

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);

OTHER TIPS

Keep in mind that since changing orientation in AndEngine doesn't exist (You are not allowed to change engine orientation), you should't make your game be using orientation changes (That would be weird for the user).

Anyways, onConfigurationChanged is called from the UI thread, and you should not manipulate objects of AndEngine but in the UpdateThread. It might cause some problems (However, if it would, your game would crash) so change it.

I think it happens because you didn't clear your ParallaxBackground before setting the new background (Perhaps the old background hides the new one?) Keep a reference to your last ParallaxEntity then remove it from the background before adding a new one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top