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

2011/10/7

Memory leak?

Duta Duta

2011/10/7

#
In this code right here, can't work out why it's so bad :/ Basically when I have this implemented in the act method so that it works when it runs, the entire program basically doesn't work upon hitting run - which I take to be a memory leak:
public void moveEnemyMethod()
    {
        int i = 0;
        while(i < 6) {
            if(i == 0 || i == 1) {
                moveEnemyRight();
                i++;
            }
            if(i == 2) {
                moveEnemyDown();
                i++;
            }
            if(i == 3 || i == 4) {
                moveEnemyLeft();
                i++;
            }
            if(i == 5) {
                moveEnemyDown();
                i = i - 5;
            }
        }
    }
poink poink

2011/10/7

#
Lol, what are you trying to demonstrate with rapping i, while and if in this overcoded method?
nccb nccb

2011/10/7

#
Let's step through your code to realise what's happening:
    public void moveEnemyMethod()
    {
        int i = 0;
So here, before the loop, i is equal to zero. Let's go into the loop for the first time:
        while(i < 6) {
            if(i == 0 || i == 1) {
                moveEnemyRight();
                i++;
            }
When we get here, i is equal to 0 so we execute this block, and then i is equal to 1. We don't do anything more in this loop, so i is 1 and we go back round:
        while(i < 6) {
            if(i == 0 || i == 1) {
                moveEnemyRight();
                i++;
            }
We do that bit again, and i is now 2. That means we go straight into the next bit:
            if(i == 2) {
                moveEnemyDown();
                i++;
            }
Now i is 3, so we're again straight into the next bit:
            if(i == 3 || i == 4) {
                moveEnemyLeft();
                i++;
            }
After this, i is 4, so we go round the loop (4 is less than 6) and end up executing that block again:
            if(i == 3 || i == 4) {
                moveEnemyLeft();
                i++;
            }
Now i is 5, so we go straight into the next bit:
            if(i == 5) {
                moveEnemyDown();
                i = i - 5;
            }
After this, i is 0. We go back round and execute the while loop with i being 0. Which is exactly how I began this post, so feel free to read my post again up to here to see what happens next ;-) That's why Greenfoot appears to freeze. I suspect the solution is that you want:
if (i < 6)
rather than while, so that these movements happen only once on each act() (assuming moveEnemyMethod is called once each act). But it's hard to say exactly what the fix should be without knowing what you intended. BTW: glancing at your profile, I see that you go to the same sixth form college that I did (many years ago, now...) Small world, and all that.
Duta Duta

2011/10/7

#
I replaced the while with if, but now they just keep going right. Is this because the statement
int i = 0;
is executed each act, meaning it only does whatever method is wanted when i == 0? Also, that's crazy about the college! :P How many years ago did you go? I'm going to assume you did A Levels (I'm doing the IB, but I think the college hasn't been running it wrong - I might be wrong though)
Duta Duta

2011/10/7

#
DW. This problem is resolved. Thanks :)
Duta Duta

2011/10/7

#
http://www.greenfoot.org/scenarios/3584 That's what it was for.
nccb nccb

2011/10/7

#
Sorry -- yes, I meant to say that you'd also need to move i to be a field (a member variable of the class rather than a local variable). I should have realised it was space invaders movement! (BTW, you might want to make the ship's firing rate a bit lower, to stop you being able to take out a whole column of enemies in one go.) I was at the college just over 10 years ago. I did A-Levels as you guessed (back then, it was either A-Levels or GNVQ, no IB), including Computing. But I must admit it was pretty boring back then -- no Greenfoot, just lots of VB and databases. I programmed games in my spare time, which was much more interesting :-)
Duta Duta

2011/10/7

#
Yeah the A Level Computing students have to VB still... Happy I'm on the IB: Greenfoot and (which we'll be moving onto after this half term) BlueJ
poink poink

2011/10/7

#
LOL it's not about VB, VCS, Java or any other programming language, it's about the programmer. Tha code above is the demonstration of a poor programmer.
nccb nccb

2011/10/7

#
poink: every programmer is capable of writing bad code, and we have all written bad code when we're learning to program. The only thing to do is to teach each other to write better code :-)
Duta Duta

2011/10/8

#
@poink I started coding a month ago, cut me some slack. @nccb Thanks :) yeah that's why I use this discussion board - its nice having people who'll help you out, and - when I can - I like to help out too, so I give something back. EDIT: In case you were interested, the code now reads:
private int i = 0;

//etc

public void moveEnemyMethod()
{
    if(i < 6) {
        if(i == 0 || i == 1) {
            moveEnemyRight();
        }
        if(i == 2) {
            moveEnemyDown();
        }
        if(i == 3 || i == 4) {
            moveEnemyLeft();
        }
        if(i == 5) {
            moveEnemyDown();
        }
        if(i < 5) {
            i++;
        }
        if(i > 4) {
            i = 0;
        }
    }
}
danpost danpost

2011/10/9

#
It might make more sense using a 'switch' statement instead of all the 'if's, as follows:
switch (i) {
    case 0:
    case 1:
        moveEnemyRight();
        break;
    case 2:
    case 5:
        moveEnemyDown();
        break;
    case 3:
    case 4:
        moveEnemyLeft();
        break;
}
i = (i + 1) % 6;  // This statement will ensure i gets incremented each act up to 5, then resets it to 0 again
The '%' symbol means to get the remainder of what comes before it, divided by what comes after it. It is like saying i++; if (i == 6) { i = 0; }
You need to login to post a reply.