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

2014/8/10

How to get points(simple) and other stuff

mimzee16 mimzee16

2014/8/10

#
Hi! So i just made a Hobbit type game (very simple). I can control "Dragon" and I try to collect "Treasure". "Hobbit" is trying to kill me ("Dragon"). "Dragon" can shoot "Fireball"s at "Hobbit" to kill him. There are no levels, it is just one world. All I want is to get points for every time "Dragon" kills "Hobbit" with "Fireball" and every time "Dragon" collects "Treasure". I just would like there to be a little score box in the corner with all the points and a "+5" or something to show up when I collect "Treasure". Also, my "Fireball"s never disappear after I shoot them and I want them to disappear after 3 seconds. Again, I literally just started Greenfoot yesterday so I am clueless :)
mimzee16 mimzee16

2014/8/10

#
Do you mind writing out the code because I have no idea how it works?
sengst sengst

2014/8/10

#
Hello. So to have the little box in the corner, you could have something like this: public void addPoints( int points ) { GreenfootImage numberImage = new GreenfootImage( number + " ", 30, null, null ); addObject( new numbers( numberImage ) , int X , int Y ); } if this were to be in an object, add getWorld(). before addObject. you supplement the X and Y coordinates and the number. have an int for the score and pass that in instead of "numberImage" if you want. all that is inside class number is this, plus the other stuff they already have: ( you can delete the act method ) public numbers( GreenfootImage image ) // the constructer of numbers. { setImage( image ); // sets the number image } as for the fireballs, have a for loop for the disapear part. for ( int i = 0; i < 30 ( or however long three seconds is ); i++ ) { // do whatever fire ball does. if ( i == 29 ) getWorld().removeObject( this ); } make sure this is last in the act method. otherwise it will give you and actor not in world exception. hope this helps!
mimzee16 mimzee16

2014/8/10

#
This is so great, but I'm still a little confused! I apologize for my little knowledge of java. How do I get a "numberImage"? I don't know what this means and what I have to insert in place of "number" and the other " ": ( number + " ", 30, null, null ). Also do I replace "new numbers" with anything? Thank you so much! I'm sorry!
sengst sengst

2014/8/11

#
That's okay. We all have to start somewhere. Sorry for my belated answer. : ) Anyway, number is the current score. If you want, you can change it to that. The thirty is the size of the numbers. The fist null will make the text black, and the second will make the background transparent. Don't replace the new numbers, that's what makes the image. Numbers is the class that will show the score. Also leave the ' + " " ' part, otherwise, it will give you a variation of something like this: required: String; found: int... ( and so on ). By the way, I just found this out earlier today, and by starting a discussion like you. So no worries, I'm glad to help.
danpost danpost

2014/8/11

#
Actor objects can be used for characters, controls, ... for anything visual you wish to add to your world. You need to create a subclass of Actor and supply methods and fields to describe the behavior and states of the actors created from that class. Then, you can create an actor from the class and add it into the world. For example, if you wanted to create a class for objects that display basic text that can be added to your world, then a basic Text class could be:
1
2
3
4
5
6
7
8
9
import greenfoot.*;
 
public class Text extends Actor
{
    public Text(String text)
    {
        setImage(new GreenfootImage(text, 24, null, null));
    }
}
You can add a method to the class to allow the text to change:
1
2
3
4
public void setText(String text)
{
    setImage(new GreenfootImage(text, 24, null, null));
}
With this new method, you could change the constructor given in the top code above to this:
1
2
3
4
public Text(String text)
{
    setText(text);
}
Another way to allow the text to change is to add this method instead:
1
2
3
4
public void addedToWorld(World world)
{
    if (isTouching(Text.class)) removeTouching(Text.class);
}
Then, instead of calling 'setText' on the initial actor object created, you can just create a new actor object using 'new Text("...")' and add it at the same location in the world. The old actor object will simple be removed when the new one is added. If it is required to know what text a Text object displays, then you would create a field to hold the String value and set it in the constructor (and in the 'setText' method, if you are using one) and add a method to return that value:
1
2
3
4
5
6
7
8
9
// added instance field
private String text; // declare the field
// in constructor (and possibly in 'setText')
this.text = text; // set the value of the field
// added method to return value
public String getText()
{
    return text; // return the value of the field
}
What I did in the last step is similar to what you would be doing with the int score in the Dragon class:
1
2
3
4
5
6
7
8
// add instance int field
private int score; // declare the field
// added method to change the value of the field by a specified amount
private void adjustScore(int adjustment)
{
    score += adjustment; // update the value of the field
    getWorld().addObject(new Text("Score: "+score), 50, 10); // display the score
}
Fields that contain primary data types are automatically initialized to a default value ('0' or 'false'). You may find a need to add a 'get' method to return the value of the score:
1
2
3
4
public int getScore()
{
    return score; // return the value of the field
}
I tried to do this step-by-step, so as not to confuse you any more than you are already. Hope this helped.
sengst sengst

2014/8/11

#
By the way, you game sounds really interesting! perhaps you would post it when you done? I would love to see it!
sengst sengst

2014/8/11

#
Danpost's answer is a lot clearer than mine. So good job danpost!
danpost danpost

2014/8/11

#
Using a 'for' loop, as suggested at the end of the initial post by sengst would cause everything to halt for those three seconds (more precisely, for the duration of the loop). The act method (Fireball class) should provide the 'loop' and the 'int' variable should be an instance field (so it can be used for multiple act cycles).
1
2
3
4
5
6
7
8
9
10
11
12
// add instance field
private int age; // in act cycles
// in act method
age++;
if (age == 160) getWorld().removeObject(this);
else
{
    // move
    // check collision w/Hobbit
    // -- if true, call 'adjustScore' in Dragon class and remove Hobbit and this from world
    // -- else, check and remove this if at an edge of the world
}
Because the normal running speed of a scenario is between 50 and 60 frames (act cycles) per second, I used 160 as the 'life-span' of the fireball.
mimzee16 mimzee16

2014/8/11

#
Thank you so much to sengst and danpost! You guys are celebrities on this site and I really appreciate how dedicated you are to each problem! I'm still a little confused, but I'll figure it out! Sengst, I will post my game soon after I try to make it the coolest it can possibly be but warning, its not as cool as it sounds :) Thanks again!
sengst sengst

2014/8/11

#
Okay! You can be sure I won't be disappointed! You should really just thank Danpost. He's better at this.
You need to login to post a reply.