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

2017/3/27

HELP! Counter Problems

salvo salvo

2017/3/27

#
why this dont work ?
public class ProjektilGelb extends Actor
{

       /**
         * diese Methode wird Abfragen ob ProjektilGelb getroffen hat -> 
         * hat es die Rakete getroffen wird der Score um 1 erhöht.
         * Das Projektil wird dabei verschwinden
         */
    public void act()//ProjektilGelb tut was auch immer in der "act" Methode drin steht
    {
        moveEast();//bewegt sich nach Osten (rechts)

        if (atWorldEdge())  
        {  
            getWorld().removeObject(this);  
        }
        if (hitDetection()) 
        {
            getWorld().removeObject(this);   
            World myWorld = getWorld();
            SpaceWelt SpaceWelt = (SpaceWelt)myWorld;
            Counter1 counter1 = SpaceWelt.getCounter1(); 
            //Counter1.addScore(); 
        }
    }
    /**
     * Abfrage ob das Projektil auf einen Gegenstand trifft
     */
    public boolean hitDetection()  
    {
        if (isTouching(Rocket2.class))
            return true;
        else
            return false;
    }     
    /**
     * Abfrage ob das Projektil am Ende der Welt ist
     */
    public boolean atWorldEdge()
    {  
        if(getX() == 100)  
            return true;  
        else 
            return false;  
    }  
    /**
     * Methode, bei der das Projektil nach links fliegt
     */
    public void moveEast()//bewegung von ProjektilGelb wird deffiniert 
    {
        setLocation(getX()+1,getY());//bewegt sich nach rechts
    }
}
Super_Hippo Super_Hippo

2017/3/27

#
You have to change the 'if' in line 17 to 'else if' and move line 19 to line 23. It would be helpful if you would describe what is happening, what should happen (the difference between both) and if you are receiving errors (compiling or run time errors) and where. Then, if someone wants to help you, he doesn't have to guess what could be wrong.
salvo salvo

2017/3/27

#
so the counter should add one point to the score if this object hits the Rocket2
danpost danpost

2017/3/27

#
salvo wrote...
so the counter should add one point to the score if this object hits the Rocket2
If you uncomment line 23 and change 'Counter1' to 'counter1' on that line.
salvo salvo

2017/3/27

#
that simple -.- thanks Dan and thanks Hippo :D
salvo salvo

2017/3/27

#
could i ask you the next question whats wrong here Greenfoot says the problem is in line 9
public class Counter1 extends Actor
{
    int score = 0;
    /**
     * Erstellt den Counter für Rakete 1
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.WHITE, Color.BLACK));
    }
    /**
     * Addiert 1 auf den Score
     */
    public void addScore()
    {
        score++; 
    }
}
danpost danpost

2017/3/27

#
salvo wrote...
could i ask you the next question whats wrong here Greenfoot says the problem is in line 9 < Code Omitted>
There does not appear to be anything wrong. What exactly does Greenfoot say is wrong with it?
salvo salvo

2017/3/27

#
in line 9 under BLACK and White there is this message: "incompatible types: java.awt.Color cannot be converted to Greenfoot.Color
danpost danpost

2017/3/27

#
salvo wrote...
in line 9 under BLACK and White there is this message: "incompatible types: java.awt.Color cannot be converted to Greenfoot.Color
Remove the following line from the top of the code:
import java.awt.Color;
salvo salvo

2017/3/27

#
ok Greenfoot says its all ok but when i Play and try it this appears in a terminal window java.lang.NullPointerException at ProjektilGelb.act(ProjektilGelb.java:32) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
salvo salvo

2017/3/27

#
i am very grateful that you guys can help me THANKS !
salvo salvo

2017/3/27

#
this appears when the method Hitdetection is true
danpost danpost

2017/3/27

#
Re-post the entire ProjektilGelb class (including imports).
salvo salvo

2017/3/27

#
import greenfoot.*;

/**
 * Write a description of class ProjektilGelb here.
 * 
 * @author  
 * @author 
 * @version 0.69
 * 
 */
public class ProjektilGelb extends Actor
{
 
       /**
         * diese Methode wird Abfragen ob ProjektilGelb getroffen hat -> 
         * hat es die Rakete getroffen wird der Score um 1 erhöht.
         * Das Projektil wird dabei verschwinden
         */
    public void act()//ProjektilGelb tut was auch immer in der "act" Methode drin steht
    {
        moveEast();//bewegt sich nach Osten (rechts)
 
        if (atWorldEdge())  
        {  
            getWorld().removeObject(this);  
        }
        else if (hitDetection()) 
        {
            getWorld().removeObject(this);   
            World myWorld = getWorld();
            SpaceWelt SpaceWelt = (SpaceWelt)myWorld;
            Counter1 counter1 = SpaceWelt.getCounter1(); 
            counter1.addScore(); 
        }
    }
    /**
     * Abfrage ob das Projektil auf einen Gegenstand trifft
     */
    public boolean hitDetection()  
    {
        if (isTouching(Rocket2.class))
            return true;
        else
            return false;
    }     
    /**
     * Abfrage ob das Projektil am Ende der Welt ist
     */
    public boolean atWorldEdge()
    {  
        if(getX() == 100)  
            return true;  
        else
            return false;  
    }  
    /**
     * Methode, bei der das Projektil nach links fliegt
     */
    public void moveEast()//bewegung von ProjektilGelb wird deffiniert 
    {
        setLocation(getX()+1,getY());//bewegt sich nach rechts
    }
}
danpost danpost

2017/3/27

#
Super_Hippo wrote...
and move line 19 to line 23.
This was said earlier about the class code. Actually, moving it down one line is enough. It is line 29 in the most recent code post that must be moved down (one line is sufficient).
You need to login to post a reply.