Scrolling screen background issues

Started by jamesreg, Sun 03/02/2019 03:40:25

Previous topic - Next topic

jamesreg

Normal game size room is 480x270
scrolling background is 3000x3000

jamesreg

#21
Current room code is this I may have missed a change or got confused in the two replies and was unsure which one to change
getting the

Failed to save room room1.crm; details below
room1.asc(16): Error (line 16): Variable 'wrapWidth' is already defined

I was confused rather to change this or not as you both spoke of it

Code: ags
// room script file
 
// Some handy variables
 int centerX, centerY, wrapWidth=1, wrapHeight=1, viewportX, viewportY;
 
//BEFORE ROOM LOAD SCRIPTS
function room_Load()
{
 
  cEgo.Transparency=100; //Make Character invisible 
  Bridge_Battle=true;    //Put room in Battle mode starting
 
  // Just calculate some useful values:
  centerX = System.ViewportWidth/2;
  centerY = System.ViewportHeight/2;
  int wrapWidth = Room.Width - System.ViewportWidth;
  int wrapHeight = Room.Height - System.ViewportHeight;
}
 
// Moved all the stuff to do with the battle into its own function, to keep things tidy
void updateBattle()
{
  // TODO: Update ships and stuff
 
  mouse.Update(); // This makes sure we get the most up-to-date mouse coordinates
  // Calculate viewport scrolling
  int moveX = mouse.x - centerX;
  int moveY = mouse.y - centerY;
 
  // Move viewport, wrapping around
  viewportX = (viewportX + moveX + wrapWidth) % wrapWidth;
  viewportY = (viewportY + moveY + wrapWidth) % wrapWidth;
  SetViewPort(viewportX, viewportY);
}
 
//scripts that repeatedly fire
function room_RepExec()
{
  // Testing script to exit game 
  if (IsKeyPressed(eKeyEscape)) {
    QuitGame(0);
  }
  //End of Testing Script  
  
  // If Z key is pressed and mode is battle mode
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==true) {
 
    Display("Navigation mode off."); // Display message that Scrolling mode is off
    mouse.Visible=true;     // Make mouse visible
    gGui2.Visible=false;     // Make target sight invisible
    Bridge_Battle=false;    // Turn Battle mode off and restore full screen mouse movement
    Mouse.Mode=eModePointer; // Turns mouse back into pointer mode
  }
 
  // If Z key is pressed and battle mode is off
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==false) {
    Display("Navigation Mode on.");  //Display Message Scrolling mode is on
    Bridge_Battle=true; //Turn battle mode and scrolling back on
 
    viewportX = GetViewportX();
    viewportY = GetViewportY();
 
    // Turn off mouse and turn on target sight
    gGui2.Visible=true;
    mouse.Visible = false;
    // Center the (now invisible) mouse on the screen
    mouse.SetPosition(centerX, centerY);
  }
 
  if(Bridge_Battle)
    updateBattle();
}

Crimson Wizard

Quote from: jamesreg on Tue 05/02/2019 21:51:19

Failed to save room room1.crm; details below
room1.asc(16): Error (line 16): Variable 'wrapWidth' is already defined

I was confused rather to change this or not as you both spoke of it

Yes, "int" need to be removed from that place.

jamesreg

#23
yippie This compiled

But still some issues:

1) Screen is scrolling BY Itself all crazy the stars are just zipping by lol
2) The GUI target cursor is at top left side of window not centered
3) The mouse is viable on screen and do not think it is staying center because i can get the top pull down menu to pop up meaning its going up there.

It would seem it does indeed follow the mouse but it never stops moving that direction you are going.

I think I know what it is correct me if I am wrong but I have the code to change battle mode and cursor mode with the z button to switch back and forth in modes. But in rep execute I do not have it defined how to set up battle mode in the first place does this look to be the problem?

Code: ags
// room script file
 
// Some handy variables
  
