简体   繁体   English

我怎样才能让钱增加而不是重复?

[英]How can i make it so money goes up instead of repeating?

So i'm creating a 2D game in unity using c#.所以我正在使用 c# 统一创建一个 2D 游戏。 Right now when i play the game and i pickup a gem money goes from 0 to 1 and if i pickup another gem it does 0 to 1 again, i'm not sure why it keeps going back to 0. What do i change to make it so when i pickup gems the money does increase and make sure it works with the savesystem.现在,当我玩游戏时,我捡到一个宝石,钱从 0 变为 1,如果我捡到另一个宝石,它又从 0 变为 1,我不知道为什么它一直回到 0。我要做什么改变因此,当我捡起宝石时,钱确实会增加,并确保它可以与保存系统一起使用。 I have 4 scripts: GemPickup, Player, Playerdata and SaveSystem.我有 4 个脚本:GemPickup、Player、Playerdata 和 SaveSystem。

Please help as I need to sort this out soon as i have to finish the game by end of November.请帮忙,因为我必须在 11 月底之前完成游戏,所以我需要尽快解决这个问题。

GemPickup Script: GemPickup 脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gempickup : Player
{
    public void OnTriggerEnter2D(Collider2D Collision)
    {
        if (Collision.gameObject.tag.Equals("Player"))
        {            
            Destroy(gameObject);
            money = money + 1;
        }        
    }
}

Player Script:播放器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public int level;
    public int health;
    public int money;

    public void Update()
    {
        if (Input.GetKeyDown("k"))
        {
            SaveSystem.SavePlayer(this);
        }

        if (Input.GetKeyDown("l"))
        {
            PlayerData data = SaveSystem.Loadplayer();

            level = data.level;
            health = data.health;
            money = data.money;

            Vector3 position;
            position.x = data.position[0];
            position.y = data.position[1];
            position.z = data.position[2];
            transform.position = position;
        }
    }
}

PlayerData script: PlayerData 脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class PlayerData
{
    public int level;
    public int health;
    public int money;
    public float[] position;

    public PlayerData(Player player)
    {
        level = player.level;
        health = player.health;
        money = player.money;

        position = new float[3];
        position[0] = player.transform.position.x;
        position[1] = player.transform.position.y;
        position[2] = player.transform.position.z;
    }
}

SaveSystem script:保存系统脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void SavePlayer (Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/player.fun";
        FileStream stream = new FileStream(path, FileMode.Create);

        PlayerData data = new PlayerData(player);

        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static PlayerData Loadplayer()
    {
        string path = Application.persistentDataPath + "/player.fun";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            PlayerData data = formatter.Deserialize(stream) as PlayerData;
            stream.Close();

            return data;            
        }
        else
        {
            Debug.LogError("Save file not found in" + path);
            return null;
        }
    }
}

try this in player script instead of attach script to gem gameobject.在播放器脚本中尝试此操作,而不是将脚本附加到 gem 游戏对象。 just add Gem tag to gem.只需将 Gem 标签添加到 gem。

public void OnTriggerEnter2D(Collider2D Collision)
{
    if (Collision.gameObject.tag.Equals("Gem"))
    {

        Destroy(Collision.gameObject);
        money = money + 1;



    }

}

Not to leave you hanging, and on a side-not that I don't use Unity, I jst want to show you the following code.不是让你悬而未决,而且不是说我不使用 Unity,我只是想向你展示以下代码。 This is how I would design it without unity.这就是我在没有统一的情况下设计它的方式。

public class Game
{

    public class GameObject
    {
        internal float[] position;

    }

    static public void Destroy(GameObject go)
    {
        // some object destruction
    }

    public class Gem : GameObject
    {
        // inherits position
    }

    public class Character : GameObject
    {
        // inherits position
        internal int health;
    }

    public class Enemy : Character
    {
        // inherits position and health
        public void OnCollision(GameObject other)
        {
            if (other is Player)
            {
                health--;
                if (health==0)
                {
                    Destroy(this);
                }
            }
        }
    }

    [System.Serializable]
    public class Player : Character
    {
        // inherits position and health
        // don't expose your internals
        // and probably should be properties.. but I don't know if they work with the serializer
        internal int level;
        internal int money;

        public void OnCollision(GameObject other)
        {
            if (other is Gem)
            {
                Destroy(other);
                money++;
            }
            else if (other is Enemy)
            {
                health--;
                if (health == 0)
                {
                    //game over;
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#有人知道如何取整,这样我可以获得金钱价值吗? - C# Does anyone know how to round up so i could get a money value? 我怎样才能做到这一点,以便我可以在编辑器中添加特殊攻击,而不是重复添加 for 循环? - How can I make it so that I can add special attacks in the editor instead of repeatedly adding for loops? 我怎样才能做到,而不是重新绘制该对象,而是在多个点多次绘制它? - How can i make it so that instead of repainting this object it just paints it multiple times at multiple points? 如果我单击按钮,计时器将如何递增而不递减? - How can i make that if i click the button the timer will count up instead down? 如何使代码在 FOR 循环中运行? 而不是那么多IF - How do I make the code run in a FOR loop? Instead of so many IF 如何使用当用户进入文本框时,文本消失? C# - How do I make it so that when the user goes into a textbox, the text disappears? C# 我怎么做才能让 catch 停止循环并回到循环的开头 - How do I make it so catch stops looping and goes back to the beginning of the loop 如何使用 $ 在 DataGrid 中格式化货币? - How Can I Format Money in a DataGrid with $? 如何制作一个可预滚动的标签控件,以便我可以添加大量文本框而无需占用整个屏幕的窗体 - how to make a prescrollable tab control so i can add in tons of text boxes without having the form not take up the entire screen 如何将数据模板绑定到可观察的集合,以便它们全部显示? - How can I bind a datatemplate to a observablecollection so that they all show up?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM