Code Monkey home page Code Monkey logo

Comments (6)

saricden avatar saricden commented on May 24, 2024

@medecau @tedbigham @benhalverson Any ideas on how we could create some sort of generalised UI class that could handle watching for viewport resizes and populate those changes throughout the whole game real-time?

I'm sure at some point we'll be hooking into document.addEventListener('resize', () => { ... }); to watch for the viewport changes... But I'm just thinking about the best approach for architecture.

I just don't want us to end up in a situation where we're juggling 9999999 different functions for resizing each little component of the game.

One thought I had was maybe we could do something along the lines of:

  1. Resize canvas to viewport
  2. Call the create() function of the current scene again (but somehow prevent duplicate sprites, etc. being added?)

from no-place-like.

medecau avatar medecau commented on May 24, 2024

Confident Phaser already has the necessary infrastructure to handle viewport resizes.

Could it be that we have a Camera like object? I need to dive into the game code and read the docs a little bit.

from no-place-like.

saricden avatar saricden commented on May 24, 2024

from no-place-like.

tedbigham avatar tedbigham commented on May 24, 2024

It seems like there's three things we care about here:

  • Gameplay objects. These need to be scaled but not moved (their world position is not changing).
  • UI objects. This need to be moved to stay in the same relative position on the screen (reactive) and probably scaled
  • The camera. This might need to be zoomed to maintain a constant physical side for the MC, and will probably be specific one-time code.

I'm thinking that instead of setting x, y, and displaySize when you add stuff to the scene, you could add new fields that give relative values for x, y, and displaySize. Then add a method to GameObject (polyfill ?) that calculates x, y, and displaySize based on the current canvas size. You'd call this method immediately when creating objects (mostly in create() ). You'd also loop over all living game objects and call it every time the game is resized.

You might end up with 2 or 3 types of that method depending whether the object is gameplay, or UI, or whatever high level type you have (i usually only have 2).

from no-place-like.

benhalverson avatar benhalverson commented on May 24, 2024

some docs for the scaling games from Phaser 3
https://github.com/samme/phaser3-faq/wiki#how-do-i-scaleresize-the-game-canvas
A demo link is here. https://labs.phaser.io/100.html?src=src\scalemanager\fit%20running%20game.js

from no-place-like.

SparrowOchon avatar SparrowOchon commented on May 24, 2024

Instead of having a listener on resize() which will end up affecting you in the long run if you want to resize objects on the fly might be better to add the event listener on screen.orientation Docs.

As for the resizing @tedbigham solution would work to add on to that you can use CSS to resize the main canvas and build the x and y relative to that.

from no-place-like.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.