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

2016/11/14

How to create an image from clicking a button in one world to another

Kieranwilko98 Kieranwilko98

2016/11/14

#
I'm making a game for college and I'm not the best of coders anyway, but basically I want to add a smaller version of a ball chosen from a world called 'choose_ball' into a world called 'main'. When I run it, it says; java.lang.ClassCastException: choose_ball cannot be cast to main at yel_ball.act(yel_ball.java:13) 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private BallCol colour ;
    
    public main()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
       super(600, 400, 1);
       grass_1 gs=new grass_1();
       addObject (gs,300,200);
       hole ho=new hole();
       addObject (ho,545,320);
       colour= new BallCol();
       addObject(colour,100,250);
    }
    public BallCol getBallCol()
    {
        return colour ;
     
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class BallCol extends Actor
{
    
    public int set;
    public void choose(int col)
    {
        set=col;
        GreenfootImage myImage=null;
        if(set==1){
          myImage=new GreenfootImage("red_ballXS.fw.png");
        }
        else if(set==2){
          myImage=new GreenfootImage("blue_ballXS.fw.png");
        }
         else if(set==3){
          myImage=new GreenfootImage("black_ballXS.fw.png");
        }
        else if(set==4){
          myImage=new GreenfootImage("yel_ballXS.fw.png");
        }
        setImage(myImage);
    }   
}
I have 4 different ball classes all with the same code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class red_ball extends Actor
     
    public void act()
    {
        // Add your action code here.
         
            if (Greenfoot.mouseClicked(this)) {
            Greenfoot.setWorld(new main());
            main mainWorld=(main) getWorld();
            BallCol BallCol=mainWorld.getBallCol();
            BallCol.choose(1);
             
        }
    }
Any ideas of why this error is happening and how to fix it?
danpost danpost

2016/11/14

#
In your red_ball class (and I presume the other 3 ball classes), line 10 above will only work if the ball is in a 'main' world. It fails if the ball is in any other type world. You must ensure that the world is indeed a 'main' world before executing that last bit of code:
1
2
3
4
5
6
7
// starting at line 10 above
if (getWorld() instanceof main)
{
    main mainWorld = (main)getWorld();
    BallCol ballCol = mainWorld.getBallCol();
    ballCol.choose(1);
}
Kieranwilko98 Kieranwilko98

2016/11/21

#
Thank you for that help but when the ball is clicked nothing happens, so I added this piece of code
1
2
3
4
5
6
7
8
if(Greenfoot.mouseClicked(this)){
Greenfoot.setWorld(new main());
}
if(getWorld() instanceof main){
main mainWorld=(main) getWorld();
BallCol BallCol=mainWorld.getBallCol();
BallCol.choose(3);
}
Now it runs without an error but the ball doesn't appear on the screen, instead a little Greenfoot picture appears, do you know what's going wrong ?
Super_Hippo Super_Hippo

2016/11/21

#
So the red_ball object is only in the menu world in which you choose the ball and in the real world (the main), you only have this BallCol ball, right? Then this should work I think.
1
2
3
4
5
6
7
if (Greenfoot.mouseClicked(this))
{
    main mainWorld = new main();
    BallCol ballCol=mainWorld.getBallCol();
    ballCol.choose(3);
    Greenfoot.setWorld(mainWorld);
}
Your code didn't work because you set a new world but the 'getWorld' will still return the world in which the object is in and not the new one.
danpost danpost

2016/11/21

#
If you do not want a new 'main' world to be created when the BallCol object is clicked on while in a 'main' world (but only when it is in a 'choose_ball' world, then your first line should be:
1
if (getWorld() instanceof choose_ball && Greenfoot.mouseClicked(this))
Having 4 different classes does not make any sense here when you decide one of four colors in each class. Use just one class and, at line 7 in your last code post, instead of 'choose(1)', 'choose(2)', etc., use 'choose(set)'.
Kieranwilko98 Kieranwilko98

2016/11/24

#
Thank you for all your help but I'm afraid none of them actually work, basically a ball actor is clicked in the world 'choose ball' and when this ball is clicked I want a new world called 'main' to open and the Ball to be placed in that world, hopefully this may help clarify things a bit clearer but if not don't worry.
danpost danpost

2016/11/24

#
Kieranwilko98 wrote...
a ball actor is clicked in the world 'choose ball' and when this ball is clicked I want a new world called 'main' to open and the Ball to be placed in that world
Well, you could continue with the 'if' supplied above:
1
2
3
4
5
6
if (getWorld() instanceof choose_ball && Greenfoot.mouseClicked(this))
{
    main mainWorld = new main();
    mainWorld.addObject(this, 0, 0); // change location as needed
    Greenfoot.setWorld(mainWorld);
}
Kieranwilko98 Kieranwilko98

2016/11/24

#
Danpost your a living legend, not all heros wear capes
You need to login to post a reply.