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

2011/9/27

Crash issue?

1
2
Duta Duta

2011/9/27

#
I have a pong game I am partway through creating (hence some areas that I still need to do), however I just added in a score, and when the ball reaches either end of the screen, instead of increasing one of the players scores and resetting the ball to the centre point, it crashes. Here's the link for the project: http://www.greenfoot.org/scenarios/3551 Open it in greenfoot, hit run and see. When the ball reaches either end of the world it stops running and the Greenfoot Terminal Window pops up with error code. I understand that the reason for this is to do with me trying to increase the counter... at least I think so, but I don't know how to get around this. Also to explain:
public class Counter extends Actor
{
    public int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;
^ I changed the value to public (from private) in an attempted fix.
brian brian

2011/9/27

#
As you tried to help me... I just had a look at your game but without seeing the rest of the code just about anything could be the problem. Does the error message not give a hint?
Duta Duta

2011/9/27

#
Click Open In Greenfoot.
brian brian

2011/9/27

#
That icon wasn't there before ... Anyway it's a fairly simple problem. On line 37 of Ball.java you have the following code.
Counter counter = (Counter) getWorld().getObjectsAt(253, 15, Counter.class);
This is what is causing the problem. The method getObjectsAt returns a list even if that list only contains one object. So when you try and say that the object it is returning is a Counter java is getting upset because it knows it is a List. The solution is to get the Counter out of the list. Assuming you are POSITIVE that it will always find one and only one counter then you could use code like.
Counter counter = (Counter) getWorld().getObjectsAt(253, 15, Counter.class).get(0);
The difference being the get(0) at the end which tells Java to get the first element of the list that getObjectsAt finds. To be honest this is a bit of a hack and you should really do some error checking to make sure that there is only one item in the list but your program does now tell me that I got three points and won the game. Hope that helps...
brian brian

2011/9/27

#
Actually while I'm at it I just noticed the rest of that method... you could definitely slim it down. As a suggestion (and it probably can be slimmed down more) consider this...
public void checkAtLeftOrRight()
    {
        if (atWorldRight()) {
            addPoint ((Counter)getWorld().getObjectsAt(253, 15, Counter.class).get(0));
        }
        else if (atWorldLeft()) {
            addPoint((Counter)getWorld().getObjectsAt(310, 15, Counter.class).get(0));
        }
    }
    
    protected void addPoint (Counter counter)
    {
        if (counter != null)
        {
            counter.add(1);
            if (counter.getValue() == 3)
            {
                ((Background) getWorld()).gameOver();
            }
            else
            {
                setLocation(250, 125);
            }
        }
    }
The only thing I changed that you might want to reconsider is I made it so that it first to three points whereas before it was getting to four. Good luck with the rest of it. Let me know when you finish.
Duta Duta

2011/9/27

#
Thanks :) Only issue I have now is that in line 50, I added .get(0) at the end of that aswell, and it works (in that if the ball touches the left edge it doesn't crash, adds 1 to a counter and resets location... The problem is that it adds 1 to the first counter, which makes no sense to me. I tried changing it to .get(1) on line 50, however that makes no difference. I realise the problem is that I have more than one counter at play, however in line 50 (and line 57 when it adds one to counterTwo) I do not reference to counter (as in the main/first counter) at all... Sorry I'm being dumb. Also yeah it was my bad that the button wasn't there before - forgot to include source, but updated it.
brian brian

2011/9/27

#
I can't recreate that... if I get the ball going off the left hand side (by making WALKING_SPEED a negative number) the right hand counter goes up, if it goes off the right side the left hand counter goes up. That's how it should be and that is with code where all I added was .get(0) to line 37 and 50 - nothing else. I even downloaded the code again to make sure... Did you change anything else while you were trying to fix your first problem?
Duta Duta

2011/9/27

#
When I posted above I hadn't seen your second reply where you slim it down - Will look over it and may well implement it. It does the same as the update I just did, which stops the game when one counter reaches 3.
brian brian

2011/9/27

#
Actually the most obvious answer is something silly but you didn't copy and past the code you got working on line 37 down to line 50 and change where it was looking for the second counter did you? Basically on line 37 it should be looking at 253,15 and on line 50 it should be 310,15 - that would be the first thing I would check!
Duta Duta

2011/9/27

#
Counter counter = (Counter) getWorld().getObjectsAt(253, 15, Counter.class).get(0);
Counter counterTwo = (Counter) getWorld().getObjectsAt(310, 15, Counter.class).get(1);
Duta Duta

2011/9/27

#
Re-download it, and look at my code in Ball.class once more please :)?
brian brian

2011/9/27

#
Okay ... now I feel dumb. I deleted the old code and added your new version and it seems to be working as far as I can see. As before to make the ball go off the left hand side I made WALKING_SPEED in Superclass (not a good name for a class by the way) a negative number. When I do that the right hand counter goes up as expected - no problems that I can see. If I leave WALKING_SPEED as a positive number then it goes off the right and the left hand counter goes up again as expected. What is happening on yours?
Duta Duta

2011/9/27

#
EDIT: What I said here was wrong. What I should have said is: Yes, if I reach the left side the counter goes up and if I reach the right side the counter goes up - the problem is that if I reach the left side I want counterTwo to go up, and if I reach the right side counter to go up.
Duta Duta

2011/9/27

#
Also I know Superclass isn't a good name for it and I know why, I just couldn't think of a better one at the time.
Duta Duta

2011/9/27

#
Ignore above. This is fixed. Thanks brian :D
There are more replies on the next page.
1
2