C# 我怎样才能让钱涨起来而不是重复?
所以我用c#在unity中创建了一个2D游戏。现在,当我玩游戏,我捡起一块宝石,钱从0到1,如果我捡起另一块宝石,它又从0到1,我不知道为什么它一直回到0。我应该做些什么改变,这样当我捡起宝石时,钱确实会增加,并确保它与储蓄系统一起工作。我有4个脚本:GemPick、Player、Playerdata和SaveSystem 请帮助我,因为我需要尽快解决这个问题,因为我必须在11月底之前完成比赛 选择脚本:C# 我怎样才能让钱涨起来而不是重复?,c#,unity3d,C#,Unity3d,所以我用c#在unity中创建了一个2D游戏。现在,当我玩游戏,我捡起一块宝石,钱从0到1,如果我捡起另一块宝石,它又从0到1,我不知道为什么它一直回到0。我应该做些什么改变,这样当我捡起宝石时,钱确实会增加,并确保它与储蓄系统一起工作。我有4个脚本:GemPick、Player、Playerdata和SaveSystem 请帮助我,因为我需要尽快解决这个问题,因为我必须在11月底之前完成比赛 选择脚本: using System.Collections; using System.Collec
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;
}
}
}
我不想让您挂起电话,也不是说我不使用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;
}
}
}
}
}
在玩家脚本中尝试此操作,而不是将脚本附加到gem游戏对象。只需将Gem标签添加到Gem
public void OnTriggerEnter2D(Collider2D Collision)
{
if (Collision.gameObject.tag.Equals("Gem"))
{
Destroy(Collision.gameObject);
money = money + 1;
}
}
为什么
player
不属于player
类?如果您从player
派生gemcopper
并创建gemcopper
对象,则gemcopper
对象中的money
对象不会链接到player
对象。。在player
和playerdata
中都有一个money
对象?!你应该重新思考你的设计,思考每个对象和功能应该在哪里。但我担心你缺少C#如何工作的基本要素。。。你应该开始一个简单的教程。。。不是从统一开始你是在增加被摧毁物体的钱,你应该增加玩家的金钱…同意@JHBonarius的设计…如果你在每个gem上创建一个GemPick实例,每个gem都有自己的金钱变量,默认情况下都从0开始…谢谢你,我删除了GemPick脚本并将其放在玩家中。像这样的嵌套类无论是否统一,都不是一个好模式。这并不是说它本质上是坏的,而是说它应该只有在有充分理由的情况下才能做到。当你意识到你没有使用Unity时,你没有检查Unity提供了什么对象(比如GameObject),所以你不必再次指定它们GameObject
也是final
;Unity开发者不应该(或不允许)从它扩展。是的,这是一个设置。有一些想法给了继承的理由,但它们并没有给这个答案增加更多的价值。团结是不同的,但我不会那样做