Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to keep sprite from moving off of the screen.  (Read 4794 times)

0 Members and 1 Guest are viewing this topic.

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
Trying to keep sprite from moving off of the screen.
« on: May 21, 2011, 04:29:56 am »
EDIT: I've figured out why it doesn't work with using a simple cout to print it's coordinates. I'm curious to why it's the opposite of what I thought. As in why when you go up the y coordinate goes down and vice versa. Is there some mathematics or subject that will help me better understand this? I don't want a simple problem like this holding me back anymore :(, as I have bigger fish to fry in the future. Or is this simply how the library is?

I'm making a basic little unrefined game.  :D
I'm having trouble with making sure these sprites don't move off the screen.

My current code is this, but it fails obviously.

Code: [Select]

sf::Vector2f b1Pos = BOARD1.GetPosition();
if (App.GetInput().IsKeyDown(sf::Key::Up) && b1Pos.y < 600.f) BOARD1.Move(0, -200 * ElapsedTime);


this doesn't keep it from moving off the screen while the height of the screen is 600.

Also if this is an easy problem, just tell me why this doesn't work and maybe a few hints to figuring out what will.

TehKyle

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trying to keep sprite from moving off of the screen.
« Reply #1 on: May 21, 2011, 10:43:36 am »
You have to contain within the 0,0 and the window width, window height

Code: [Select]
       //Set camera within the boundary
        if (CamX < 0)
            CamX = 0;
        if (CamX >= MapMaxX - SCREENX)
            CamX = MapMaxX - SCREENX;
        if (CamY < 0)
            CamY = 0;
        if (CamY >= MapMaxY - SCREENY)
            CamY = MapMaxY - SCREENY;

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Trying to keep sprite from moving off of the screen.
« Reply #2 on: May 21, 2011, 11:23:21 am »
Quote from: "Code_Machine"
EDIT: I've figured out why it doesn't work with using a simple cout to print it's coordinates. I'm curious to why it's the opposite of what I thought. As in why when you go up the y coordinate goes down and vice versa. Is there some mathematics or subject that will help me better understand this? I don't want a simple problem like this holding me back anymore :(, as I have bigger fish to fry in the future. Or is this simply how the library is?

There's nothing fancy to it, really. It's just that the origin is at the upper left corner and the y-axis is flipped. That's the way it is for almost all libraries and programming languages. I guess it's for practical reasons. Anyway, there's no fancy math behind it or anything like that. Just get used to it and you'll be fine. :)

Code_Machine

  • Newbie
  • *
  • Posts: 13
    • View Profile
Trying to keep sprite from moving off of the screen.
« Reply #3 on: May 21, 2011, 11:41:32 am »
Thanks for your answers; you guys helped a great deal! :)

 

anything