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

2017/8/27

Having issues trying to get an actor to keep track of points and how to have one actor react to the Boolean of another actor

Dolbypixar Dolbypixar

2017/8/27

#
So, I want my actor frogboi to know how many points(like from a counter) there are but I've already tried a lot of phrases and it gives me either the error "cannot find symbol - method TotesPoints()" and "non-static method totesPoints() cannot be referenced from static context". I am honestly completely lost as to what I need to do, or how to get my frogboi actor to call the method totesPoints() from the other actor points. Also, I want my Actor snake to pause when stopSnake == true and my actor frogboi is what can make that statement true. How do I get the snake to know that statement is true? Thanks in advance to any form of help or advice.
danpost danpost

2017/8/27

#
It is difficult to help without actually seeing what codes you have. My first thought, however, is that you could use field references in your world class code to the counter and to your frogboi, possibly with getter methods, so the other actors can gain access to them.
Dolbypixar Dolbypixar

2017/8/27

#
so here is the frogboi class, the part that pertains to the issue. Some parts of the code is commented so I could compile and see how the program runs, but the errors they would throw are "could not find symbol - variable stopSnake"
Grass grassWorld  = (Grass) getWorld();
        points Points = grassWorld.getPoints();
        Actor Dave = (Actor)getWorld().getObjects(points.class).get(0);
        int davePoints = points.totesPoints();
        //boolean stopSnake = false;
        
        if (Greenfoot.isKeyDown("w"))
        {
            //move up
            setLocation(getX(), getY() - 2);
        }
        else if (Greenfoot.isKeyDown("s"))
        {
            //move down
            setLocation(getX(), getY() + 2);
        }
        else if (Greenfoot.isKeyDown("a"))
        {
            //move left
            setLocation(getX() - 2, getY());
        }
        else if (Greenfoot.isKeyDown("d"))
        {
            //move right
            setLocation(getX() + 2, getY());
        }
        // else if (Greenfoot.isKeyDown("shift")&& davePoints >= 100)
        // {
            // //frog scream and scares snake
            // //playGreenfoot.sound(frogscream.mp3)
            // Points.bumpPoints(-100);
            // stopSnake = true;
        // }
This is the points class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class points here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class points extends Actor
{
    public int totalPoints = 0;
    GreenfootImage image = new GreenfootImage("0", 20, Color.BLACK, Color.GREEN);
    
    public points()
    {
        setImage(image);
    }
    
    /**
     * The points displayed increase by a certain amount 
     */
    public void bumpPoints(int dave) 
    {
        totalPoints += dave;
        setImage(new GreenfootImage("" + totalPoints, 20, Color.BLACK, Color.GREEN));
    }
    
    public int totesPoints()
    {
        return totalPoints;
    }
}
This is the snake class
Actor Frogboi = (Actor)getWorld().getObjects(frogboi.class).get(0);
        // Add your action code here.
        // if (stopSnake == true)
        // {
            // //stop snake for 1-2 seconds
        // }
        
        //movements of snake
        //follows frog
        turnTowards(Frogboi.getX(), Frogboi.getY());
        move(1);
        
        Actor frog = getOneIntersectingObject(frogboi.class);
        if (frog != null)
        {
            Greenfoot.playSound("fail.mp3");
            getWorld().removeObject(frog);
            Greenfoot.stop();
        }
and this is the World(grass) class
private points thePoints;
    
    /**
     * Constructor for objects of class Grass.
     * 
     */
    public Grass()
    {    
        // creates a world of 800*600 pixels?
        super(800, 600, 1); 
        addObject (new frogboi(), 400, 300);
        addObject (new snake(), Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(575));
        addObject (new anthill(), Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(558));
        thePoints = new points();
        addObject(thePoints, 775, 575);
        boolean stopSnake = false;
        
        for (int i = 0; i < 10; i++)
        {
            addObject(new ant1(), Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(600));
        }
        
        for (int i = 0; i < 10; i++)
        {
            addObject(new ant2(), Greenfoot.getRandomNumber(800), Greenfoot.getRandomNumber(600));
        }
    }
    
    public points getPoints()
    {
        return thePoints;
    }
@danpost Would the getter methods give back an int to the frogboi so I can put it in a comparison?
Dolbypixar Dolbypixar

2017/8/27

#
Line 4 of the frogboi throws back the error message of "non-static method totesPoints() cannot be referenced from static context" And when I change it to 'int davePoints = Dave.totesPoints();' it throws back "cannot find symbol - method totesPoints()"
danpost danpost