int centerX, centerY,  wrapWidth=1, wrapHeight=1, viewportX, viewportY;
//BEFORE ROOM LOAD SCRIPTS
function room_Load()
{
 
  cEgo.Transparency=100; //Make Character invisible 
  Bridge_Battle=true;    //Put room in Battle mode starting
 
  // Just calculate some useful values:
  centerX = System.ViewportWidth/2;
  centerY = System.ViewportHeight/2;
   wrapWidth = Room.Width - System.ViewportWidth;
   wrapHeight = Room.Height - System.ViewportHeight;
}
 
// Moved all the stuff to do with the battle into its own function, to keep things tidy
void updateBattle()
{
  // TODO: Update ships and stuff
 
  mouse.Update(); // This makes sure we get the most up-to-date mouse coordinates
  // Calculate viewport scrolling
  int moveX = mouse.x - centerX;
  int moveY = mouse.y - centerY;
 
  // Move viewport, wrapping around
  viewportX = (viewportX + moveX + wrapWidth) % wrapWidth;
  viewportY = (viewportY + moveY + wrapWidth) % wrapWidth;
  SetViewport(viewportX, viewportY);
}
 
//scripts that repeatedly fire
function room_RepExec()
{
  // Testing script to exit game 
  if (IsKeyPressed(eKeyEscape)) {
    QuitGame(0);
  }
  //End of Testing Script  
  
  // If Z key is pressed and mode is battle mode
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==true) {
 
    Display("Navigation mode off."); // Display message that Scrolling mode is off
    mouse.Visible=true;     // Make mouse visible
    gGui2.Visible=false;     // Make target sight invisible
    Bridge_Battle=false;    // Turn Battle mode off and restore full screen mouse movement
    Mouse.Mode=eModePointer; // Turns mouse back into pointer mode
  }
 
  // If Z key is pressed and battle mode is off
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==false) {
    Display("Navigation Mode on.");  //Display Message Scrolling mode is on
    Bridge_Battle=true; //Turn battle mode and scrolling back on
 
    viewportX = GetViewportX();
    viewportY = GetViewportY();
 
    // Turn off mouse and turn on target sight
    gGui2.Visible=true;
    mouse.Visible = false;
    gGui2.Centre();
    // Center the (now invisible) mouse on the screen
    mouse.SetPosition(centerX, centerY);
  }
 
  if(Bridge_Battle)
    updateBattle();
}

Snarky

#24
Quote from: jamesreg on Tue 05/02/2019 22:28:44
yippie This compiled

Great!

Quote from: jamesreg on Tue 05/02/2019 22:28:44
But still some issues:

1) Screen is scrolling BY Itself all crazy the stars are just zipping by lol

It would seem it does indeed follow the mouse but it never stops moving that direction you are going.

Another mistake in my code. I forgot to add the line that resets the mouse position to the middle each turn. At the very end of updateBattle(), after SetViewport(viewportX, viewportY); add the line mouse.SetPosition(centerX, centerY);

Quote2) The GUI target cursor is at top left side of window not centered
The GUI is wherever you position it. Just put it where you want.

QuoteBut in rep execute I do not have it defined how to set up battle mode in the first place does this look to be the problem?
Quote3) The mouse is visable on screen.

You're on the right track. The problem is not in Rep_Exec but in room_Load(). When we set Bridge_Battle to true we should also set all the other settings. The best thing would be to move all the code for turning it on and off into a function, e.g.:

Code: ags
// variables as before

void setBattleMode(bool doBattle)
{
  if(doBattle != Bridge_Battle) // If set to mode we're already in, ignore
  {
    if(doBattle)
    {
      Display("Navigation Mode on.");  //Display Message Scrolling mode is on
 
      viewportX = GetViewportX();
      viewportY = GetViewportY();
 
      // Turn off mouse and turn on target sight
      gGui2.Visible=true;
      gGui2.Centre();
      mouse.Visible = false;
      // Center the (now invisible) mouse on the screen
      mouse.SetPosition(centerX, centerY);
    }
    else
    {
      Display("Navigation mode off."); // Display message that Scrolling mode is off
      mouse.Visible=true;     // Make mouse visible
      gGui2.Visible=false;     // Make target sight invisible
      Mouse.Mode=eModePointer; // Turns mouse back into pointer mode
    }
    Bridge_Battle = doBattle;
  }
}

