2

This is what I've tried, but it just doesn't work in p5.js I basically need a line that "renders only the visible portion of the game area on canvas"

Essentially, I need help fixing my function that makes an auto scroller for my game when the player reaches past the co-ordinates 1000x and 300y on the screen...What am I doing wrong?

function ScrollToPlayer(){
  setInterval(function(){
    ScrollTO();
  }, 1);
}
function ScrollTO(){
  if(this.pos.x>1000){
     window.scrollTo(this.pos.x,this.pos.y);
  }
}

https://editor.p5js.org/LttntLark/sketches/JBSg2zIfEf All the code is there. Please read the comments for further explanation on what I need help on.

  • 2
    This code depends heavily on the rest of your game's implementation. Can you add a bit more contextual code as a [mcve]? Are you sure you want to use `window.scrollTo` along with p5.js? Thanks. – ggorlen Oct 22 '20 at 21:50
  • added the code, @ggorlen –  Oct 23 '20 at 14:25
  • 1
    Thanks, but the code needs to be in the question itself, not a link. And it needs to be minimal--the smallest amount of code needed to reproduce the issue you're asking about. – ggorlen Oct 23 '20 at 15:26
  • that snippit of code in the question, WAS all the code required to make the errors there was no variables or anything else outside of it that was required to make the function –  Oct 23 '20 at 15:48
  • 1
    When you run the above code nothing happens--there's no context. It's not clear where the screen is, what the player is, what `this` refers to, what the application looks like or is supposed to do, etc. I think you have an XY problem as stated--`window.scrollTo` doesn't seem relevant to making an autoscroller with p5 to me--the typical approach is to render only the visible portion of the game area on canvas. Anyway, I'm happy to answer MCVE viewport questions as the topic [interests me](https://jsfiddle.net/7yv7u572/108/), but I can't offer anything here because the spec is unclear. – ggorlen Oct 23 '20 at 16:59
  • 1
    Check out [this](https://stackoverflow.com/questions/16919601/html5-canvas-camera-viewport-how-to-actually-do-it) for a canonical thread on making viewports. You can adapt it to p5 without much trouble. – ggorlen Oct 23 '20 at 17:01
  • @ggorlen , "this.pos.x" is referring to the player(mover)'s x position and I need help with making a line that "renders only the visible portion of the game area on canvas" If i were to have to make a minimal reproducible example, i'd need to post my entire code, which depends on itself to work in every part I wrote. SO, this snippit is the only part that I am having trouble making a scroller for my game...If you could help with with making a line that "renders only the visible portion of the game area on canvas" that would be greatly appreciated, as my game relies on it to function! –  Oct 23 '20 at 19:20
  • 1
    Did anything in either of the links I posted above seem like what you're going for in terms of behavior or is it irrelevant? You posted a couple of answers (now deleted) and it isn't clear to me whether the problem was solved or not. – ggorlen Oct 23 '20 at 20:06
  • It was not solved, I basically need a line that "renders only the visible portion of the game area on canvas"...the links were in HTML, i need JS...I started JS 3 months ago, so converting isnt an option @ggorlen –  Oct 24 '20 at 02:09

0 Answers0