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

2021/3/16

Problem with GreenfootImage

cl0q cl0q

2021/3/16

#
So basically I just wanted to have the rocket in my game rotated without having to edit the picture since that gives me white borders again, however, I always get the error "non-static method rotate(int) cannot be referenced from a static context. I looked it up online and tried to create an instance from the GreenfootImage class, but it's set to private. So what can I do? The method in question is prep().
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * @T. B., Klasse 10
 * Das ist die Raketen-Klasse, in dieser wird bestimmt, was die Raketen-Objekte machen sollen...
 */

public class Rakete extends Actor
{
    private int leben;
    
    public Rakete(){
        leben=3;
        
    }
    
    public void prep()
    {
        GreenfootImage.rotate(90);
    }
    public void act(){
        prep();
        if(Greenfoot.isKeyDown("right")){
            move(3);
        }
        if(Greenfoot.isKeyDown("left")){
            move(-3);
        }
        if(Greenfoot.isKeyDown("up")){
            setLocation( getX() , getY()-3);
        }
        if(Greenfoot.isKeyDown("down")){
            setLocation( getX() , getY()+3);
        }

        World w=getWorld();
        w.showText("Anzahl Leben: "+leben,   w.getWidth()/2, w.getHeight()-20);

    }

    public void lebenEingesammelt(){
        leben++;
    }

    public void gestorben(){
        leben--;
    }
}
rocket770 rocket770

2021/3/16

#
Replace line 19 with getImage().rotate(45);
cl0q cl0q

2021/3/16

#
rocket770 wrote...
Replace line 19 with getImage().rotate(45);
Well, the code works now, however, the rocket spins endlessly now, how can I make it set to 90deg on start and stay like that? Edit: i got it by using setRotation(). I really make my life too complicated smh...
danpost danpost

2021/3/16

#
Remove lines 17 thru 20 and line 22. Then put the following at line 14:
setRotation(90);
Finally, swap the usage in act of all move and setLocation lines (or use setLocation throughout).
You need to login to post a reply.