简体   繁体   English

在Block Breaker游戏中(Unity3D)我试图在所有积木都毁掉时加载关卡

[英](Unity3D) in Block Breaker game I'm trying to load levels when all bricks destroyed

Good Morning Developers! 早安开发人员! so here is what i'm trying to do: i created a block breaker game, and i wrote some code so that when all bricks in the scene are destroyed the next level is loaded. 因此,这就是我想要做的事情:我创建了一个破坏块游戏,并编写了一些代码,以便当场景中的所有积木都被销毁时,会加载下一个关卡。 It works fine, but there is a bug! 它工作正常,但是有一个错误! when i lose before destroying all the bricks, and then i press "play again", the static variable who is responsible of counting bricks on scene does not reset to 0! 当我在销毁所有积木之前输了钱,然后按“再次播放”时,负责计算现场积木的静态变量不会重置为0! it keeps the number of brick before i lost and add to it the number of bricks in the new scene!, so instead of returning 24 for ex (which is the correct number of bricks in scene) it returns 35 (11 + 24) how can i fix that please? 它会在我丢失之前保留砖的数量,并在新场景中添加砖的数量!因此,与其返回ex的24(这是场景中正确的砖的数量),不如返回35(11 + 24)我可以解决这个问题吗? here is the code i'm using: first the brick script : 这是我正在使用的代码:首先是Brick脚本:

public int maxHits;
public int timesHit;
public Sprite[] hitSprites;
public static int breakableCount = 0;

private bool isBreakable;
private LevelManager levelManager;
// Use this for initialization
void Start () {
    isBreakable = (this.tag == "Breakable");
    if(isBreakable){
        breakableCount++;
    }
    print (breakableCount);
    timesHit = 0;
    levelManager = GameObject.FindObjectOfType<LevelManager> ();
}
void OnCollisionEnter2D(Collision2D collision) {
    if (isBreakable) {
        HandleHits ();
    }
}

void HandleHits(){
    //TODO remove the print!!
    print ("collison");
    timesHit++;
    if (timesHit >= maxHits) {
        breakableCount--;
        print (breakableCount);
        levelManager.BrickDestroyed ();
        Destroy (gameObject);
    } else {
        LoadSprite ();
    }
}
// Update is called once per frame
void Update () {

}

//TODO Remove this when player can WIN
void NextLevel(){
    levelManager.LoadNextLevel ();
}

void LoadSprite(){
    int spriteIndex = timesHit - 1;
    this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}

and here is the LevelManager script I'm using to manage levels : 这是我用来管理关卡的LevelManager脚本:

public void LoadLevel (string name) {
    Debug.Log ("level change requested for : " + name);
    Application.LoadLevel (name);
}
public void ExitRequest() {
    Debug.Log ("Exit game requested");
    Application.Quit ();
}
public void LoadNextLevel () {
    Application.LoadLevel (Application.loadedLevel + 1);
}
public void BrickDestroyed () {
    if(Brick.breakableCount <= 0) {
        LoadNextLevel ();
    }
}

hope i explained correctly, and sorry if i made some English errors i'm not native speaker lol, Thank you have a nice day ^^ 希望我能正确解释,对不起,如果我犯了一些英语错误,我不是母语使用者,谢谢,祝您有愉快的一天^^

-Edited due to misunderstanding- I didn't realize that was your BRICK script. -由于误解而编辑-我不知道那是您的BRICK脚本。 The reset should be inside our LevelManager. 重置应该在我们的LevelManager中。

Your first line in your function to load a new level in LevelManager should be: 在LevelManager中加载新关卡的函数中的第一行应为:

breakableCount = 0;

This will make it so that when the level is initialized that the counter is reset. 这样可以确保在初始化级别时重置计数器。

Also, you could reset the same way as soon as you've decided that a person has beat the current level. 同样,一旦您确定某人已超过当前水平,就可以以相同的方式重置。


Also, I recognize this from Ben Tristram's Unity Dev Course. 另外,我从Ben Tristram的Unity开发课程中也认识到这一点。 You should try using the tools built into his class for questions, there is a lot of support there for these specific exercises! 您应该尝试使用班级内置的工具来解决问题,这些特定练习为您提供了很多支持!

Stack Overflow is great though, and it's a great source for when that stuff falls through. 堆栈溢出虽然很棒,但是当这些东西落空时,它是一个很好的来源。 Another place to check is https://gamedev.stackexchange.com/ 另一个要检查的地方是https://gamedev.stackexchange.com/

private static int breakableCount = 0;
public static int BreakableCount 
{ 
    get{ return breakableCount; }
    set{
         breakableCount = value;
         if(breakableCount <= 0){ EndOfLevel() }
    }
}

Turning your variable into property (or you can use a method if you prefer), you can now add some logic when it gets modified. 将变量转换为属性(或者如果愿意,可以使用方法),现在可以在修改变量时添加一些逻辑。

EndOfLevel is just a method you call to load the next level, save some data and reset some static values before leaving. EndOfLevel只是您调用的一种方法,用于加载下一个级别,保存一些数据并在离开之前重置一些静态值。

I Want to update this post because i find a solution and i have another question in the same subject! 我想更新此帖子,因为我找到了解决方案,并且在同一主题中还有另一个问题! First i'll tell you how i fixed it: as @JorgeSantos suggested i created a ResetGame fonction in my loadlevel script : 首先,我将告诉您如何修复它:如@JorgeSantos所建议,我在负载级别脚本中创建了一个ResetGame功能:

void ResetGame(){
    Brick.breakableCount = 0;
    print ("breakableCount set to 0 ");
}

then i called that fontion in my LoadLevel fonction : 然后我在我的LoadLevel功能中称该字体为:

public void LoadLevel (string name) {
    ResetGame ();
    Debug.Log ("level change requested for : " + name);
    Application.LoadLevel (name);

now the variable is resetting just fine the only problem (it's not really a problem because the game runs fine, it's just that i want to know why it's happening) is that for ex let's say that i run the game, destroy 4 bricks, and then i lose, (keep in mind that there are 24 bricks in the scene) so i left 20 bricks non destroyed! 现在变量可以很好地重置唯一的问题(这并不是真正的问题,因为游戏可以正常运行,只是我想知道为什么会这样),例如,假设我运行游戏,销毁了4块积木,并且然后我输了(记住场景中有24块砖),所以我留下了20块未毁的砖! when i press play again, in the console, i notice that when i destroy a brick the breakableCount variable is not taking new values, then when i destroy 4 brick, (which means i'm in the same number of bricks left as i were before losing), then the breakableCount variable takes the value 20 (which is the right value) and continue decreasing when i destroy bricks normally!, You can see now the the game continue to work fine, but i dont understand why that variable is not reset to the right number of brick after i click on play again, and only takes effect when i reach the same number of destroyed brick as in my fist try?! 当我再次按播放键时,在控制台中,我注意到当我销毁一块砖块时,breakableCount变量未采用新值,那么当我销毁4块砖块时,(这意味着我所剩砖块的数量与我以前所剩的砖块数量相同)在输掉之前),然后breakableCount变量取值为20(这是正确的值),并在我正常销毁积木时继续减小!,您现在可以看到游戏继续正常运行,但是我不明白为什么该变量不起作用再次单击播放后重置为正确的砖块数量,并且仅在达到与我的拳头尝试相同的销毁砖块数量时才生效吗? hope i made my point clear looking forward for your answers, and thank you all ^^ 希望我能说清楚一点,期待您的回答,并谢谢大家^^

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

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