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

2013/5/15

"Modificationscreen cannot be cast to terrainone"

1
2
mattjonescalday mattjonescalday

2013/5/15

#
What does this mean and how can I fix it. I am trying to get an variable which changes if a button if clicked. The actor (modificationscreen) is in one world (terrainone) whereas the variable has to be cast in another world.
Gevater_Tod4711 Gevater_Tod4711

2013/5/15

#
You can't cast every object into every class you want. (E.g. you can't cast an Actor to World) You can only case objects into instances of their superclasses or sometimes to their subclasses (E.g. when you call getWorld() you can cast the returned world from World to YourWorldsName). This exception means that you can't cast an instance of the class Moificationscreen so an instance of the class terrainone. But I can't tell you how to fix this without knowing your code. If you just delete the cast to terrainone then you should not get this exception but probably you need to do this. So for another solution you will have to post your code.
davmac davmac

2013/5/15

#
It means you have a value of type 'Modificationscreen', and you are trying to cast it to type 'terrainone', but this doesn't make sense because a Modification cannot be a terrainone. If you are trying to get an actor from the world, you need to use getObjects(...) or similar. If you need more specific help, please post your code.
mezboycalday mezboycalday

2013/5/15

#
sup jonesy lirddddddd
mezboycalday mezboycalday

2013/5/15

#
dont help him! hes trying to do a controlled assesment and by doing ths he is cheating! thanks
mattjonescalday mattjonescalday

2013/5/15

#
I will now put my code in for you all to see. In my last post I said the actor was modificationscreen, I was wrong about this, it is in fact an actor called skisbutton in the world modificationscreen.
//this is the actor in modification screen
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class skisbutton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class skisbutton extends Actor
{
    public int selectionskis=0;
    /**
     * Act - do whatever the tracksbutton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))
        {
           GreenfootImage image = getImage();
            image.scale(image.getWidth()+30,image.getHeight()+30);
            setImage(image);
            terrainone terrainoneWorld = (terrainone) getWorld();
            skisbutton skisbutton = terrainoneWorld.gettheselectionskisValue();
            selectionskis=1;
            getshared();



        }
    }
    public int getshared(){
        return selectionskis;
        
        
        
    }
          
}
//this is the terrainone world where I want the actor skisbutton to cast to



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

/**
 * Write a description of class terrainone here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class terrainone extends World
{
    private Counter theCounter;
    private Counter2 theCounter2;
    private Counter3 theCounter3;
    public tracksbutton  theselectiontracksValue;
    public skisbutton  theselectionskisValue;
    public wheelsbutton theselectionwheelsValue;
    public largebaybutton  thebaysizeselectionlargeValue;
    public mediumbaybutton  thebaysizeselectionmediumValue;
    public smallbaybutton  thebaysizeselectionsmallValue;

    /**
     * Constructor for objects of class terrainone.
     * 
     */
    public terrainone()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 11, 60);        
        setBackground("cell.jpg");
        setPaintOrder(robot.class, person.class, grasspicture.class, grass.class);
        setPaintOrder(robot.class, person.class, rockpicture.class, rock.class);

        locationbuilder();
    }

    private void locationbuilder()
    {   

        

        addObject(new bottombanner(), 0 , 10);
        addObject(new bottombanner(), 1 , 10);
        addObject(new bottombanner(), 2 , 10);
        addObject(new bottombanner(), 3, 10);
        addObject(new bottombanner(), 4 , 10);
        addObject(new bottombanner(), 5 , 10);
        addObject(new bottombanner(), 6 , 10);
        addObject(new bottombanner(), 7, 10);
        addObject(new bottombanner(), 8 , 10);
        addObject(new bottombanner(), 9 , 10);

        addObject(new person(), 2 , 0);
        addObject(new person(), 7, 2);
        addObject(new person(), 9 , 6);
        addObject(new person(), 4 , 6);

        
        theCounter = new Counter();
        theCounter2 = new Counter2();
        theCounter3 = new Counter3();
        addObject(new carryinginfo(), 6 , 10);
        addObject(new powerinfo(), 1 , 10);
        addObject(theCounter, 3, 10);
        addObject(theCounter2, 8, 10);
        addObject(theCounter3, 9, 10);
        //addObject(new robot(),0,0);
        addObject(new basecamp(), 6, 3);

        
        theselectiontracksValue = new tracksbutton();
        theselectionskisValue = new skisbutton();
        theselectionwheelsValue = new wheelsbutton();
        thebaysizeselectionlargeValue = new largebaybutton();
        thebaysizeselectionmediumValue =  new mediumbaybutton();
        thebaysizeselectionsmallValue = new smallbaybutton();

        int selectiontracksValue = ((tracksbutton) getObjects(tracksbutton.class).get(0)).getshared();
        int selectionskisValue = ((skisbutton) getObjects(skisbutton.class).get(0)).getshared();
        int selectionwheelsValue = ((wheelsbutton) getObjects(wheelsbutton.class).get(0)).getshared();
        int baysizeselectionlargeValue = ((largebaybutton) getObjects(largebaybutton.class).get(0)).getshared();
        int baysizeselectionmediumValue = ((mediumbaybutton) getObjects(mediumbaybutton.class).get(0)).getshared();
        int baysizeselectionsmallValue = ((smallbaybutton) getObjects(smallbaybutton.class).get(0)).getshared();

        if (selectiontracksValue>0&&baysizeselectionlargeValue>0){
            addObject(new robottrackslarge(), 6, 3);

        }
        else if (selectiontracksValue>0&&baysizeselectionmediumValue>0){
            addObject(new robottracksmedium(), 6, 3);

        }
        else if (selectiontracksValue>0&&baysizeselectionsmallValue>0){
            addObject(new robottrackssmall(), 6, 3);

        }
        else if (selectionskisValue>0&&baysizeselectionlargeValue>0){
            addObject(new robotskislarge(), 6, 3);

        }
        else if (selectionskisValue>0&&baysizeselectionmediumValue>0){
            addObject(new robotskismedium(), 6, 3);

        }
        else if (selectionskisValue>0&&baysizeselectionsmallValue>0){
            addObject(new robotskissmall(), 6, 3);

        }
        else if (selectionwheelsValue>0&&baysizeselectionlargeValue>0){
            addObject(new robotwheelslarge(), 6, 3);

        }
        else if (selectionwheelsValue>0&&baysizeselectionmediumValue>0){
            addObject(new robotwheelsmedium(), 6, 3);

        }
        else if (selectionwheelsValue>0&&baysizeselectionsmallValue>0){
            addObject(new robotwheelssmall(), 6, 3);

        }

        
    } 

    public Counter getCounter()
    {
        return theCounter;
    }

    public Counter2 getCounter2()
    {
        return theCounter2;
    }

    
    public tracksbutton gettheselectiontracksValue()
    {
        return theselectiontracksValue;
    }
    public skisbutton gettheselectionskisValue()
    {
        return theselectionskisValue;
    }
    public wheelsbutton gettheselectionwheelsValue()
    {
        return theselectionwheelsValue;
    }
    public largebaybutton getthebaysizeselectionlargeValue()
    {
        return thebaysizeselectionlargeValue;
    }
    public mediumbaybutton getthebaysizeselectionmediumValue()
    {
        return thebaysizeselectionmediumValue;
    }
    public smallbaybutton getthebaysizeselectionsmallValue()
    {
        return thebaysizeselectionsmallValue;
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}
Thanks.
mezboycalday mezboycalday

