繁体   English   中英

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

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

所以我正在使用 c# 统一创建一个 2D 游戏。 现在,当我玩游戏时,我捡到一个宝石,钱从 0 变为 1,如果我捡到另一个宝石,它又从 0 变为 1,我不知道为什么它一直回到 0。我要做什么改变因此,当我捡起宝石时,钱确实会增加,并确保它可以与保存系统一起使用。 我有 4 个脚本:GemPickup、Player、Playerdata 和 SaveSystem。

请帮忙,因为我必须在 11 月底之前完成游戏,所以我需要尽快解决这个问题。

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;
        }        
    }
}

播放器脚本:

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 脚本:

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;
    }
}

保存系统脚本:

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;
        }
    }
}

在播放器脚本中尝试此操作,而不是将脚本附加到 gem 游戏对象。 只需将 Gem 标签添加到 gem。

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

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



    }

}

不是让你悬而未决,而且不是说我不使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM