Viewport won't move

Started by Secret Fawful, Sun 13/04/2014 09:04:17

Previous topic - Next topic

Secret Fawful

So- I've been at this for a few days now, and I'm about at the end of my rope. I have an object that leaves one room, moves across another room, and ends up in a third room. You can move the object from one room to another. From Room 1 to Room 3, and from Room 3 to Room 1.

The issue I'm having is in Room 2. I want to cut to that room, and have the camera follow the object across the room depending on which side it enters from.

By tweaking the code, I've managed to get the camera to pan from the left side of the room to the right side. The problem comes in when I try to get it to pan from the right side of the room to the left side. It just won't do it, no matter what variation of the code I try. I'll post the code here. I've been told this code should work, but the camera stays completely still on the right side of the room.

Code: ags
// room script file
int xpos = 0;
    
function room_Load()
{
  SmoothScroll_ScrollingOn();
  
  if (cartroom == 0)
  {
    xpos = 1000;
    cEgo.x = 1000;
    cEgo.Transparency = 100;
    oCart.X = 1056;
  }
  
  if (cartroom == 1)
  {
    xpos = 0;
    cEgo.x = 0;
    cEgo.Transparency = 100;
    oCart.X = -56;
  }
  
  if (cartroom == 2)
  {
    cEgo.Transparency = 0;
    oCart.Visible = false;
    oCart.Clickable = false;
  }
}

function room_AfterFadeIn()
{
  if (cartroom == 0)
  {
    oCart.Move(-58, oCart.Y, 5, eBlock, eAnywhere);
    cEgo.ChangeRoom(13, 156, 124);
  }
  
  if (cartroom == 1)
  {
    oCart.Move(1000, oCart.Y, 5, eBlock, eAnywhere);
    cEgo.ChangeRoom(71, 298, 388);
  }
}

function repeatedly_execute_always()
{  
  if (cartroom == 0)
  {
    SetViewport(xpos, 0);
    xpos--;
  }
    
  if (cartroom == 1)
  {
    SetViewport(xpos, 0);
    xpos++;
  }
  
  if (!oCart.Moving)
  {
    ReleaseViewport();
  }
}


I'd greatly appreciate any help with this. Oh, and I considered making the object a character instead and setting that to the player character, having the camera follow it across the room that way, but I wanted to learn how to control the camera via SetViewport, and this code should work. Somewhere there's a user error on my part.

I've also tried a variation that has this code instead-

Code: ags
function repeatedly_execute_always()
{  
  if (!oCart.Moving)
  {
    ReleaseViewport();
  }
}

function room_RepExec()
{
 if (cartroom == 0)
  {
    SetViewport(xpos, 0);
    Wait(1);
    xpos--;
  }
    
  if (cartroom == 1)
  {
    SetViewport(xpos, 0);
    Wait(1);
    xpos++;
  }
}

Andail

Why won't you set the viewport to the object's position directly?
So under repeatedly_execute_always you put
SetViewport (oCart.X, oCart.Y);
You just need to adjust the coordinates so that the object appears in the middle, obviously.


Secret Fawful

Quote from: Andail on Sun 13/04/2014 10:44:47
You just need to adjust the coordinates so that the object appears in the middle, obviously.

I don't know how to do this. I've tried to figure it out, but at this point it's like a puzzle. I did try setting it to the object's coordinates, but the camera still didn't move when I called SetViewport, so I figured I must be missing something.

Snarky

To get the object in the middle of the screen, set the viewport coordinates to the object coordinates minus half the viewport width/height. (If the object is big, you might also want to add in half the object width/height.)

SMF spam blocked by CleanTalk