2013/5/15

#
dav mac lad dont help him plz
Gevater_Tod4711 Gevater_Tod4711

2013/5/15

#
In your skisbutton class in line 24 you try to cast the current world (getWorld()) to terrainone. This is only possible if the world is an instance of this class. Are you shure that terraione is the world class you are using when you are calling this method? Or have you got other world classes that could be used in this moment?
mezboycalday mezboycalday

2013/5/15

#
gevater tod4711 lad just leave m8 stop cheating
Gevater_Tod4711 Gevater_Tod4711

2013/5/15

#
@mezboycalday Why shouldn't we help him?
mezboycalday mezboycalday

2013/5/15

#
he is doing a gcse and is not allowed any help
mattjonescalday mattjonescalday

2013/5/15

#
The actor skisbutton is in the world modificationscreen, and I am using it to modify the robot. When it is pressed it changes a variable which I want the other world to detect and as a result select the correct robot with specific values. Here is the code for the world the skisbutton is in....
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Modificationscreen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Modificationscreen extends World
{

    /**
     * Constructor for objects of class Modificationscreen.
     * 
     */
    public Modificationscreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(10, 10, 60);
        addObject(new skisbutton(), 3 , 2);
        addObject(new tracksbutton(), 5 , 2);
        addObject(new wheelsbutton(), 7 , 2);
        addObject(new largebaybutton(), 7 , 3);
        addObject(new mediumbaybutton(), 5 , 3);
        addObject(new smallbaybutton(), 3 , 3);
        addObject(new backbutton(), 8 , 7);

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {

    }
}
mezboycalday mezboycalday

2013/5/15

#
plz dont
mezboycalday mezboycalday

2013/5/15

#
\/
mattjonescalday mattjonescalday

2013/5/15

#
Please ignore mezboycalday this counts as research in the project. He is just annoyed that nobody is looking at his "counter probz" post.
There are more replies on the next page.
1
2