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

2020/2/25

int in showText

1
2
Yoshi21137 Yoshi21137

2020/2/25

#
I am trying to put an integer into the showText method but it comes up with: int cannot be converted into java.lang.string this is my current code:
1
2
3
4
5
6
public Main()
{   
    super(1000, 650, 1);
    prepare();
    showText(lives, 50, 50);
}
danpost danpost

2020/2/26

#
You can concatenate an int to an empty String object:
1
String text = ""+lives;
Yoshi21137 Yoshi21137

2020/2/26

#
Thanks, but now there is a problem with:
1
int kills = ((Main)getWorld().getObjects(Main.class).get(0)).kills;
and:
1
getWorld().addObject(new Bullet(), getX()+40, getY()+8);
danpost danpost

2020/2/27

#
Yoshi21137 wrote...
Thanks, but now there is a problem with: << Codes Omitted >>
Not enough code provided to conclude anything. What is the class name of your world and also what is the class name of the class where "kills" is located?
Yoshi21137 Yoshi21137

2020/2/27

#
int kills is in the "Main" World class Main is the world name The second line of code is in the player class
Yoshi21137 Yoshi21137

2020/2/27

#
This is the World (Main)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Main extends World
{
    int spawnBoss = 0;
    int lives = 3;
    int kills = 0;
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public Main()
    {   
        super(1000, 650, 1);
        prepare();
        showText(""+lives, 73, 42);
        showText(""+kills, 73, 66);
        if (lives == 0)
        {
            //removeObject(knight);
        }
    }
 
    public void act()
    {
        if (kills == 20)
        {
            spawnBoss = spawnBoss+1;
        }
        if (spawnBoss == 1)
        {
            addObject(new GoblinPriest(), 0, 325);
            addObject(new BossHealth(), 750, 235);
            spawnBoss = spawnBoss+1;
        }
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Score score = new Score();
        addObject(score,75,55);
        Knight knight = new Knight();
        addObject(knight, 250, 325);
    }
}
Yoshi21137 Yoshi21137

2020/2/27

#
And this is where I am calling the int from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
{
    int bulletSpeed = 100;
    int kills = 0;
    //int lives = ((Main)getWorld().getObjects(Main.class).get(0)).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();
        //score();
        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

#
If Main is the world class, then why are you getting (Actor) objects in the first line?
Yoshi21137 Yoshi21137

2020/2/27

#
I don't know how it works
danpost danpost

2020/2/27

#
Yoshi21137 wrote...
I don't know how it works
The getWorld method gets the world -- and that is what you want to cast as type Main:
1
Main main = (Main)getWorld();
Yoshi21137 Yoshi21137

2020/2/27

#
would I put that in the code?
danpost danpost

2020/2/27

#
Yoshi21137 wrote...
would I put that in the code?
You could put it before your line 1 and adjust your line to use main:
1
2
Main main = (Main)getWorld();
int kills = main.kills;
or combine them as:
1
int kills = ((Main)getWorld()).kills;
Yoshi21137 Yoshi21137

2020/2/27

#
But now it is saying there is something wrong with:
1
int kills = ((Main)getWorld()).kills;
Yoshi21137 Yoshi21137

2020/2/27

#
and it won't construct the world until whatever is wrong is fixed
Yoshi21137 Yoshi21137

2020/2/27

#
tried the first option but it says there is also something wrong with:
1
int kills = main.kills;
There are more replies on the next page.
1
2