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

2017/1/25

Can't access Pacman from another class

TheGreenFoot TheGreenFoot

2017/1/25

#
Hello guys, I am currrently programming a game for school where the player can move pacman and has to dodge the lobsters. A am trying to access the "stopThatSound" method from the Lobster so the sound stops when Pacman is killed, but it says "not a statement" (at the dot betweeen pacman and stopThatSound) when I try to access the method from the Lobster class. Can someone help me pls? (Sorry for my bad English, I am from Germany.) This is my Lobster class.
public class Lobster extends Animal
{

    public void act()
    {
        turnAtEdge();
        randomTurn();
        move();
        lookForCrab();
    }

    public void turnAtEdge()
    {
        if ( atWorldEdge() ) 
        {
            turn(17);
        }
    }

    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(100) > 90) {
            turn(Greenfoot.getRandomNumber(90)-45);
        }
    }

    /**
     * Versucht eine Krabbe zu fangen; d.h. prüft, ob wir auf eine Krabbe gestoßen sind.
     * Wenn ja, wird die Krabbe aus dem Spiel entfernt und die Ausführung des Programms beendet.
     */
    public void lookForCrab()
    {
        if ( canSee(Pacman.class) ) 
        {
           eat(Pacman.class);
           Pacmansworld pacmansworld = getWorld();
           Pacman pacman = pacmansworld.getPacman();
           pacman.stopThatSound;
        }
    }
}
This is the World class' code:
public class PacmansWorld extends World
{
    private Pacman thePacman;
    
    /**
     * Constructor for objects of class PacmansWorld.
     * 
     */
    public PacmansWorld()
    {    
        super(1000, 1000, 1);
        thePacman = new Pacman();
        addObject(thePacman, 5, 5);
    }
    
    public Pacman getPacman()
    {
        return thePacman;
    }
    
    
}
What did I do wrong?
danpost danpost

2017/1/25

#
You will need to show the Pacman class as well.
TheGreenFoot TheGreenFoot

2017/1/25

#
Ok sry for that.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.Random;
import java.awt.Color;

public class Pacman extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int thingsEaten;
    private int whichImage;
    public GreenfootSound wakawaka = new GreenfootSound("wakawaka.mp3");
    
    /**
     * Erzeugt einen Pacman und initialisiert ihre beiden Bilder.
     */
    public Pacman()
    {
        image1 = new GreenfootImage("Pacman.png");
        image2 = new GreenfootImage("Pacman2.png");
        thingsEaten = 0;
        setImage(image1);
        whichImage = 1;
        wakawaka.play();
    }
 
    public void act()
    {
        checkKeypress();
        switchImage();
    }
    
    /**
     * Wechselt das Bild des Pacman zwischen Pacman und ClosedPacman.
     */
    public void switchImage()
    {
        if (whichImage == 1)
        {
            setImage(image2);
            whichImage = 2;
        }
        else
        {
            setImage(image1);
            whichImage = 1;
        }
    }
            
    /**
     * Prüft, ob eine Steuertaste auf der Tastatur gedrückt wurde.
     * Wenn ja, reagiert die Methode entsprechend.
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left")) 
        {
            turn(-10);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(10);
        }
        if (Greenfoot.isKeyDown("up")) 
        {
            move();
            move();
        }
    }

    /**
     * Prüft, ob wir auf einen Wurm gestoßen sind.
     * Wenn ja, wird er gefressen. Wenn nein, passiert nichts.
     */
    public void lookForWorm()
    {
        if ( canSee(Worm.class) ) 
        {
            eat(Worm.class);
            Greenfoot.playSound("slurp.wav");                 
            thingsEaten ++;
            if (thingsEaten > 5)
            {
                Greenfoot.playSound("fanfare.wav");
                Greenfoot.stop();
            }    
        }
    }
    
   
     public void stopThatSound()
    {
        wakawaka.stop();
    }

}
danpost danpost

2017/1/25

#
I see it now. All method calls must include a set of parenthesis. Line 38 of the Lobster class does not have them, so the compiler things you are referring to a variable instead of calling a method. I am kind of surprised that it said 'not a statement' and not 'cannot find symbol -- variable stopThatSound'.
TheGreenFoot TheGreenFoot

2017/1/25

#
Dammit. Thank you so much!
TheGreenFoot TheGreenFoot

2017/1/25

#
Now it says "cannot find symbol - class Pacmansworld in line 36 of the Lobster -_-
danpost danpost

2017/1/25

#
TheGreenFoot wrote...
Now it says "cannot find symbol - class Pacmansworld in line 36 of the Lobster -_-
Capitalize the 'w'.
TheGreenFoot TheGreenFoot

2017/1/25

#
And I forgot to say: Worm, Lobster and Pacman are subclasses of Animal:
import greenfoot.*;

import java.util.List;
import java.util.ArrayList;

/**
 * Animal. Dies ist die Basisklasse für alle Tiere. Zusätzlich zu den Standardmethoden 
 * der Actor-Klasse verleiht sie die Fähigkeit, sich zu bewegen und zu drehen.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Animal extends Actor
{
    private static final double WALKING_SPEED = 5.0;
    
    /**
     * Konstruktor für Animal - ohne Aufgabe.
     */
    public Animal()
    {
    }

    /**
     * Leere Methode. Tiere verfügen über keine Standardaktionen.
     */
    public void act()
    {
    }
    
    
    /**
     * Dreht 'angle' Grad nach rechts (im Uhrzeigersinn).
     */
    public void turn(int angle)
    {
        setRotation(getRotation() + angle);
    }
    

    /**
     * Rückt vorwärts in die aktuelle Richtung.
     */
    public void move()
    {
        double angle = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
        int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
        
        setLocation(x, y);
    }

    
    /**
     * Prüft, ob wir nahe an einem der Ränder der Welt sind. Liefert true zurück, wenn ja.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
    
    /**
     * Liefert true zurück, wenn wir genau dort, wo wir sind, ein Objekt der Klasse 'clss' sehen. 
     * False, wenn dort kein solches Objekt vorhanden ist.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    
    /**
     * Versucht, ein Objekt der Klasse 'clss' zu fressen. Dies ist nur erfolgreich, wenn dort,
     * wo wir gerade sind, ein solches Objekt vorhanden ist. Andernfalls macht diese Methode
     * nichts.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
}
danpost danpost

2017/1/25

#
TheGreenFoot wrote...
And I forgot to say: Worm, Lobster and Pacman are subclasses of Animal
I noticed, however.
TheGreenFoot TheGreenFoot

2017/1/25

#
Yeah i know but maybe u wanted that code too...
TheGreenFoot TheGreenFoot

2017/1/25

#
changed it and now it says: incompatible types: greenfoot.World cannot be converted to pacmans world in line 36...
danpost danpost

2017/1/25

#
The 'getWorld' method return a reference to a World object, not a PacmansWorld object. You cannot assign an World object to a reference variable that is to hold a PacmansWorld object. You need to typecast the returned value to show that it is indeed a PacmansWorld object:
PacmansWorld pacmansworld = (PacmansWorld) getWorld();
TheGreenFoot TheGreenFoot

2017/1/25

#
Thank you, now running without errors!
You need to login to post a reply.