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

2016/5/31

Help with accessing functions between actors

Sammi Sammi

2016/5/31

#
Hello~ I'm writing a game sorta like Doodle Jump, except it's Harry Potter eating Horcruxes instead, and I want to make the ScoreBoard show the number of Horcruxes he's eaten. This is part of my code for the Horcrux:
public void destroyHorcrux(){ 
        if(getOneIntersectingObject(HarryPotter.class) != null){
            Actor scoreboard=getOneObjectAtOffset( 350,50,ScoreBoard.class);
            scoreboard.addScore();
            getWorld().removeObject(this);

        }
    }
The code won't compile because it can't find addScore(). But addScore() is placed inside my ScoreBoard, that's for sure. It's probably just a tiny problem in getting the object, but I can't find it. Please help!! Thank you!
Sammi Sammi

2016/5/31

#
Okay, I've got it already. But then another question popped up: How do I make the Scoreboard see the object Horcrux? Because in the game Horcruxes change from the first to the sixth as you go up in level, so my code looks something like this:
public void act() //for my scoreboard
    {
        
        setImage( new GreenfootImage( "Score:" + score, 24, java.awt.Color.GREEN, null));
        Horcrux horcrux= getWorld().getObjects(Horcrux.class);//I know this is wrong, but I don't know how to fix...
        horcrux.change(score);
    }    
    public void addScore(){score++;}
and in the Horcrux it's this:
public void change(int point){
        if(point <=7){setImage("diary.png");}
        else if( point>7&&point<=14){setImage("ring.png");}
        else if(point>14&&point<=21){setImage("locket.png");}
        else if(point>21&&point<=28){setImage("cup.png");}
        else if(point>28&&point<=35){setImage("diadem.png");}
        else if(point>35&&point<=42){setImage("snake.png");}
    }
I know that getObjects() returns a list, but I don't know what else to use. The Horcruxes appear above the bricks(this game is like doodle jump) and go down with them, and multiple ones can appear at the same time. I want to get each one as they appear at the top of the screen. (only one appears at a time) So I don't know how to do it.... Thank you for your help!
danpost danpost

2016/5/31

#
You are getting the right object and I am sure the 'addScore' method is in the ScoreBoard class. However, the compiler will not look in the ScoreBoard class when the variable 'scoreboard' is typed as an Actor object. Change line 3 to the following:
ScoreBoard scoreboard = (ScoreBoard)getOneObjectAtOffset(350, 50, ScoreBoard.class);
This will make the compiler look in the ScoreBoard class for the 'addScore' method on the next line. There could be another issue, however. Will this always be where the ScoreBoard object is located:
Horcrux horcrux = this;
int scoreboardX = horcrux.getX()+350;
int scoreboardY = horcrux.getY()+50;
At all locations where a Horcrux object may intersect the HarryPotter object, the point (scoreboardX, scoreboardY) must be occupied by the scoreboard image. If the intersecting can occur at different places in the world, then this will probably not work. Best would be to keep a reference to the ScoreBoard object in your World subclass with a method to obtain that reference. Something like this:
// called from the Horcrux class with
((MyWorld)getWorld()).getScoreBoard().addScore();

// the method in the World subclass would be
public ScoreBoard getScoreBoard()
{
    return scoreboard;
}

// with the field in the World subclass of
private ScoreBoard scoreboard;
Just make sure that when you create the ScoreBoard object that this 'scoreboard' field gets set to it.
Xevinaly Xevinaly

2016/5/31

#
What you need to do is store the horcruxes to a list and then iterate through them setting each ones changed image.
List<Horcrux> horcruxes = getWorld().getObjects(Horcrux.class);

for(Iterator<Horcrux> i = horcruxes.iterator(); i.hasNext(); ) {
    Horcrux horcrux = i.next();
    horcrux.change(score);
}
However, in terms of game design you should try to set the image when it spawns instead of constantly updating it. This could be seen as such wherever the code is to spawn the horcruxes.
// In the horcrux constructor
public Horcrux(int score)
{
    change(score);
}

//Where the horcruxes are spawned
{
    //spawn details
    getWorld().addObject(new Horcrux(scoreboard.getScore()), x, y);
}
danpost danpost

2016/5/31

#
I just noticed your subsequent post above (the one immediately following your initial post). The scoreboard does not need to see the horcrux. Remove lines 4 and 5 from the 'act' method of the ScoreBoard class (as shown above). Then change the name from 'act' to 'updateImage'. Finally, change the 'addScore' method to this:
public void addScore()
{
    score++;
    updateImage();
}
Also, you should have a constructor for the class (to set the initial image):
public ScoreBoard()
{
    updateImage();
}
Now, let me understand what you are trying to do in the latter part of that post (the 'change' method of the Horcrux class). It appears that you want all active Horcrux objects already in the world to change its image when the score reaches a multiple of 7. But, I fear that is not actually what you want. Please confirm and explain in more detail.
You need to login to post a reply.