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

2012/2/5

(New to Java and Greenfoot) Error: cannot find symbol - method setRotation(int)

fisch3r fisch3r

2012/2/5

#
Hi there! I'm new to Greenfoot and Java in general, but i'd love to learn more about programming. I think this question has probably been asked before (maybe not in this exact way,), but this seems like an awesome forum for e beginner like me. You guys seem pretty helpful. :) My problem is this: I'm writing a Bomberman clone at school, and i have a problem with my main Actor. The idea is that he's supposed to look up when going up, down when going down, and left when going left. Nothing is supposed to happen when he goes right, as the "sprite" (or image) is already turned right. I first tried to just rotate him (with setRotation in a method (it's a method, and not a function, right?)), but then he walks upside down when going left (as the rotation is set to 180 degrees here). I've then gotten this far:
    GreenfootImage jacobRight;
    GreenfootImage jacobLeft;
    GreenfootImage jacobUp;
    GreenfootImage jacobDown;
    
    public Bomberman()
    {
        jacobRight=getImage();
        jacobLeft=new GreenfootImage(getImage());
        jacobLeft.mirrorHorizontally();
        jacobUp=new GreenfootImage(getImage());
        jacobUp.setRotation(-90);
        jacobDown=new GreenfootImage(getImage());
        jacobDown.setRotation(90);
    }
which in my head should give the following result: Do nothing when going right, mirror the actor when going left, and rotate him +/- 90 degrees when going up and down. I have a setDirection() method which you can see here. It is called whenever one of the arrow keys are pressed.
    
    public void setDirection()
    {
        if (direction==UP)
        {
            setImage(jacobUp);
        }
        
        else if (direction==DOWN)
        {
            setImage(jacobDown);
        }
        
        else if (direction==LEFT)
        {
            setImage(jacobLeft);
        }
        
        else if (direction==RIGHT)
        {
            setImage(jacobRight);
        }
    }
Can anyone help me get an idea of what's wrong? I thought I could do the rotation like this, just like I could with mirrorHorizontally().
fisch3r fisch3r

2012/2/5

#
Here's the full source if that's of any help:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Bomberman extends Actor
{
    int UP=0;
    int DOWN=1;
    int LEFT=2;
    int RIGHT=3;
    
    int direction;
    
    GreenfootImage jacobRight;
    GreenfootImage jacobLeft;
    GreenfootImage jacobUp;
    GreenfootImage jacobDown;
    
    public Bomberman()
    {
        jacobRight=getImage();
        jacobLeft=new GreenfootImage(getImage());
        jacobLeft.mirrorHorizontally();
        jacobUp=new GreenfootImage(getImage());
        jacobUp.setRotation(-90);
        jacobDown=new GreenfootImage(getImage());
        jacobDown.setRotation(90);
    }

    public void act() 
    {
        move();
    }
    
    public void move()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() -1);
            direction=UP;
            setDirection();
        }
        
        else if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() +1);
            direction=DOWN;
            setDirection();
        }
        
        else if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() -1, getY());
            direction=LEFT;
            setDirection();
        }
        
        else if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() +1, getY());
            direction=RIGHT;
            setDirection();
        }
    }
    
    public void setDirection()
    {
        if (direction==UP)
        {
            setImage(jacobUp);
        }
        
        else if (direction==DOWN)
        {
            setImage(jacobDown);
        }
        
        else if (direction==LEFT)
        {
            setImage(jacobLeft);
        }
        
        else if (direction==RIGHT)
        {
            setImage(jacobRight);
        }
    }
}
danpost danpost

2012/2/5

#
When setting the image for each direction, you need to use setRotation(0) for RIGHT (which is default), setRotation(90) for DOWN, setRotation(180) for LEFT, and setRotation(270) for UP. Then use the mirrorHorizontally and mirrorVertically to correct the image for LEFT and, if you want, UP. This is because the rotation determines the direction for the move statement.
danpost danpost

2012/2/5

#
Another thing, do not use getImage(); instead, use the actual name of the image file. Then all that needs done is setRotation(90 * direction); and if (direction == LEFT) img.mirrorVertically();.
danpost danpost

2012/2/5

#
I simplified the code. Unless you have a need to save instance variables (which you do not, at least, as yet, here), there is really no need to store them. My simplified code here reduced your 86 lines to 40 lines, and does do what you need it to do.
import greenfoot.*;

public class Bomberman extends Actor
{
    public Bomberman()
    {
    }

    public void act() 
    {
        move();
    }
    
    public void move()
    {
        if (Greenfoot.isKeyDown("up")) 
        {
            setRotation(270);
            move(1);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(1);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move(1);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(1);
        }
        GreenfootImage img = new GreenfootImage("bomberman.png");
        if (getRotation() == 180) img.mirrorVertically();
        setImage(img);
    }
}
Just adjust the name of the image file in line 36.
fisch3r fisch3r

2012/2/5

#
Thanks a lot! That really helped!
You need to login to post a reply.