Movement in gage

I need help implimenting sprite movement in gage.

I can’t seem to get the sprite to stop at map boundaries.

here is some code, but the sprite still goes past the map. Also I can only return to the sprites origin. i.e. if
i start at 400,300, I cant walk back to 0,0.

public static void render(Graphics g)
   {
       if(parallax != null)
       {
            parallax.render(g);
           // heroSprite.render(g,  -parallax.getX(), -parallax.getY());
            heroSprite.render(g,  0, 0);
            
         
            if(heroSprite.getX() < parallax.getX()+parallax.getWidth() )
            {
                //Move fighter                     
                heroSprite.setX(heroSprite.getX()+dirx+40);
            }
            if(heroSprite.getY() < parallax.getPixelsY())
            {
                heroSprite.setY(heroSprite.getY()+diry+40);
            }

            
       }
   }

When you are checking to see if they are going to go out of bounds, you are forgetting to add the dirx and diry variables.

Should look more like this:


public static void render(Graphics g) 
   { 
  if(parallax != null) 
  { 
  parallax.render(g); 
      // heroSprite.render(g,  -parallax.getX(), -parallax.getY()); 
  heroSprite.render(g,  0, 0); 
   
     
  if(heroSprite.getX()+dirx+40 < parallax.getX()+parallax.getWidth() ) 
  { 
      //Move fighter       
      heroSprite.setX(heroSprite.getX()+dirx+40); 
  } 
  if(heroSprite.getY()+diry+40 < parallax.getPixelsY()) 
  { 
      heroSprite.setY(heroSprite.getY()+diry+40); 
  } 
 
   
  } 
   }