Adjusting a background's size within the script?

Started by Feenyx, Wed 15/10/2014 20:39:11

Previous topic - Next topic

Feenyx

Quote from: Snarky on Sat 18/10/2014 21:09:49
Can't reproduce: for me the screen scrolls to follow the character. What values do you have for the sidebars? (Note that if for example your right-hand sidebar is 50 pixels wide on a 900-pixel wide background, the SidebarRight value should be 50, not 850.)


Ugh, my bad. Got that fixed now. However, one last thing is the character seems to flicker like crazy, as much as the repeatable execute function occurs. Won't that affect my walk animation? (currently not animated)

Snarky

#21
Hmmyes, I see what you mean. It's a bug/limitation of SetViewport(), it only updates the viewport one frame after the character position is updated, so the character flickers between two positions on the screen. There is a workaround that might fix this (see here), but it gets a bit complicated. I'll see if I can add it in...

Edit: Try this. Since the code was getting mildly complicated, I broke it out into a separate function.

Code: AGS
// GlobalScript.asc
int animationCounter=0;
int animationFrameOld;

void scrollWithBoundaries()
{
  animationCounter++;
  if(animationFrameOld != player.Frame)
    animationCounter=0;
  if(animationCounter == player.AnimationSpeed || !player.MovementLinkedToAnimation)
  {
    // Scroll viewport so player is at the center of the screen (default behavior)
    int viewportX = player.x - System.ViewportWidth/2;
    int viewportY = player.y - System.ViewportHeight/2;
    // Don't scroll into left sidebar
    if(viewportX < GetRoomProperty("SidebarLeft"))
      viewportX = GetRoomProperty("SidebarLeft");
    // Don't scroll into right sidebar
    else if(viewportX + System.ViewportWidth >= Room.Width - GetRoomProperty("SidebarRight"))
      viewportX = Room.Width - System.ViewportWidth - GetRoomProperty("SidebarRight");

    SetViewport(viewportX, viewportY);
  }
  animationFrameOld = player.Frame;
}

function repeatedly_execute_always()
{
  scrollWithBoundaries();
}

Feenyx

Quote from: Snarky on Sat 18/10/2014 21:30:34
Hmmyes, I see what you mean. It's a bug/limitation of SetViewport(), it only updates the viewport one frame after the character position is updated, so the character flickers between two positions on the screen. There is a workaround that might fix this (see here), but it gets a bit complicated. I'll see if I can add it in...

Edit: Try this. Since the code was getting mildly complicated, I broke it out into a separate function.

Code: AGS
// GlobalScript.asc
int animationCounter=0;
int animationFrameOld;

void scrollWithBoundaries()
{
  animationCounter++;
  if(animationFrameOld != player.Frame)
    animationCounter=0;
  if(animationCounter == player.AnimationSpeed || !player.MovementLinkedToAnimation)
  {
    // Scroll viewport so player is at the center of the screen (default behavior)
    int viewportX = player.x - System.ViewportWidth/2;
    int viewportY = player.y - System.ViewportHeight/2;
    // Don't scroll into left sidebar
    if(viewportX < GetRoomProperty("SidebarLeft"))
      viewportX = GetRoomProperty("SidebarLeft");
    // Don't scroll into right sidebar
    else if(viewportX + System.ViewportWidth >= Room.Width - GetRoomProperty("SidebarRight"))
      viewportX = Room.Width - System.ViewportWidth - GetRoomProperty("SidebarRight");

    SetViewport(viewportX, viewportY);
  }
  animationFrameOld = player.Frame;
}

function repeatedly_execute_always()
{
  scrollWithBoundaries();
}


I tried this out and placed it on top of all my Global scripts functions. However, I think I might have placed it in the wrong place since it won't seem to work anymore. The camera just keeps following the main character at all times and still shows the black bars but no flickers. As if it was not considering it as a function since I removed it from the repeatable execute always section (which now contains scrollWithBoundaries). Also, I'm not getting any error messages. What did I do wrong?:~(

Snarky

I don't know, dude. When I first tested it I had the same problem, and my mistake was that I'd forgotten to actually call scrollWithBoundaries() in repeatedly_execute_always(), but if you ARE doing that...

Wait, you say "placed it on top of all my Global scripts functions"? There's only one copy, right? You haven't ended up with two separate repeatedly_execute_always() functions, for example?

If you can't get it to work, you could post or send me the contents of your global script.

SMF spam blocked by CleanTalk