2017/8/27

#
Okay. There are a couple of things you need to be aware of. By convention, class names should begin with an uppercase character and variables should begin with lowercase letters. By keeping with convention, not only is it easier to determine what was being named or referred to by us trying to help, but it also makes it easier for you to code things without getting these kind of error so frequently. Note that 'Points' is the name of the 'points' type object that contains the 'thePoints' value you with to access at line 4. So:
int davePoints = Points.totesPoints();
should work for 4. You cannot call the totesPoints method on 'Dave' because you have it as an 'Actor' type object -- not a 'points' type object. Had you used:
points Dave = (points)getWorld().getObjects(points.class).get(0);
for line 3, making 'Dave' a reference to a 'points' type object, then:
int davePoints = Dave.totesPoints();
would work.
Dolbypixar wrote...
@danpost Would the getter methods give back an int to the frogboi so I can put it in a comparison?
If you are referring to the getter method at lines 29 through 32 in your Grass class, then no. It is coded to return a 'points' type object with contains the 'thePoints' field that holds the int value. If you wanted it to return the int value, you could instead use:
public int getPoints()
{
    return thePoints.totesPoints();
}
Dolbypixar Dolbypixar

2017/8/27

#
Alright, so that works now, thanks so much. If I want my snake actor to get a boolean value from frogboi would I make like a public boolean type getter method in frogboi and call that in snake?
danpost danpost

2017/8/27

#
Dolbypixar wrote...
If I want my snake actor to get a boolean value from frogboi would I make like a public boolean type getter method in frogboi and call that in snake?
That would work if done correctly.
Dolbypixar Dolbypixar

2017/8/27

#
Alright, I was actually able to make that work. Just another question, Is there any way to pause just one actor for a short time. I've heard of the delay method but an issue that I've seen mentioned is that it pauses the whole program.
danpost danpost

2017/8/27

#
Dolbypixar wrote...
Is there any way to pause just one actor for a short time. I've heard of the delay method but an issue that I've seen mentioned is that it pauses the whole program.
Yes. You just need to add an int timer field:
private int paused;

public void act()
{
    if (paused > 0) --paused;
    if (paused == 0)
    {
        // code currently in your act method
    }
}
Somewhere in your act method (line 8 above), you will have 'paused' set to some positive value (55*number of seconds to pause) when some condition triggers the actor to pause for that short time. As an example:
if isTouching(Stunner.class)
{
    removeTouching(Stunner.class);
    paused = 145; // approx. 3 seconds to be paused
}
I had the Stunner object removed from the world; however, it is not required to remove it (provided that it is moving and will be off the actor after the timer expires). I guess what I am trying to point out is that the condition that triggered the pausing must eventually be cleared so that the actor can resume acting again. Also, not ALL actions of the actor need be paused. You might still want to allow the actor to perform some other non-relocating actions while paused (then again, you may not).
Dolbypixar Dolbypixar

2017/8/27

#
I tried to implement that but I don't think I did it right. Like it will pause for a second but then it slows down its speed. Is it supposed to do that or did I take a misstep somewhere?
public class snake extends Actor
{
    private int paused;

	/**
     * Act - do whatever the snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (paused > 0 ) 
		{
			--paused;
		}
		else if (paused == 0)
		{		
    		Actor Frogboi = (Actor)getWorld().getObjects(frogboi.class).get(0);
            frogboi daveSnake = (frogboi)getWorld().getObjects(frogboi.class).get(0);
    		boolean stopSnake = daveSnake.stoppingSnake();
    		//Add your action code here.
    
            if (stopSnake == true)
            {
                paused = 55;//for 1 second-ish
            }
            
            //movements of snake
            //follows frog
            turnTowards(Frogboi.getX(), Frogboi.getY());
            move(1);
            
            Actor frog = getOneIntersectingObject(frogboi.class);
            if (frog != null)
            {
                Greenfoot.playSound("fail.mp3");
                getWorld().removeObject(frog);
                Greenfoot.stop();
            }
        }    
    }
}
Yehuda Yehuda

2017/8/28

#
After it stops for a second (55 act cycles) lines 17-38 will run again. If you didn't make daveSnake.stoppingSnake back to false, then after every second there will be another pause (for 55 act cycles) since lines 22-25 are running.
Dolbypixar Dolbypixar

2017/8/28

#
Oh, okay, I see that now. I added a line in the beginning of my frogboi class so that stopSnake is set back to false. Thank you so much for the help, I would have been completely lost. :)
You need to login to post a reply.