function room_Load()
{
  cEgo.Transparency=100; //Make Character invisible 
 
  // Just calculate some useful values:
  centerX = System.ViewportWidth/2;
  centerY = System.ViewportHeight/2;
  wrapWidth = Room.Width - System.ViewportWidth;
  wrapHeight = Room.Height - System.ViewportHeight;

  // Start in battle mode
  setBattleMode(true);
}

// updateBattle() as before (with previous fixes)

function room_RepExec()
{
  // Testing script to exit game 
  if (IsKeyPressed(eKeyEscape)) {
    QuitGame(0);
  }
  //End of Testing Script  
  
  // If Z key is pressed, toggle battle mode
  if (IsKeyPressed(eKeyZ))
    setBattleMode(!Bridge_Battle);  // Toggle battle mode on/off

  if(Bridge_Battle)
    updateBattle();
 }

jamesreg

#25
Good news Its working and everything seems fine and I even started using functions instead of a lot of repeated code and junk code thanks for the help.

I still have the bug where I have to use a display command and clear it when toggling between the battle mode and cursor mode.
I do not know why it is doing that.

Code: ags
// room script file

// Some handy variables
  function Nav_Mode () 
{
  Bridge_Battle=true;
  mouse.Visible=false;
  gGui2.Visible=true;
  gGui2.Centre();
  cEgo.Transparency=100;
 
   
}
  
function Cursor_Mode ()
{
  Bridge_Battle=false;
  Mouse.Mode=(eModePointer);
  mouse.Visible=true;
  gGui2.Visible=false;
  cEgo.Transparency=100;
  
}

int centerX, centerY,  wrapWidth=1, wrapHeight=1, viewportX, viewportY;
//BEFORE ROOM LOAD SCRIPTS
function room_Load()
{
 BattleMode=true;
  Nav_Mode();
  
 
  // Just calculate some useful values:
  
  centerX = System.ViewportWidth/2;
  centerY = System.ViewportHeight/2;
   wrapWidth = Room.Width - System.ViewportWidth;
   wrapHeight = Room.Height - System.ViewportHeight;
    
}
 
// Moved all the stuff to do with the battle into its own function, to keep things tidy
void updateBattle()
{
  // TODO: Update ships and stuff
 
  mouse.Update(); // This makes sure we get the most up-to-date mouse coordinates
  // Calculate viewport scrolling
  int moveX = mouse.x - centerX;
  int moveY = mouse.y - centerY;
 
  // Move viewport, wrapping around
  viewportX = (viewportX + moveX + wrapWidth) % wrapWidth;
  viewportY = (viewportY + moveY + wrapWidth) % wrapWidth;
  SetViewport(viewportX, viewportY);
  mouse.SetPosition(centerX, centerY);
}
 
//scripts that repeatedly fire
function room_RepExec()
{
  // Testing script to exit game 
  if (IsKeyPressed(eKeyEscape)) {
    QuitGame(0);
  }
  //End of Testing Script  
  
  if (Bridge_Battle==true) {
    Nav_Mode();
  }
  if (Bridge_Battle==false) {
    Cursor_Mode();
  }
  
  // If Z key is pressed and mode is battle mode
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==true) {
      Cursor_Mode();
     
    Display("Navigation mode off."); // Display message that Scrolling mode is off
  }
 
  // If Z key is pressed and battle mode is off
  if (IsKeyPressed(eKeyZ) && Bridge_Battle==false) {
    Nav_Mode();
    Display("Navigation Mode on.");  //Display Message Scrolling mode is on
    
    mouse.SetPosition(centerX, centerY);
  }
 
  if(Bridge_Battle)
    updateBattle();
}
}

Crimson Wizard

jamesreg, I noticed this line in your code, in function Cursor_Mode ():
Code: ags

MouseMode=(eModePointer);


I think it should probably be instead:

Code: ags

Mouse.Mode=eModePointer;


jamesreg

