Applying Fade Out/Transition to new, in-game UI items (newb warning)

27 views
Skip to first unread message

JF

unread,
Jun 8, 2014, 8:02:56 PM6/8/14
to replica-island-...@googlegroups.com
After many hours, I've managed to get an AdMob ad added to my game. The problem is, once the player dies, it doesn't fade to black like the rest of the screen.


Here's my primary piece of code for adding the ad:
  Class: AndouKun
  Method: onCreate

        setContentView(R.layout.main);
        .....
        adView2 = (AdView) findViewById(R.id.adView2);
        adView2.setBackgroundResource(R.anim.ui_button);
       
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("XXXXXXXXXXXXXXXXXXXXXXX")
        .build();
         adView2.loadAd(adRequest);
       
        AlphaAnimation showAnimation = new AlphaAnimation( 0.0f, 1.0f );
         showAnimation.setDuration( 4000 );
         showAnimation.setFillAfter( true );
         showAnimation.setInterpolator( new AccelerateInterpolator() );
         adView2.startAnimation( showAnimation );
       

Note: I also added "adView2" to my main.xml, etc. etc.

Question
: As you can see, I'm using an "AlphaAnimation" item to give the effect of it fading in with the rest of the screen. But, as mentioned, I can't make it fade out upon death. Attempts include: calling a new  AlphaAnimation of fading out upon Deconstruct, and GameFlowEvent.EVENT_END_GAME, but it still sticks around as other items fade.

I also tried creating a new method that runs the new animation for adView2 to fade when the player dies, but i get this runtime error: "Only the original thread that created a view hierarchy can touch its views.”


Bonus Question: Adding "setBackgroundResource(R.anim.ui_button);" was the only way I could get the ad to actually appear on the screen.  Can anyone tell me why that's the case? Because i know it's not proper coding :-/. Could this be the cause of my problem?


Bart Hirst

unread,
Jun 9, 2014, 9:26:36 AM6/9/14
to ReplicaIsland Coding Community
Solution: remove AdMob  ;)

--
You received this message because you are subscribed to the Google Groups "ReplicaIsland Coding Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to replica-island-coding...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

JF

unread,
Jun 9, 2014, 7:07:31 PM6/9/14
to replica-island-...@googlegroups.com
Hahaha! For what it's worth, this isn't some get-rich-quick attempt at app development, lol.  It's very much a learning experience.  If I'm still enjoying myself by the time this app's published, I'm starting a new one from scratch (not a game).


Anyway, any thoughts on how he's getting the screen to fade out? :)
Solution: remove AdMob  ;)
To unsubscribe from this group and stop receiving emails from it, send an email to replica-island-coding-community+unsubscribe@googlegroups.com.

Bart Hirst

unread,
Jun 10, 2014, 8:44:46 AM6/10/14
to ReplicaIsland Coding Community

Is it just using an Android API? Seriously search for fadeout in the code

To unsubscribe from this group and stop receiving emails from it, send an email to replica-island-coding...@googlegroups.com.

JF

unread,
Jun 10, 2014, 9:24:15 AM6/10/14
to replica-island-...@googlegroups.com
I've been sifting through code with "fade" in it for a day or two now, haha (again, I'm new here).

I did make some progress last night though: it looks the Thesourcecomponent class is calling a method in the HudSystemdSystem class that starts to fade the on screen bitmap items. Then once fading is done, it changes the game event and eventually stops the game.

I'm hoping with this info, I'll be able to get that fade method to somehow apply to the ad as well. Will circle around once I have a solution (hopefully tonight, lol)

Thanks!

JF

unread,
Jun 11, 2014, 12:26:34 AM6/11/14
to replica-island-...@googlegroups.com
Whhhhelp, I have yet another item to add to my growing list of 'half-assed-amateur-hour-solutions-that-seemingly-work', lol.  In case anyone's still reading these threads and has a similar question:

When the player dies, the PlayerComponent class calls the "startFade" and "sendGameEventOnFadeComplete" methods in the HudSystem class. I couldn't figure out how to simply integrate the my AdView object into these methods (for multiple reasons including ignorance, lol. This would probably be much cleaner).

Instead,
1. Now the PlayerCompent class does two things (1) it sends the HudSystem class the request to change game event after fade is complete (as was the case before), but now it also tries (2) changing the game event itself without waiting for the fade
via: 
LevelSystem level = sSystemRegistry.levelSystem;
level.sendGameEvent(GameFlowEvent.EVENT_END_GAME, 0, true);
                   
2. AndouKun (which conducts the action based on the Game event) now uses the following for "Event_End_Game":

  if(!adFade){
               runOnUiThread(new Runnable() {
                    public void run() {
                       if(!adFade){
                        showAnimation = new AlphaAnimation( 1.0f, 0.0f );
                        showAnimation.setDuration( 1000 );
                        showAnimation.setFillAfter( false );

                        showAnimation.setInterpolator( new AccelerateInterpolator() );
                        adView2.startAnimation( showAnimation );
                           adView2.setVisibility(View.GONE);   
                       }
                  }
              });}else{
              
              
                    mGame.stop();
                    finish();

}

3. Lastly, HudSystem now flags the variable above adFade to true after the fade.

In other words, PlayerComponent now directly changes GameEvent, but in doing so, only triggers the AdView to begin fading and not the game to actually end since my adFade variable is still false.  Then Once Hudsystem is done fading, it flags adFade to true, and retries changing the Game Event which now finishes actually ending the game. The " runOnUiThread(new Runnable()" functionality was what really brought it together for me, as it allowed me to manipulate a View after it was created, which was a major headache until now.


Anyway, if anyone does read this and has a question, feel free to reach out.  
Reply all
Reply to author
Forward
0 new messages