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

2020/2/25

int in showText

1
2
danpost danpost

2020/2/27

#
Yoshi21137 wrote...
and it won't construct the world until whatever is wrong is fixed tried the first option but it says there is also something wrong with: << Code Omitted >>
Show adjusted Knight class code (entire class).
Yoshi21137 Yoshi21137

2020/2/27

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

/**
 * Write a description of class Knight here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Knight extends Players
{
    Main main = (Main)getWorld();
    int kills = main.kills;
    int bulletSpeed = 100;
    int lives = ((Main)getWorld()).lives;    
    /**
     * Act - do whatever the Knight wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        looseHealth();
    }
    
    public void looseHealth()
    {
        if (canSee(GoblinShaman.class))
        {
            lives = lives-1;
        }
    }
    
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            turn(-90);
            move(5);
            turn(90);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            turn(90);
            move(5);
            turn(-90);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-5);
        }
        if(Greenfoot.isKeyDown("enter") && getObjectsInRange(bulletSpeed, Bullet.class).isEmpty())
        {
            getWorld().addObject(new Bullet(), getX()+40, getY()+8);     
        }
    }
}
danpost danpost

2020/2/27

#
You cannot dot getWorld in a field declaration line (line 14). It is executed during object (actor, in this case) creation and will return a null value since the actor cannot be in a world while being created. Doing anything to a null value will result in a NullPointerException error. Honestly, it is not wise to have two different classes hold the same value in fields of primitive type (int, double, boolean, long, etc.). Keep its value with the object that it describes and have it accessed when needed by other objects.
Yoshi21137 Yoshi21137

2020/2/27

#
Now it is showing there is a problem in here
public void die()
{
    if (canSee(Bullet.class))
    {
        (main).kills = (main).kills+1
        main.removeObject(this)
    }
}
Yoshi21137 Yoshi21137

2020/2/27

#
I forgot to type the semicolon at the end of line 7 when posting
danpost danpost

2020/2/27

#
Where is the line to define main?
Yoshi21137 Yoshi21137

2020/2/27

#
I tried using that but it is saying there is a problem
public void die()
    {
        if (canSee(Bullet.class))
        {
            (main).kills = (main).kills+1;
            main.removeObject(this);
        }
    }
(main).kills = (main).kills+1;
Yoshi21137 Yoshi21137

2020/2/27

#
Main main = (main)getWorld();

public void die()
    {
        if (canSee(Bullet.class))
        {
            (main).kills = (main).kills+1;
            main.removeObject(this);
        }
    }
danpost danpost

2020/2/27

#
You cannot expect the actor (who dies from being hit by a bullet) to be in a world while being created. Your main field will be set to null as it is assigned the value of (main)getWorld() while it is being created. Move line 1 to inside the if clause.
Yoshi21137 Yoshi21137

2020/2/27

#
I used that and moved it to the bullet class and it works but the counter doesn't this is the current code:
public void kill()
    {
        if (isTouching(GoblinShaman.class))
        {
            Main main = (Main)getWorld();
            removeTouching(GoblinShaman.class);
            main.kills = main.kills+1;
        }
    }
    int kills = 0;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Main()
    {    
        super(1000, 650, 1); 
        prepare();
        showText(""+lives, 73, 42);
        showText(""+kills, 73, 66);
    }
danpost danpost

2020/2/28

#
Maybe you should change line 7 so that, instead of just changing the value of kills, you call a method in Main that changes its value AND updates the display.
Yoshi21137 Yoshi21137

2020/2/28

#
How would I do that?
Yoshi21137 Yoshi21137

2020/2/28

#
It works now. Thanks for helping me and for staying with me for the 3 days you did!
You need to login to post a reply.
1
2