Super jumper! Touch screen loops?

Forum for the book "Beginning Android Games", see http://www.apress.com/9781430230427

Super jumper! Touch screen loops?

Postby GamersHeaven » Tue Jun 28, 2011 8:50 pm

Have bin going thru the super jumper sample for a while now and noticed something strange?

Is there an reason to that you put all the screen touch events inside the loop that gets the cordinates for the screen touch?

I experimented a little bit with the main menue screen button press loop! And got an more responsive menue with this!

Code: Select all
@Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        Boolean TouchScreen=false;
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                       
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                TouchScreen=true;
                break;
            }
         }
            if(TouchScreen==true){
                if(OverlapTester.pointInRectangle(playBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new GameScreen(game));
                    return;
                }
                if(OverlapTester.pointInRectangle(highscoresBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new HighscoresScreen(game));
                    return;
                }
                if(OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new HelpScreen(game));
                    return;
                }
               
                if(OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    Settings.soundEnabled = !Settings.soundEnabled;
                    if(Settings.soundEnabled)
                        Assets.music.play();
                    else
                       Assets.music.pause();
                }
                if(OverlapTester.pointInRectangle(ExitBounds, touchPoint))
                   QuitGame=true;
            }
       
           if(QuitGame==true)
              ExitGame();
       
    }


As you can see so did i keep an minimal amount of code inside the touch event loop.
Its size is decided by how big the screen size is?
I know its nothing in an basic game like this but should save a few fps ingame?
Or are i completely wrong?
I want some answers from my android guru :D

Iam thinking of putting all the overlap test calls in an private void and only calling them when someone actually presses the screen :D
GamersHeaven
 
Posts: 126
Joined: Wed May 25, 2011 8:33 pm
Location: Sweden.

Re: Super jumper! Touch screen loops?

Postby dohkoxar » Wed Jun 29, 2011 6:19 am

Let me try to get this hmm. You are trying to save some processing power by only going through all the overlapping detection by only having it go through it if your TouchScreen is true. But everytime you go through all the touch events with get(i), you are already implying the screen was touched, so you will still go through every overlap method evertime you update the screen.

Maybe I just don't understand what you are trying to say :(
dohkoxar
 
Posts: 12
Joined: Thu May 26, 2011 8:43 pm

Re: Super jumper! Touch screen loops?

Postby GamersHeaven » Wed Jun 29, 2011 3:16 pm

dohkoxar wrote:Let me try to get this hmm. You are trying to save some processing power by only going through all the overlapping detection by only having it go through it if your TouchScreen is true. But everytime you go through all the touch events with get(i), you are already implying the screen was touched, so you will still go through every overlap method evertime you update the screen.

Maybe I just don't understand what you are trying to say :(

Not at all!
But yes iam looking thru various ways of optimizing marios great code to learn some more :D
I whas asking why all of this whas inside the loop also!

if(TouchScreen==true){
if(OverlapTester.pointInRectangle(playBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new GameScreen(game));
return;
}
if(OverlapTester.pointInRectangle(highscoresBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new HighscoresScreen(game));
return;
}
if(OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new HelpScreen(game));
return;
}

if(OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
Settings.soundEnabled = !Settings.soundEnabled;
if(Settings.soundEnabled)
Assets.music.play();
else
Assets.music.pause();
}
if(OverlapTester.pointInRectangle(ExitBounds, touchPoint))
QuitGame=true;
}


I dont fully understand how android actually works !
But i have bin told to keep the lines of code inside an loop to an minimal.
Iam trying to drain mario on as much knowledge as possible :D

This is the original code!
Where the loop cycle neads to chew thru all these lines in every cycle!
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();

int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
touchPoint.set(event.x, event.y);
guiCam.touchToWorld(touchPoint);

if(OverlapTester.pointInRectangle(playBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new GameScreen(game));
return;
}
if(OverlapTester.pointInRectangle(highscoresBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new HighscoresScreen(game));
return;
}
if(OverlapTester.pointInRectangle(helpBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new HelpScreen(game));
return;
}
if(OverlapTester.pointInRectangle(soundBounds, touchPoint)) {
Assets.playSound(Assets.clickSound);
Settings.soundEnabled = !Settings.soundEnabled;
if(Settings.soundEnabled)
Assets.music.play();
else
Assets.music.pause();
}
}
}
}


In my modification so does it only nead to go thru these lines in every cycle!

@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
Boolean TouchScreen=false;
int len = touchEvents.size();
for(int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if(event.type == TouchEvent.TOUCH_UP) {
touchPoint.set(event.x, event.y);
guiCam.touchToWorld(touchPoint);
TouchScreen=true;
break;
}
}

if(TouchScreen==true)
Buttons();

if(QuitGame==true)
ExitGame();

}

It does the same thing?
Iam an total noob and wondered if iam forced to do it the original way to not get any undesired hangups or buggs later on?
It more or less that i noticed an frame rate drop of 10-20 fps as soon as i touched the screen?
GamersHeaven
 
Posts: 126
Joined: Wed May 25, 2011 8:33 pm
Location: Sweden.

Re: Super jumper! Touch screen loops?

Postby mzechner » Thu Jun 30, 2011 1:22 pm

Functionally my and your code do exactly the same. Also, the overlap tests are only executed once, when a TOUCH_UP event is seen. The for loop is then exited in 3 out of the 4 cases. I would be surprised if you gained any additional speed increase :)

The reason i put the code inside the for (and if block) is that it logically belongs there. For every touch event i check if it is of type TOUCH_UP, if it is i perform the overlap testing. My brain just works like that :p
mzechner
Site Admin
 
Posts: 4715
Joined: Sat Jul 10, 2010 3:50 pm

Re: Super jumper! Touch screen loops?

Postby GamersHeaven » Thu Jun 30, 2011 2:47 pm

mzechner wrote:Functionally my and your code do exactly the same. Also, the overlap tests are only executed once, when a TOUCH_UP event is seen. The for loop is then exited in 3 out of the 4 cases. I would be surprised if you gained any additional speed increase :)

Thanks :D
Some of the answers i whas looking for ;)
mzechner wrote:The reason i put the code inside the for (and if block) is that it logically belongs there. For every touch event i check if it is of type TOUCH_UP, if it is i perform the overlap testing. My brain just works like that :p

Yes but you are an programmer :D
And iam not :oops:
But its fun!
GamersHeaven
 
Posts: 126
Joined: Wed May 25, 2011 8:33 pm
Location: Sweden.

Re: Super jumper! Touch screen loops?

Postby mzechner » Thu Jun 30, 2011 6:34 pm

you are a programmer! Just take your time to get comfortable with this new language. The initial jump from Basic to Java can be harsh, but you seem to do a great job so far :)
mzechner
Site Admin
 
Posts: 4715
Joined: Sat Jul 10, 2010 3:50 pm


Return to Beginning Android Games

Who is online

Users browsing this forum: Bing [Bot] and 1 guest