Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using the AnalogOnScreenControl to move my sprite around the screen. I would like to restrict the sprite so it can not move outside the screen boundaries.

I have tried putting a line at the top of the screen and checking collisions between the sprite and the line, but the effect is that the sprite sticks to the top line. It does not move at that point. So I tried the onAccelerometerChanged() method as an alternative, but that also doesn't work.

How do I detect the screen boundary and prevent the sprite from going beyond it?

share|improve this question
2  
Why not simply test if your sprite bounds go outside your world boundaries? It doesn't have to be done with OpenGL. –  Sidar Feb 26 '13 at 11:09
1  
@Sidar question didn't really call for world boundaries, called for screen boundaries, and he didn't say anything about OpenGL. Am I missing something? –  Cameron Fredman Feb 26 '13 at 22:23
    
@CameronFredman GLES = OpenGL ES. The question implies it was OpenGL ES bound. It's not much relevant to anything GLES1. He simply needs to check boundaries of whatever he is keeping track of. Screen/world all included. It was a case of semantics here. Nothing more nothing less. My point still stands regardless of the space he is in. –  Sidar Feb 27 '13 at 5:49
    
@Sidar his original question referred to "AndEngine gles1". AndEngine named their versions GLES1 and GLES2. Run a google search for "AnalogOnScreenControl" and you'll see the entries take you straight into AndEngine code, plus he had the andengine tag on the original post. Don't think he was asking an opengl question, except to the extent andengine happens to use opengl. –  Cameron Fredman Feb 27 '13 at 5:55
    
@CameronFredman If andengine is tagged then it shouldn't be in the title. Obviously it confused me since I don't work with AndEngine nor do I know of the 2 different builds. But that's just me. –  Sidar Feb 27 '13 at 5:58

1 Answer 1

up vote 3 down vote accepted

What you're asking for is a subset of collision handling. Essentially, when you've moved the player outside of the scene, you want to move the player back into the scene, and you want to do that in the same update as you've moved the player, immediately after moving the player, so the out-of-scene location never gets rendered.

Lets assume you do something like this to move the sprite:

physicsHandler.setVelocity(pValueX*100, pValueY*100);

Now you want to move them back into the scene if they're outside it. So we're going to need a new method. Lets throw in a method call right after moving the sprite (we'll call it "adjustToSceneBinding()" so you have this:

physicsHandler.setVelocity(pValueX*100, pValueY*100);
adjustToSceneBinding(player);

Sidenote, you can do the adjustToSceneBinding() method from inside or outside the Sprite. Doing it from inside (i.e., have sprite be a class that extends sprite) is probably more proper, but for our purposes doesn't matter. Now the adjustToSceneBinding code:

public boolean adjustToSceneBinding(Sprite player) {

    // Correct the X Boundaries.
    if (player.getX() < 0) {
        player.setX(0);
    } else if (player.getX() + player.getWidth() > CAMERA_WIDTH) {
        player.setX(CAMERA_WIDTH - player.getWidth());
    }
}

If the sprite's X coordinate is less than zero, move them back to zero. If the sprite's X PLUS the player's width is more than the camera width, move them back to the camera width minus the player width. Don't forget the player X coordinate represents the left side of the sprite. That's why I'm taking into account the width in addition to the position for calculating the right side.

The above code only addresses left/right movement, but I'm going to let you extrapolate how to do the same for up down.

share|improve this answer
    
Thank you sooo..... much. It's working properly.Very nice logic.And thanks for giving me chance for making up/down code through that i understand that how it works. –  Bin Feb 27 '13 at 6:53
    
@Bin You got it, buddy. –  Cameron Fredman Feb 27 '13 at 21:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.