I fixed that already still was not the issue with why a display statement has to be made after hitting the z hotkey to toggle the battle and cursor modes on and off.  That bug has been there in my old code before you guys helped and even now after we have tidy it up and tightened it up.
I don't know whats causing it. Id rather just hit z and toggle modes without any pop up.

Snarky

First, looking at your code there's one problem that jumps out: you're setting Bridge_Battle both inside the Nav_Mode() and Cursor_Mode() functions, and before/after you call them. Don't do that! If the value is set in the functions, don't change it anywhere else, but always use the functions to change it. That way you ensure that the value of Bridge_Battle always matches the other settings.

Similarly, you should be disciplined about putting everything that has to do with switching between the modes inside the Nav_Mode()/Cursor_Mode() functions. So line 87 in the code you posted should not be part of room_RepExec(), but should go in the Nav_Mode() function. (Put the Display() call before the Nav_Mode() call to avoid the cursor moving after you center it.)

These things may seem minor, but they are actually really important in the long run. Notice how in the code I posted, the room_RepExec() bit that switches between the modes is just two lines, while in yours it's 20. (BTW, lines 68-73 in your code are wrong and should be deleted. The Nav_Mode()/Cursor_Mode() functions are only for switching modes, and you shouldn't call them constantly to switch to the mode you're already in.)

Quote from: jamesreg on Wed 06/02/2019 02:04:13
why a display statement has to be made after hitting the z hotkey to toggle the battle and cursor modes on and off.  That bug has been there in my old code before you guys helped and even now after we have tidy it up and tightened it up.
I don't know whats causing it. Id rather just hit z and toggle modes without any pop up.

So you're saying that if you take out the Display() calls, it no longer works? The reason is probably that when you press Z, you hold down the key for more than one game loop, so the mode flips off and then immediately back on, off, on, off... so it's almost random what state it ends up in.

One way to fix that is to do what Cassiebsg suggested and add a Wait(1) call instead of Display(), but you could still end up flipping it multiple times. The easiest fix is to use a flag to check that you only do it once for each keypress:

Code: ags
bool modeWasFlipped;

function room_RepExec()
{
  // Testing script to exit game 
  if (IsKeyPressed(eKeyEscape)) {
    QuitGame(0);
  }
  //End of Testing Script  

  if(IsKeyPressed(eKeyZ))
  {
    // We only switch mode if we haven't already done so during this keypress
    if(modeWasFlipped == false) // another way to write this is just: if(!modeWasFlipped)
    {
      if(Bridge_Battle == true)  // or just: if(Bridge_Battle)
        Cursor_Mode();
      else
        Nav_Mode();

      modeWasFlipped = true; // We've switched mode, so set the flag
    }
  } // End: IsKeyPressed(eKeyZ)
  else // the key is released, so we reset the flag
    modeWasFlipped = false;

  if(Bridge_Battle == true) // Same: if(Bridge_Battle)
    updateBattle();
}


Notice also how I'm using indentation to show which different lines belong inside which if-conditions. Try to be consistent about your indentation; it makes code much easier to read. (It's sort of like punctuation for English sentences: do it wrong and you'll probably still be understood, but it's just much less readable and more confusing.)

jamesreg

Thank you so much for this is working exactly the way I wanted.

I know that many times it is hard helping novice coders and even doing some of or in my case much of the coding yourself and think they are just getting freebies. The things that are learned and lessons are of great assistance.

More then just the specific things in this code I picked up shorter ways to do code, more cleaner and organized ways to do it and a few commands and functions I did not think I would be able to learn and tearing though this code and examining it will help me to improve and understand further.

I greatly thank you for the assistance and your time has not been wasted on me.

Snarky

Great, I'm glad it's been of use, and look forward to the game. Hope you manage to sort out the rest of it, too.

jamesreg

#31
Ignore this post made a mistake

jamesreg

#32
Code: ags
 BEnemy1.X = cEnemy1.x/50  + (BEnemy1.Width) / 5;
  BEnemy1.Y = cEnemy1.y/50  + (BEnemy1.Width) / 5;
  
  bGreenDot.X = viewportX/50 + (bGreenDot.Width) /5;
  bGreenDot.Y = viewportY/50 - (bGreenDot.Width) /5;


