Scratch tricks you don't know

foreword

        You may have experience in making a small map mobile engine. Before that, your engine may look like this:

        Traditional minimap movement engine

        There is no flaw in the technique, but the ancients once said: "The green hills outside the mountains, the buildings outside the buildings"

        So I actually have a more perfect engine

 New minimap movement engine

        This engine is able to run normally, if you don’t believe me, you can try it yourself

        As for why the new engine can run and what is the use of expansion, let’s see the breakdown below


Realization principle

        The book continues from the above, in fact, the mystery of this engine is in this operation symbol. As we all know, the return data of the hexagonal code is a Boolean value, which is subdivided into "True" and "False", which are true and false. However, when the Boolean value code is embedded in the operation code, "true" and "false" are transformed into "yes" and "nothing", which are also "1" and "0", as shown in the picture

Before embedding operators

After embedding operators

        All judges can also testify to themselves that I have never lied

        I call this phenomenon "Boolean binarization", which is the basic reason why the engine works

        Next, I sort out the principles of the optimization engine in the preface and give:

  • For example [Increase the Y coordinate by ((if W or ↑ is pressed) times 10)]

        Because adding 0 coordinates will not change, so the role will not be affected if you don’t press the keyboard, and the effect is the same as the traditional method.


Extended usage

        I uphold the spirit of Scratch, so I only use words to teach how to optimize. You can apply to all mobile games as long as you draw inferences from one example!

Optimization:

  • Find the key detection code
  • The small map can be copied accordingly (change the individual places as appropriate)
  • Because the coordinates of other types of maps are controlled by variables, just treat [increase variable (X coordinate) ()] as [increase X coordinate ()]
  • Note: The large map engine may be a bit complicated. If you don’t know how to change it, you can read more about it. It’s better to teach people to fish than to teach them to fish. Once you understand the principles, you can optimize everything!

conclusion

        Although the amount of code has not changed, the structure of this new engine provides convenience for subsequent iterations. I think the benefits of optimization are as follows:

  • This set of code does not have a very complicated structure. The traditional structure [if < > then] is very unfriendly to Debug personnel. After optimization, there is only one line of code, which does not look complicated. It is not only easy to Debug, but also very friendly to novices. The entire code structure is also much clearer and clearer.
  • Because the judgment and calculation of the new structure are integrated, it may save computing power

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131999671