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

2016/5/5

Problem with score counter

LittleMiiMe LittleMiiMe

2016/5/5

#
Hey everyone I've a little problem with a score counter I've been working on. The way it works is that I've objects which if they have been shot they will send a true value via a method to their superclass called spawn. There that true value will be put into a another value (A1), A1 if true will add 1 to another value (B1). When that happened A1 will get the value false and reset the method it self.
public void getHitAEnemy(boolean a)
    {
        A1 = a;
        if (A1 == true)
        {
            B1 += 1;
            A1 = false;
            getHitAEnemy(A1);
        }
    }
that's the processing code. When an enemy has been hit by a bullet this code will be run
if (isTouching(Bullet.class) == true)
{
     int i = 0;
     i++;
     if (i == 2)
     {
          C1 = true;
          getHitEnemy(C1);
          this.getWorld().removeObject(this);
     }
}
the i is just for delay. What am i doing wrong? Or is there a better way (that i as a basic programmer can understand) to check if an enemy has been hit and the score needs to be added up? Thanks for helping
valdes valdes

2016/5/5

#
There are some errors in your logic. First with the code to run when the enemy is hit. I suppose that code is inside the act() method of the Enemy. If that is the case, i will never be == 2. Let me try to elaborate on that Imagine that the act method is executed every computer cycle, like a loop When touching the bullet, you create a local variable i, with a 0 value. Next line increases the value to 1. the condition (i == 2) is false, and the method ends. In the next computer cycle, if it is still touching the bullet, a new variable i is created with a value of 0, increases to 1 and i == 2 is still false and always will be false. My suggestion, remove that i and the if (i == 2). If you want some delay you could add a timer or do a loop.
valdes valdes

2016/5/5

#
For the score. In multiplayer games, the score is best kept on each player. In one player games, you could also keep it in the world. I think keeping the score in the enemies won't work, because they die, the objects are destroyed and the value will be lost.
valdes valdes

2016/5/5

#
I offer this simple solution (not the most elegant and object oriented but solves the problem) In your world class declare a variable for the score:
public int score = 0;
In the Enemy class, in the act method
if (isTouching(Bullet.class) == true)
{
          this.getWorldOfType(YourWorldsName.class).score++;
          this.getWorld().removeObject(this);
}
and the score increments. in the act method of your World, you should display the score as an image. For a more complex solution you can check out my BebeComelon scenario, for multiplayer scores using the imported class Counter and different points given by each Enemy.
LittleMiiMe LittleMiiMe

2016/5/5

#
Oh i see thank you for replying, I''l test this immediately. Should have read the code better, but in school we are learning green foot and we use the official book but since we're nearing the end of the year we are literally flying through the book. But thanks for helping.
valdes valdes

2016/5/5

#
Ok I understand, let me try to give you a crash-course with the Counter class, so you finish your project fast. In Greenfoot, go to the Edit Menu, Import Class, select Counter and click on Import. You will now have a new actor called Counter. in your world. Declare a counter
public Counter counter = new Counter("Score: ");
in your world Constructor, add the counter to the world
addObject(counter, 70, 20);
70,20 are the x-y coordinates, change the values of course. in your Enemy class, inside the act method
if (isTouching(Bullet.class) == true)
{
          this.getWorldOfType(YourWorldsName.class).counter.add(1);
          this.getWorld().removeObject(this);
}
where 1 is the quantity of points to be added to the score. Of course you can change it. And that's it, score implemented. If you need to get the score, perhaps to display it in an end title, use
counter.getValue();
which returns an int.
SPower SPower

2016/5/5

#
I'd like to add something about how to delay a piece of code. In your original code, I think your intention was to let an integer count up to 2. For these kinds of purposes, letting an integer count up to two happens almost instantaneously. If you want to delay something by using an integer, using a for-loop (or, alternatively, a while-loop) is the way to go:
for (int i = 0; i < 100000; i++) {
    i++;
}
It'll be a matter of trial and error to see which maximum value (in the example I used 100,000) will give the desired result, but know that you'll have to use large numbers for it. Adding integers together is lightning fast, even in Java :) If you want to go a little more complex, there is a way to pause the scenario for a specified amount of milliseconds. This is how that's done:
Thread.currentThread().sleep(1000); // 1000 milliseconds, or 1 second
The specifics behind this are a bit complex, and not necessarily needed in this case, but know that this will pause the entire program for, in this case, 1 second.
danpost danpost

2016/5/5

#
The 'getHitAEnemy' method can be reduced to this:
public void getHitAEnemy(boolean a)
{
    if (a == true) B1 += 1;
    A1 = false;
}
To explain the reduction, the last two lines ends up calling the current method with a false value, which only sets the value of A1 to false. So the only other change within the method is incrementing B1 if the boolean parameter was true. It is difficult to understand what you are wanting the method to do (or even what conditions are required before calling the method from outside the method). This is due to the non-descriptive names of your fields. I actually do not understand the need for the method at all since you should be able to increment the B1 field directly in the subclass. For the delay in removing the enemy, you can use an int field (like 'i', but it should be declared outside of the method so that its value will be retained for multiple act cycles). As long as its value is zero, the actor can remain in the world. Once its value is set to some positive value, it should be decremented; and when decremented back to zero, then remove the actor.
// instance field
private int removeIn = 0;

// last lines in act method
if (removeIn > 0)
{
    removeIn--;
    if (removeIn == 0) getWorld().removeObject(this);
}
// else (removeIn == 0) do nothing
Then all that is needed is to set the value of 'removeIn' to some positive value when it is determined that the actor is to be removed (like when hit by a bullet).
You need to login to post a reply.