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

2022/1/4

Ending a programm

1
2
3
jdkdoen05 jdkdoen05

2022/1/25

#
int numberOfCheckPoints = getWorld().getObjects(Checkpoint.class).size(); CheckPoint checkPoint = (CheckPoint)getOneIntersectingObject(CheckPoint.class); if(isTouching(CheckPoint.class) && distanceCompleted + 1)%numberOfCheckPoints = identificationNumber) { distanceCompleted++; if (distanceCompleted == 20*numberOfCheckPoints) { Greenfoot.stop(); } }
danpost danpost

2022/1/25

#
Compare my line 15 with line 3 above. Notice the differences in formatting.
jdkdoen05 jdkdoen05

2022/2/8

#
So now the programm doenst stops.
danpost danpost

2022/2/8

#
jdkdoen05 wrote...
So now the programm doenst stops.
Show complete class codes. Please use code tags (see link "Posting code? read this!" below the reply box).
jdkdoen05 jdkdoen05

2022/2/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/** 
 * Write a description of class Gegner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spieler2  extends Mover
{
    GreenfootImage gameName = new GreenfootImage ("Winner 2.png");
    private int distanceCompleted;
    private int checkpointzahl = 0;
    public void act()
    {
        if (Greenfoot.isKeyDown("a")) 
        {
        turn(-3);
        move(1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
        turn(3);
        move(1);
        }
        if (Greenfoot.isKeyDown("s")) 
        {
        move();
        }
        if (Greenfoot.isKeyDown("w"))
        {
        backward();
        }
        int numberOfCheckPoints = getWorld().getObjects(Checkpoint.class).size();
        Checkpoint checkPoint = (Checkpoint)getOneIntersectingObject(Checkpoint.class);
        if (checkPoint != null && (distanceCompleted + 1)%numberOfCheckPoints == checkPoint.identificationNumber);
        {
            distanceCompleted++;
            if (distanceCompleted++ == 20*numberOfCheckPoints) 
            {
                
                setImage (gameName);
            }
        }
        Checkpointzählen();
    }
    public void Checkpointzählen()
    {
        if (this.isTouching(Checkpoint.class))
        {
            checkpointzahl = checkpointzahl + 1;
        }
        if (checkpointzahl == 20)
        {
        Greenfoot.setWorld(new Winner2());
        }
    }
}
jdkdoen05 jdkdoen05

2022/2/14

#
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;

/**
 * Diese Klasse ist die Basisklasse für alle Tiere.
 * Zusätzlich zu der Standardmethoden aus der Klasse Actor bietet diese Klasse die
 * Möglichkeit zum Bewegen und Drehen an.
 * 
 * @author Michael Kolling
 * @author Gunnar Johannesmeyer (Übersetzung und kleinere Anpassung)
 * @version 1.0

 */
public class Mover extends Actor
{
    // Konstante
    private static final double BEWEGUNGSGESCHWINDIGKEIT = 3.5;
    
    /**
     * Konstruktor der Klasse Tier - hier wird nichts gemacht.
     */
    public Mover()
    {
        
    }
    
    
    /**
     * Drehe den übergebenen Wert in Grad nach rechts.
     * Das p vor pGrad zeigt an, dass es ein Parameter (Übergabewert) ist.
     */
    public void turn(int pGrad)
    {
        setRotation(getRotation() + pGrad);
    }
    

    /**
     * Bewegt Objekt in aktueller Richtung nach vorne.
     */
    public void move()
    {
        double grad = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(grad) * BEWEGUNGSGESCHWINDIGKEIT);
        int y = (int) Math.round(getY() + Math.sin(grad) * BEWEGUNGSGESCHWINDIGKEIT);
        
        setLocation(x, y);
    }
    
    /**
     * Bewegt Objekte in aktueller Richtung nach hinten. (Rückwärtsgang)
     */
    public void backward()
    {
     double grad = Math.toRadians( getRotation() );
        int x = (int) Math.round(getX() + Math.cos(grad) * -BEWEGUNGSGESCHWINDIGKEIT);
        int y = (int) Math.round(getY() + Math.sin(grad) * -BEWEGUNGSGESCHWINDIGKEIT);
        
        setLocation(x, y);
    }
    

    
    
    

   
    
    /**
     * Versuche, ein Objekt des Typs pKlassentyp zu fressen.
     * Dies ist nur dann möglich, wenn sich an der aktuellen Position ein Objekt des Typs befindet.
     * Anderfalls macht diese Methode nichts.
     */
    public void eat(Class pKlassentyp)
    {
        Actor akteur = getOneObjectAtOffset(0, 0, pKlassentyp);
        if(akteur != null) {
            getWorld().removeObject(akteur);
        }
    }
   
}
jdkdoen05 jdkdoen05

2022/2/14

#
The first one is the Player and the second one ist the Mover.
danpost danpost

2022/2/14

#
In Spieler2 class, remove lines 46 thru 57. Then, replace lines 43 with what was line 56.
jdkdoen05 jdkdoen05

2022/2/14

#
Thanks, but now we have the problem that there is no ending after touching 20 checkpoints.
danpost danpost

2022/2/14

#
jdkdoen05 wrote...
Thanks, but now we have the problem that there is no ending after touching 20 checkpoints.
Show revised Spieler2 class codes.
jdkdoen05 jdkdoen05

2022/2/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/** 
 * Write a description of class Gegner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spieler2  extends Mover
{
    GreenfootImage gameName = new GreenfootImage ("Winner 2.png");
    private int distanceCompleted;
    private int checkpointzahl = 0;
    public void act()
    {
        if (Greenfoot.isKeyDown("a")) 
        {
        turn(-3);
        move(1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
        turn(3);
        move(1);
        }
        if (Greenfoot.isKeyDown("s")) 
        {
        move();
        }
        if (Greenfoot.isKeyDown("w"))
        {
        backward();
        }
        int numberOfCheckPoints = getWorld().getObjects(Checkpoint.class).size();
        Checkpoint checkPoint = (Checkpoint)getOneIntersectingObject(Checkpoint.class);
        if (checkPoint != null && (distanceCompleted + 1)%numberOfCheckPoints == checkPoint.identificationNumber);
        {
            distanceCompleted++;
            if (distanceCompleted++ == 21*numberOfCheckPoints) 
            {
                Greenfoot.setWorld(new Winner2());
            }
        }
    }
}








danpost danpost

2022/2/14

#
For testing purposes, add the following line after line 39:
getWorld().showText("Distance: "+distanceCompleted, 150, 50);
Also (just to reduce time consumption), remove the "21*" from line 40. Run and report back what results you get.
jdkdoen05 jdkdoen05

2022/2/15

#
So now the text appears and starts counting, but it also never ends as also the game never ends.
danpost danpost

2022/2/15

#
jdkdoen05 wrote...
So now the text appears and starts counting, but it also never ends as also the game never ends.
Okay, I see the issue now. Remove the "++" in line 40 (the field was already incremented on line 39). I missed that before.
jdkdoen05 jdkdoen05

2022/2/22

#
So we now did that but Player2 wins after just 1 second. The programm ends after just 2 seconds.
There are more replies on the next page.
1
2
3