简体   繁体   中英

(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! 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? here is the code i'm using: first the brick script :

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 :

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. The reset should be inside our LevelManager.

Your first line in your function to load a new level in LevelManager should be:

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. 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/

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.

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 :

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

then i called that fontion in my LoadLevel fonction :

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! 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?! hope i made my point clear looking forward for your answers, and thank you all ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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