This site requires JavaScript, please enable it in your browser!
Greenfoot back
SushiLlama
SushiLlama wrote ...

2019/10/9

Tank Game Turret Rotation and Tank Movement Problem

1
2
SushiLlama SushiLlama

2019/10/10

#
danpost wrote...
SushiLlama wrote...
(deleted) i wonder why i get NullPointerExceptions in line 13 in Turret.class. << Codes Omitted >>
If there is no mouse action, no MouseInfo object is created for that act cycle. Therefore, getMouseInfo may return a null value -- and you cannot call a method (such as getX or getY) on a non-existent object. Make sure the returned value is not null before calling any methods on it.
Done! Thanks!!!
protected Shell[] shells = new Shell[5];
    public Tank() {
        for(int i=0;i<shells.length;i++) {
            //shells[i] = new Shell(this);
        }
    }
public void shoot() {
        if(shells!=null) {
            System.out.println("My Ammo isnt empty"+shells.length);
            for(int i=0;i<shells.length;i++) {
                if(shells[i]!=null) {
                    Shell shootShell = shells[i]; 
                    shootShell.executeShell(); //move the shell bla bla
                    shells[i] = null;
                    break;
                }
            }
        }
    }
Why does the console print out that there is ammo? Shouldnt the array be null? I want to have 5 shots in my tank ammo depot (gets filled up when shells hit a wall for the second time, the first time it will just be reflected). But somehow this just wont work... I want the tank to take a shell out of the first occupied array element. For now i didnt implement the reload on second bounce -> i should have just five shots. Even when i undo commenting out shells = new Shell(this); in the constructor i just cant shoot but dont get any errors. I hope its okay that i didnt open a new thread since i guess this is easy fixable. Impressive how much time you spend helping lost people here! I really admire you for this!
danpost danpost

2019/10/11

#
SushiLlama wrote...
<< Code Omitted >> Why does the console print out that there is ammo? Shouldnt the array be null?
No. the fact that all the elements in the array are null does not make the array itself null. It is an actual Array object full of null elements. Line 8 only asks if the array exists at all. Also, you print out the length of that array, which you fixed at 5 in line 1. The only way to change the length is to assign a new array to the shells field (which is something you do not want to do).
I want to have 5 shots in my tank ammo depot (gets filled up when shells hit a wall for the second time, the first time it will just be reflected). But somehow this just wont work... I want the tank to take a shell out of the first occupied array element. For now i didnt implement the reload on second bounce -> i should have just five shots. Even when i undo commenting out shells = new Shell(this); in the constructor i just cant shoot but dont get any errors.
Go ahead and remove the commenting for that line. That will load the array with the 5 shells required. For ease of use, add an int field to track shell usage. Use its value for which shell to fire and increment it after firing one. If its value is 5, do not allow firing one and do not increment the field. On second wall bounce, reset the value back to zero to start over (as reloading). Using the same 5 shells each time should not create any issue.
SushiLlama SushiLlama

2019/10/11

#
danpost wrote...
SushiLlama wrote...
<< Code Omitted >> Why does the console print out that there is ammo? Shouldnt the array be null?
No. the fact that all the elements in the array are null does not make the array itself null. It is an actual Array object full of null elements. Line 8 only asks if the array exists at all. Also, you print out the length of that array, which you fixed at 5 in line 1. The only way to change the length is to assign a new array to the shells field (which is something you do not want to do).
I want to have 5 shots in my tank ammo depot (gets filled up when shells hit a wall for the second time, the first time it will just be reflected). But somehow this just wont work... I want the tank to take a shell out of the first occupied array element. For now i didnt implement the reload on second bounce -> i should have just five shots. Even when i undo commenting out shells = new Shell(this); in the constructor i just cant shoot but dont get any errors.
Go ahead and remove the commenting for that line. That will load the array with the 5 shells required. For ease of use, add an int field to track shell usage. Use its value for which shell to fire and increment it after firing one. If its value is 5, do not allow firing one and do not increment the field. On second wall bounce, reset the value back to zero to start over (as reloading). Using the same 5 shells each time should not create any issue.
I now did it more like a revolver type. The array gets shifted left to reload. It works fine but the first shot takes two ammo... NEW ROUND 5 Shells left Shell 0: Shell@77a02c Shell 1: Shell@113610a Shell 2: Shell@23aaf1 Shell 3: Shell@142318d Shell 4: Shell@f79f49 4 Shells left Shell 0: Shell@113610a Shell 1: Shell@23aaf1 Shell 2: Shell@142318d Shell 3: null Shell 4: null NEW ROUND 4 Shells left Shell 0: Shell@113610a Shell 1: Shell@23aaf1 Shell 2: Shell@142318d Shell 3: null Shell 4: null 3 Shells left Shell 0: Shell@23aaf1 Shell 1: Shell@142318d Shell 2: null Shell 3: null Shell 4: null NEW ROUND 3 Shells left Shell 0: Shell@23aaf1 Shell 1: Shell@142318d Shell 2: null Shell 3: null Shell 4: null 2 Shells left Shell 0: Shell@142318d Shell 1: null Shell 2: null Shell 3: null Shell 4: null NEW ROUND 2 Shells left Shell 0: Shell@142318d Shell 1: null Shell 2: null Shell 3: null Shell 4: null 1 Shells left Shell 0: null Shell 1: null Shell 2: null Shell 3: null Shell 4: null No Ammo left -------------------- My code looks like this (shoot() is executed when spacebar is pressed):
protected Shell[] shells = new Shell[5];
protected int coolDown = 0;

public void shoot() {
        if(shells[0]!=null && coolDown == 0) { //is there shells left and coolDown = 0?
            //shoot
            getWorld().addObject((Actor)shells[0], getX(), getY());
            shells[0] = null;
            coolDown = 50;
            //reload
            for(int i=0;i<shells.length-1;i++){
                shells[i] = shells[i+1];
                shells[shells.length-1] = null; //necessary?
            }
        } 
    }
danpost danpost

2019/10/11

#
Line 13 would be necessary, else line 5 would be useless. However, it is not something you would do on each iteration of the loop; but, only after the loop completes. Switch lines 13 and 14
SushiLlama SushiLlama

2019/10/11

#
danpost wrote...
Line 13 would be necessary, else line 5 would be useless. However, it is not something you would do on each iteration of the loop; but, only after the loop completes. Switch lines 13 and 14
Works like a charm!!! Wow! Thanks again :D
You need to login to post a reply.
1
2