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

2015/3/24

Colliding

1
2
3
4
danpost danpost

2015/3/30

#
Some issues: * first codeset (1) Line 1 calls the 'getValue' method; please show the method in its entirety; (2) Line 3 declares an 'addedToWorld2' method; if this method is to be called automatically when the actor is added to the world, then it MUST be named 'addedToWorld' (without the '2'); (3) Line 6 and lines 8 through 15 deal with an unused/unsaved 'image3' image; it will not exist once the method is completed; * second codeset (1) Line 1 refers to 'ap3', show how you define and set its value; (2) Line 3 -- 'final' ??? nvm
aliozkan aliozkan

2015/3/30

#
1.this is my randomValue
public int randomValue = 1+Greenfoot.getRandomNumber(30);
And this is the method to refer it.
public int getValue()
{
    return randomValue;
}
2. I used addedToWorld in this method. This method makes the randomValue appear and stay above the airplane.
protected void addedToWorld(World world)
{
    GreenfootImage image = new GreenfootImage(""+randomValue, 13, null, null);
    randomValueDisplay.setImage(image);
    world.addObject(randomValueDisplay, getX(), getY());
    updateRandomValueDisplayLocation();
}
that's why when I am making the scoreboard I used addedToWorld2. 3.
danpost wrote...
(3) Line 6 and lines 8 through 15 deal with an unused/unsaved 'image3' image; it will not exist once the method is completed;
What do you mean by this? The image that I made in the addedToWorld method works perfectly. Why image3 won't work? 4.
Airplane3 ap3 = (Airplane3) getOneIntersectingObject(Airplane3.class);
5. I learned it from Super_Hippo his second post in the first page.
Super_Hippo wrote...
Since randomValue (which I probably would name differently, maybe 'size' if it is meant to be a "large fish eat small fish"-game, but that doesn't matter) is a private field, you have to create a getter-method in the class of the Enemy (in the following this class is called 'Enemy', I don't know how you called it).
public int getRandomValue()
{
    return randomValue;
}
Then you can use something like this in the class of the user-controlled object:
Enemy e = (Enemy) getOneIntersectingObject(Enemy.class);
if (e != null)
{
    final int v = e.getRandomValue();
    if (randomValue > v)
    {
        randomValue += (v+1)/2;
        e.remove();
    }
    else /*if (randomValue < v)*/ //I don't know what should happen if both have the same size
    {
        remove(); //or not
        //end the game - e.g. 'Greenfoot.stop();'
    }
}
aliozkan aliozkan

2015/3/30

#
I am not getting an error message, the problem is the scoreboard is not appearing. I have to add something to the end of this method but I don't know what.
if (ap3 != null)
    {
        final int a = ap3.getRandomValue();
                    getWorld().addObject(new Explosion(), getX(), getY());
        if (randomValue >= a)
        {
            randomValue += a / 2;
            ap3.remove();
            GreenfootImage image = new GreenfootImage(""+randomValue, 13, null, null);
            randomValueDisplay.setImage(image);
aliozkan aliozkan

2015/3/30

#
I tried to add this as 11th and 12th line to the upper code, but the scoreboard still didn't show up.
GreenfootImage image2 = new GreenfootImage("Score: "+randomValue2, 35, null, null);
GreenfootImage image3 = new GreenfootImage(WIDTH, HEIGHT);
aliozkan aliozkan

2015/3/30

#
Ok now I cut and paste all the stuff in the addedToWorld2 to addedToWorld and the score board appears however, it appears all the time not when the game ends.
Super_Hippo Super_Hippo

2015/3/30

#
Is the Airplane the user-controlled plane? Is there always one Airplane object in the world? Then you can use this to get a reference to the airplane object.
airplane = (Airplane) getWorld().getObjects(Airplane.class).get(0);
If not, you could have a reference to it in you world class.
private Airplane airplane= null;
//when adding
ap = new Airplane();
addObject(ap, x, y);

public Airplane getAirplane()
{
    return airplane;
}

//then you can use this
airplane = ((WorldName)getWorld()).getAirplane();
In line 3 you have a 'addedToWorld2' method. Is the '2' there on purpose?
danpost danpost

2015/3/30

#
(1) Okay -- now, I wonder what the is difference between 'randomValue' and 'randomValue2'; (2) If the scoreboard is to appear at the end of the game, then that code should not be in an 'addedToWorld' method at all; it should be in a 'gameOver' method -- maybe; (3) do not get me wrong; the 'image3' field is declared locally (within the method) and assigned an image which is programmatically edited; however, nowhere is it being saved in a field for later use (by the method it is in or by any method called within that method); therefore, there will be no references to that image once the method ends, which in turn will flag it for garbage collection; It looks like you just need an 'else' code-block for the 'if (randomValue >= a)' clause which should probably be where the scoreboard code should go.
Super_Hippo Super_Hippo

2015/3/30

#
Ah sorry, I didn't see that there is already a third page... ignore my last post.
aliozkan aliozkan

2015/3/31

#
Okay I figured out to print 'Game Over Score: randomValue ' to the screen. Thank you for your help. After scoreboard, I wanted add a timer. I did it by using basic actor but this time it goes so fast how can make it increase slower?
danpost danpost

2015/3/31

#
What is the timer being used for? How fast and how long to you want the timer to run for? Is this timer supposed to be displayed or is it supposed to be behind the scenes?
aliozkan aliozkan

2015/4/1

#
I want the time to tick every second. And i want it to start when the player clicks to 'run'. I made it to appear and increase but it increases too fast. I want to stop the timer when all the enemy airplanes are destroyed
danpost danpost

2015/4/1

#
You probably just need to use two int fields:
Actor tenthsDisplay = new BasicActor();
private int tenths;
private int acts;

// in constructor or method it calls
tenthsDisplay.setImage(new GreenfootImage("Time:  0.0", 24, null, null));
addObject(tenthsDisplay, 120, 20);

// in act or method it calls
acts = (acts+1)%6;
if (acts == 0)
{
    tenths++;
    String text = "Time:  "+(tenths/10)+"."+(tenths%10);
    tenthsDisplay.setImage(new GreenfootImage(text, 24, null, null));
}
aliozkan aliozkan

2015/4/2

#
Thank you but before I read your suggestion I did it by a different way. Here is my way:
private Actor TimerDisplay = new BasicActor();
private int tick = 0;
protected void addedToWorld(World world)
{
GreenfootImage image4 = new GreenfootImage("Time: "+ tick, 20, null, null);
    world.addObject(TimerDisplay, 1450, 830);
    TimerDisplay.setImage(image4);
}
public void act() 
    {
tick++;        
        TimerDisplay.setImage(new GreenfootImage("Time: "+tick / 60, 20, null, null));
}
But the problem is I don't know how to make it stop, could you please help me on that
danpost danpost

2015/4/2

#
A simple way to make it stop is by removing the actor from the world. If the actor is not in the world, then its act method will not execute and run the timer. If that is not an option, then you will need to add an instance boolean to the actor class to control the running state of the timer and place a condition on incrementing the 'tick' field in the act method.
aliozkan aliozkan

2015/4/2

#
Never mind I did it everything works perfect thank you again. If I need help again I will write.
There are more replies on the next page.
1
2
3
4