Got it to working im still about one radar blip over from the enemy when I go looking for him but other then that it works perfect any ideas why.




Snarky

Quote from: jamesreg on Mon 11/02/2019 15:42:07
Could the scripting we did for this room with the mouse always being called to the center be effecting trying to get a radar system working?

this is code I tried having it go by mouse
Code: ags
 bGreenDot.X = mouse.x/75 + (bGreenDot.Width) / 5;
  bGreenDot.Y =  mouse.y/80 + (bGreenDot.Width) / 5;


Sort of, but the actual problem here is that you're not taking account of the viewport.

In AGS, X and Y coordinates come in two flavors. Some things are in room coordinates, while other things are in screen coordinates. Room coordinates are for things that exist "in the room", logically enough, like characters, objects, hotspots/regions etc., while screen coordinates are for things that shouldn't move just because you're scrolling around in the room, so stuff like GUIs and your mouse cursor (you normally don't want your cursor to disappear off-screen just because the room scrolls away from where it was).

To convert between them, you have to consider the Viewport location. This allows you to convert back and forth:

Code: ags
  roomX = screenX + GetViewportX();
  screenY = roomY - GetViewportY();


QuoteI even tried other ways to make it work like turning cEgo into an invisible character and have him follow the mouse and do if character is over hotspot but hes not walking just following mouse so I tried if mouse is over hotspot and it seems to not recognize mouse over hotspot processes eighter.

Yeah, stop doing that. I'm not sure why you thought that would help. In fact, why are you using hotspots at all?

jamesreg

#34
I was using hot spots because we are not all master coders so I figured if I made hotspots I could mouse over them and then tell the button to move that way. 90 percent of what I want to do later can be done in the panels or basic interactions but this part of game you have to know how to code to do it.


Original code now I need the two bGreendot lines in the format you gave me.

Code: ags
BEnemy1.X = cEnemy1.x/75  + (BEnemy1.Width) / 5;
  BEnemy1.Y = cEnemy1.y/80  + (BEnemy1.Width) / 5;
  
  bGreenDot.X = mouse.x/75 + (bGreenDot.Width) / 5;
  bGreenDot.Y =  mouse.y/80 + (bGreenDot.Width) / 5;


ok so you told me
Code: ags
roomX = screenX + GetViewportX();
  screenY = roomY - GetViewportY();



Now ill try to see if I can figure out how to write that out.
I understand what the formula means and what it is meant to do.
I just have to play with it till I get it right now.


EDIT
I am till having trouble understanding how I need to code the player radar part. I understand what your code says and is supposed to do but I am still confused on the whole room vs view port coordinates things but making progress the scrolling i done, almost have weapons working and almost have radar if I can get to that point I can combine a lot of other things I already done and be on my own for awhile Im just still confused how to write this out and want to understand how to better understand the room vs viewport thing.

jamesreg

#35


I got this moving some now but I think it is my numbers which are off or maybe im still doing it totally wrong

Code: ags
 bGreenDot.X = viewportX/75 + (bGreenDot.Width) /5;
  bGreenDot.X = viewportX/80 - (bGreenDot.Width) /5;


it moves side to side just fine (except it wraps a couple grids to early on the map)
It does not move the blip up or down at all.

But I am running into my enemy at about the right location good news.

Disregard the above

Code: ags
BEnemy1.X = cEnemy1.x/50  + (BEnemy1.Width) / 5;
  BEnemy1.Y = cEnemy1.y/50  + (BEnemy1.Width) / 5;
  
  bGreenDot.X = viewportX/50 + (bGreenDot.Width) /5;
  bGreenDot.Y = viewportY/50 - (bGreenDot.Width) /5;
 


found my typos and changed the numbers to the size of the gui and it works except for beight roughly half or a full radar blip apart from the enemy when I go looking for him is this due to the fact the enemy is a character and the player is a viewport and I need to compensate for that?
Im slowly starting to understand

SMF spam blocked by CleanTalk