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

2012/5/14

Constructor

Matt Matt

2012/5/14

#
    public YellowCar() //Constructor
    {    
        if( YellowCar = 1);
        setRotation(90); //set rotation to north
        
        if( else YellowCar = 2);
        setRotation(0); 
    }
}

I am trying to create a constructor so I can put actors in more than one direction, I am getting an error message saying that their is an illegal start to the expression, what am I doing wrong
SPower SPower

2012/5/14

#
do something like this:
public YellowCar(int direction)
{
    if (direction == 0) setRotation(90);
}
Your first wrong thing is this:
if( YellowCar = 1);
If you compare things, you do == instead of =, because = sets the value, == compares it. This is also wrong:
if( else YellowCar = 2); 
for else, write this:
else if (condition) {
    //statements;
}
And the last thing: YellowCar IS NO INTEGER, so you can't give it an integer value like 1 or 2. That's why I put a parameter in the constructor. You don't have to set the rotation to 0, because that always starts at 0 :) Do you understand this?
SPower SPower

2012/5/15

#
@carter4 You're just putting ads on greenfoot.org, aren't you?
davmac davmac

2012/5/15

#
I've deleted carter4; seemed to be a spambot.
Matt Matt

2012/5/18

#
Oh ok, I do understand does that mean I should write something in the world like
addObject(new YellowCar(0),30,15);
Matt Matt

2012/5/18

#
I tried it and it worked. Thanks for all your help
You need to login to post a reply.