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

2019/12/10

How to change my world with actors

Petr Petr

2019/12/10

#
i have a problem with changing level to next level. But when i want to get my actor to the world Greenfoot give me this java.lang.ClassCastException: class svět2 cannot be cast to class MyWorld (svět2 and MyWorld are in unnamed module of loader java.net.URLClassLoader @4440516c) at hráč.addedToWorld(hráč.java:32) any solution?
danpost danpost

2019/12/10

#
Petr wrote...
when i want to get my actor to the world Greenfoot give me this java.lang.ClassCastException: class svět2 cannot be cast to class MyWorld (svět2 and MyWorld are in unnamed module of loader java.net.URLClassLoader @4440516c) at hráč.addedToWorld(hráč.java:32)
Show class codes of actor being placed into world.
Petr Petr

2019/12/10

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

/**
 * Write a description of class hráč here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class hráč extends Actor
{
    // nastavení zvukového efektu
    GreenfootSound cink = new GreenfootSound("26f8b9_sonic_ring_sound_effect.mp3");
    //celková rychlost hráče
    int rychlost = 3;
    // určuje vzdálenost od pevného objektu
    int vzdálenost;
    //deklaruje svět v hráči
    MyWorld můjsvět;
    boolean maximumbodů = false;
    // pevně nastavaené směry do kterých se hráč může pohybovat
    class Směr{
        public static final int Nahoru = 270;
        public static final int Dolů = 90;
        public static final int Doprava = 0;
        public static final int Doleva = 180;
    
    }
    public hráč(){
    vzdálenost = getImage().getWidth();
    }
    public void addedToWorld(World w){
        můjsvět = (MyWorld)w;
    }
    // odělá body z mapy a přidá 5 score
    public void points(){
    Actor body = getOneIntersectingObject(points.class);
      if(body!=null){
        můjsvět.removeObject(body);
        můjsvět.addScore(5);
        cink.play();
        
      }
      
    }
    /**
     * Act - do whatever the hráč wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //pohyb po přes tlačítka na klávesnici
        
        
        if(Greenfoot.isKeyDown("W")){
            setRotation(Směr.Nahoru);
            moveHráč();
    }else if (Greenfoot.isKeyDown("S")){
            setRotation(Směr.Dolů);
            moveHráč();
    }else if(Greenfoot.isKeyDown("A")){
            setRotation(Směr.Doleva);
            moveHráč();
    }else if(Greenfoot.isKeyDown("D")){
            setRotation(Směr.Doprava);
            moveHráč();
    } 
       points();
    }
    public void moveHráč(){
        //zjišťuje pevnou souřadnici X
        int X = getX();
        //zjišťuje pevnou souřadnici Y
        int Y = getY();
        //zjišťuje rotaci
        int Rotace = getRotation();
        int ZměnaX = getZměnaX(Rotace);
        int ZměnaY = getZměnaY(Rotace);
        int přídavekKeX = adjustOffset(ZměnaX);
        int přídavekKeY = adjustOffset(ZměnaY);
        Actor rock = getOneObjectAtOffset(adjustOffset(ZměnaX),adjustOffset(ZměnaY),rock.class);
        if(rock == null){
            setLocation(X + ZměnaX ,Y + ZměnaY);
        }
        
    }
    private int getZměnaX(int Rotace){
        if(Rotace == Směr.Doprava){
          return rychlost;
        }
        if(Rotace == Směr.Doleva){
            return -rychlost;
        }       
        return 0;
    }
    private int getZměnaY(int Rotace){
        if(Rotace == Směr.Dolů){
          return rychlost;
        }
        if(Rotace == Směr.Nahoru){
            return -rychlost;
        }   
        return 0;
    }
    private int adjustOffset(int mimo){
      int známkanarazu = (int)Math.signum(mimo);
      int vzádálenostod = vzdálenost/2;
      int adjustAmount = vzádálenostod * známkanarazu; 
      return mimo + adjustAmount;
    }
}
Petr Petr

2019/12/10

#
this is the code
Petr Petr

2019/12/10

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class enemy here. * * @author (your name) * @version (a version number or a date) */ public class enemy extends Actor { /// přidává svět do actora MyWorld můjsvět; public static final int nahoru = 270; public static final int doleva = 180; public static final int dolů = 90; public static final int doprava = 0; /** * Act - do whatever the enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // random pohyb po herní ploše if(getWallInFront()==false) { move (2); }else { int random = Greenfoot.getRandomNumber(3); if(random == 0 ) { turn(nahoru); }else if(random == 1) { turn(dolů); }else if(random == 2) { turn(doprava); }else if(random == 3) { turn(doleva); } } // oddělaní hráče z herní plochy po doteku z nepřítelem Actor hráč = getOneIntersectingObject(hráč.class); if(hráč!=null) { World můjsvět = getWorld(); konec Konec = new konec(); restart Restart = new restart(); můjsvět.addObject(Konec,můjsvět.getWidth()/2,můjsvět.getHeight()/2); můjsvět.removeObject(hráč); můjsvět.addObject(Restart,400,150); } } // funkce zjištůjící doteku zdi z nepřítelem private boolean getWallInFront() { GreenfootImage myImage = getImage(); int distanceToFront = myImage.getWidth()/2; int xoffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); int yoffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation()))); Actor wall = getOneObjectAtOffset(xoffset,yoffset, rock.class); return (wall!=null); } public void přehrátzvuk() { } }
Petr Petr

2019/12/10

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

/**
 * Write a description of class lulv here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class lulv extends Actor
{
    MyWorld můjsvět;
    int width;
    int height = 60;
    public int score = 0;
    GreenfootImage boardImage;
    // metoda která přidává do světa počitadlo score
    public void addedToWorld(World w){
      můjsvět = (MyWorld)w;
      width = můjsvět.getWidth();
      boardImage = new GreenfootImage (width ,height);
      boardImage.setColor(Color.BLACK);
      boardImage.fillRect(0,0,width,height);
      setImage(boardImage);
    }
    //přidává množství nazbíraných bodů
    public void addScore(int množství){
      score += množství;
      if(score >= 50)
       {
       
       
           if (getWorld() instanceof MyWorld) Greenfoot.setWorld(new svět2());
           else if (getWorld() instanceof svět2) Greenfoot.setWorld(new vyhráls());
        }
    } 
    

    
    // vypisuje score 
    public void drawScore(){
        boardImage.setColor(Color.BLACK);
        boardImage.fillRect(0,0,width,height);
        boardImage.setColor(Color.RED);
        boardImage.setFont(new Font("Arial",24));
        boardImage.drawString("Score:" + score,20,40);
    }
    
    
    /**
     * Act - do whatever the lulv wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // vypisuje score do světa
        drawScore();
        
    }    
}
danpost danpost

2019/12/10

#
In your addedToWorld method in the hráč class, you need to verify what type of World subclass the actor was placed in (MyWorld or something else) using instanceof. Probably need to do the same in your other Actor subclasses as well.
Petr Petr

2019/12/11

#
thank you sooooooo much . You saved my life .
Petr Petr

2019/12/11

#
now i have problem with my score counter i dont know why java.lang.NullPointerException at lulv.drawScore(lulv.java:44) at lulv.act(lulv.java:59)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class lulv here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class lulv extends Actor
{
    MyWorld můjsvět;
    svět2 můjsvět2;
    int width;
    int height = 60;
    public int score = 0;
    GreenfootImage boardImage;
    // metoda která přidává do světa počitadlo score
    public void addedToWorld(World w,svět2 w2){
      můjsvět = (MyWorld)w;
      můjsvět2 = (svět2)w2;
      width = můjsvět.getWidth();
      width = můjsvět2.getWidth();
      boardImage = new GreenfootImage (width ,height);
      boardImage.setColor(Color.BLACK);
      boardImage.fillRect(0,0,width,height);
      setImage(boardImage);
    }
    //přidává množství nazbíraných bodů
    public void addScore(int množství){
      score += množství;
      if(score >= 50)
       {
       
       
           if (getWorld() instanceof MyWorld) Greenfoot.setWorld(new svět2());
           else if (getWorld() instanceof svět2) Greenfoot.setWorld(new vyhráls());
        }
    } 
    

    
    // vypisuje score 
    public void drawScore(){
        boardImage.setColor(Color.GREEN);
        boardImage.fillRect(0,0,width,height);
        boardImage.setColor(Color.RED);
        boardImage.setFont(new Font("Arial",24));
        boardImage.drawString("Score:" + score,20,40);
    }
    
    
    /**
     * Act - do whatever the lulv wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // vypisuje score do světa
        drawScore();
        
    }    
}
danpost danpost

2019/12/11

#
Now your addedToWorld method has too many parameters. So, it does not have the correct method signature to be automatically called by greenfoot when the actor is added to the world. Hence, boardImage is not assigned any GreenfootImage object and remains containing a null value. I, however, do not see anything in the code that would require the need to know what type of world the actor is in. If all you need is the width of the world:
int width = getWorld().getWidth();
would be sufficient:
import greenfoot.*;

public class lulv extends Actor
{
    int score;
    
    protected void addedToWorld(World w)
    {
        setImage(new GreenfootImage(w.getWidth(), 60);
        getImage().setFont(new Font("Ariel", 24));
        drawScore();
    }
    
    private void drawScore()
    {
        getImage().setColor(Color.GREEN);
        getImage().fill();
        getImage().setColor(Color.RED);
        getImage().drawString("Score:"+score, 20, 40);
    }
    
    public void addScore(int množství)
    {
        score += množství;
        drawScore();
        if (score < 50) return;
        if (getWorld() instanceof MyWorld) Greenfoot.setWorld(new svět2());
        else if (getWorld() instanceof svět2) Greenfoot.setWorld(new vyhráls());
    }
}
No act method is needed for this actor. It does not do anything on its own. It just sits in the world.
You need to login to post a reply.