“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

[Last Section]

👉 Practice

😜 Game master script

The script is mounted on the background map, and at the beginning and end we need to register and unregister click events. Click the logic inside the event:

  • 1. Initialize the preform
  • 2. Set the location
  • 3. Randomly generate spheres
  • 4. Preview the generation in the upper right corner

See the onClickStart method in the source code for more details

Note:

In order for a script on the UI to respond to a click event, it must mount an object with a UITransform under the Canvas

😜 Update score

The [score] node is mounted with [ScriptScore] script, which is directly updated every second. The score variable saved before can be taken.

But that’s not enough for us. Our display has to be elegant. Yes, elegant.

So we need to use art number, this start our launch page maximum score is the first communication technology.

Find the Font that our [number. FNT] file assigned to [Label] and add it to the code

this.schedule(() = > {
    // Set the score art word
    let LabelScore = this.node.getComponent(Label);
    if(LabelScore ! =null) {
        LabelScore.string = ScriptStatic.CurrentScore + "".toString();
    } else {
        console.log("Get text node empty")}},1);
Copy the code

😜 Determination of death

Triggered when the top line is touched. So the UI has collision detection and needs to respond to event callbacks.

We added RigidBody2D and BoxCollider2D. Note that in order for the system to call the callback, we need to activate EnabledContactListener and register the event in onLoad of the script. The code is as follows:

protected onLoad() {
    let TempCollider = this.node.getComponent(BoxCollider2D);
    if(TempCollider ! =null) {
        console.log("Deathline registered a collision event.")
        TempCollider.on(Contact2DType.BEGIN_CONTACT, this.MethodColliderEnterDeath, this);
    } else {
        console.log("The Collider of Death is empty")}}start() {
    / / [3]
}

/** * Note that the RigiBody2D Type is static and not the default *@param selfCollider
 * @param otherCollider
 * @param contact
 * @constructor
 * @private* /
private MethodColliderEnterDeath(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    console.log("Executed the collision of the death line.")
    // Determine whether to save the score for the maximum value
    let MaxScore = localStorage.getItem(ScriptStatic.MaxScore);
    if(MaxScore ! =null) {
        // Use Number() to convert a string to Number to compare sizes
        if (Number(MaxScore) < ScriptStatic.CurrentScore) {
            localStorage.setItem(ScriptStatic.MaxScore, ScriptStatic.CurrentScore + ""); }}// Display the death screen
    if (this.NodeDeath ! =null) {
        this.NodeDeath.setPosition(0.0.0);
    } else {
        console.log("The death screen is empty.")}// Disable background click response
    if (this.NodeBg ! =null) {
        let TempScript = this.NodeBg.getComponent(ScriptGame);
        if(TempScript ! =null) {
            console.log("Canceled click event");
            // this.NodeBg.off(Node.EventType.TOUCH_START, TempScript.onClickStart, this);
            TempScript.IsDeath=false;
        } else {
            console.log("Obtained script is empty")}}else {
        console.log("The death screen is empty.")}}Copy the code

At this point, our entire development process is over.

The next article xiaokong shares on different platforms [wechat QQ, Bytedouyin, Baidu, etc.] to release the finished products and shelves.

The next komono will take you through the use of advertising systems to enhance gameplay and monetize advertising.

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 welcome to like 👍 collect 🌟 message 📝