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

2016/4/6

How to access an int value from another actor?

madeirense madeirense

2016/4/6

#
So basically I want to know how to access the current "Score" that is present in a variable in another Actor (called Score) MyWorld:
private void addPowerUP()
    {
        if(Score.class.score == 1000 ){
            int x = Greenfoot.getRandomNumber(1366);
            int y = Greenfoot.getRandomNumber(768);
            addObject(new PowerUP(), x, y);
        }
    }
Super_Hippo Super_Hippo

2016/4/6

#
Is there only one 'Score' object in the world? Is 'score' public or private? You could also call the method from the 'Score' class when the score reached 1000.
//after score changed
if (score == 1000) ((MyWorld) getWorld()).addPowerUP();
or just (without the extra method)
if (score == 1000) getWorld().addObject(new PowerUP(), Greenfoot.getRandomNumber(1366), Greenfoot.getRandomNumber(768));
Make sure the score changes every act-cycle or you will need to make sure that you only add the power up once and not several times until the score changed.
madeirense madeirense

2016/4/6

#
Hello Hippo, thanks for your reply, first of all where do I put the code that you sent me? I've tried to put it on my Score Class but it didn't work. Secondly, hwo do I solve the following?
score = (score + amount) ;

// I need to change it to

score = ((score + amount) * bonus) ;
But once again, I need to access the bonus value from another actor (Called BONUS).
Super_Hippo Super_Hippo

2016/4/6

#
Yeah, it should be in the score class. Did you put it into a method? What exactly you mean with "didn't work"? Why is the bonus value in a class called BONUS? What is this class doing? Do you really want to multiply score+amount with bonus or only 'amount'? Because if you do it like this, it will increase even if amount is 0. If there is only one Bonus object in the world and 'bonus' is public (you didn't answer this question again), it will be this to get the variable.
(Bonus)(getWorld().getObjects(Bonus.class).get(0)).bonus
madeirense madeirense

2016/4/7

#
Sorry Hippo, I can't get it to work :( Maybe it would be easier if you looked into my source code here: http://www.greenfoot.org/scenarios/16376 So basically what I still need to in my Project is, - Adding the PowerUP, when the score reaches a certain amount, 1000 for instance, - Making the Ship "invincible" for a few seconds to prevent destroying the ship if an Enemy Object spawns on the same location as the Ship, - The Bonus isn't working yet. Basically what the Bonus do is multiply the Score by a fixed amout, 1x, 2x, 3x,... If you guys could spare some time to help me out with this I would really apreciate, because I'm stuck and I can't progress at all because of that :P
madeirense madeirense

2016/4/7

#
Super_Hippo Super_Hippo

2016/4/7

#
Ok, the reason why no power up is spawned is that the score isn't always increased by 1. So if the score if higher than 1000, 'score==1000' isn't true. You can change the line to this:
if (score > 999)
But with this, there is a new power up created every time the score is increased after reaching 1000. So you could add a boolean like this:
//outside methods
private boolean addedPowerUp = false

//...
if (!addedPowerUp && score > 999)
{
    //add power up
    addedPowerUp = true;
}
If you want to to add a new power up every 1000 points for example, you could use an int:
private int addedPowerUp = 0;

//...
if (addedPowerUp * 1000 + 999 < score)
{
    // add power up
    addedPowerUp++;
}
Instead of making the ship invincible, you could make it impossible for asteroids to spawn in a certain range (here in example 100 cells) around the ship.
protected void addedToWorld(World w)
{
    while (getObjectsInRange(100, Ship.class) != null)
    {
        setLocation(Greenfoot.getRandomNumber(w.getWidth()), Greenfoot.getRandomNumber(w.getHeight()));
    }
}
I don't understand yet how the bonus is supposed to work. When do you want to get a bonus? How should the bonus affect the score?
You need to login to post a reply.