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

2017/2/18

How do I count collisions and pass them across actors?

dcx dcx

2017/2/18

#
I am trying to make a game of pong. Ive got the collisions working and all but I can't get the scores to work. I have all the digits necessary and respective classes and I made two specific classes (score1 and score2) that are supposed to change image when the ball has been detected as hitting the limit of the map (left and right - "limite1" and "limite2"). I have figured that one of the problems is that the score1 (and score2 which I haven't done yet but must be the same as score1 with different variable names) isn't being incremented. I have tried so many ways to create these variables in such a way that I could invoke them globally. I indented the scores within the "ponto", which is the one that is responsible for the movement of the ball and checking of limits. Thanks for your time!
Super_Hippo Super_Hippo

2017/2/18

#
First, the score class shouldn't extend the ball class because the score isn't a ball. There are several ways to do it. One would be to save the scores in the world.
private int[] score = {0, 0};
private Actor[] scoreActors = {new Actor() {}, new Actor() {}};

//when adding:
addObject(scoreActors[0], 50, 20);
addObject(scoreActors[1], 250, 20);
updateImage(0);
updateImage(1);



private void updateImage(int player)
{
    scoreActor[player].setImage(new GreenfootImage("Score: " + score[player] , 20, Color.BLACK, new Color(0,0,0,0)));
}

public void addScore(int player, int value)
{
    score[player] += value;
    updateImage(player);
}
Then when you want to increase the score, you have to call the addScore method on the current world object passing the current player (0 or 1) and the value you want it to increase. If this should always be 1, you can change the method to this:
public void addScore(int player)
{
    score[player]++;
    updateImage(player);
}
As an alternative, you could import the Counter class and each player adds his own Counter to the world.
You need to login to post a reply.