search

Home  >  Q&A  >  body text

javascript - How to write the logic and code for the layered rendering of objects and characters in the game?

For example, in the Lone Gunman, the character is sometimes under a tree and sometimes on the grass. They are all objects in the game world. How is the hierarchical logic between the character and the map object handled, and how can the code be optimized?

女神的闺蜜爱上我女神的闺蜜爱上我2739 days ago916

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-06-26 10:54:05

    Games are generally drawn in a loop. My opinion is to create a classification object at the beginning, for example:

    var saves = {
        "background" : [],
        "foreground" :[],
        "characters" : [],
        "ui" : [],
    };

    Just start in the order of for...in.
    For 3D, it may be easier to work with because of the z coordinate. For 2D, since the canvas drawing mechanism is "the picture drawn later covers the picture drawn before", so it needs to be sorted to sort out the hierarchy.

    Looking forward to a better answer downstairs. I am not a game developer, I just tried it out of interest.
    This is one of my demos

    reply
    0
  • 迷茫

    迷茫2017-06-26 10:54:05

    The person above is right, the game is based on frame animation, and in each frame, the level of the object can be reset

    reply
    0
  • Cancelreply