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

2014/11/3

error for setting rotation

coder04 coder04

2014/11/3

#
Can you please tell me the problem in this actor
import greenfoot.*; 

public class Miniship extends Actor
{
    private int timer = 0;
    //public static int rotation;
    public void act()
    {
        move(4);
        fire();
        reduceTime();
        randomTurn();
        turnAtEdge();
        //Miniship.rotation = getRotation();
    }
  
  
       public void fire()
    {
        if (timer == 0)
        {
            getWorld().addObject(new Enemylaser() , getX() , getY());
            redlaser.setRotation(getRotation());
            Actor laser = getOneObjectAtOffset(0,0,Redlaser.class);
            timer = 60;
            Greenfoot.playSound("laser.wav");
        }
    }
    
       public void reduceTime()
    {
        if (timer > 0)
            timer--;
    }
    
    /**
     * With a 10% probability, turn a bit right or left.
     */
    public void randomTurn()
    {
        if ( Greenfoot.getRandomNumber(100) < 10 )
        {
            turn( Greenfoot.getRandomNumber(40)-20 );
        }        
    }
    
    /**
     * If we reach the edge of the world, turn a little bit.
     */
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(7);
        }
    }
    public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    } 
} 
danpost danpost

2014/11/3

#
I do not see where the variable 'redlaser' is defined either in the 'fire' method or in the class in general.
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Why are you adding an enemy laser in what seems to be your player class?
getWorld().addObject(new Enemylaser() , getX() , getY());
And if you want to do
redlaser.setRotation(getRotation())
shouldn't you first make a reference to it? So like:
redlaser laser = new redlaser();
laser.setRotation(getRotation());
If you're not making a reference it will most likely give an error on it. Lastly, what are you trying to do with the code:
Actor laser = getOneObjectAtOffset(0,0,Redlaser.class);
? You're making a variable which can be a reference to actor classes, then put redlaser.class in it. However, after that you don't use the actor for anything and I'm trying to get what you would like to accomplish with a reference to a laser you just fired. I mean: if it's fired it's fired right? It's not like you would want to do anything about the lasers direction after firing it... Help us out here man :)
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Maybe if you're trying to create a laser with the direction of the ship it would look more like this:
redlaser laser = new redlaser();
getWorld().addObject(laser, getX(), getY());
laser.setRotation(getRotation());
coder04 coder04

2014/11/3

#
thanx ive fixed the problem
You need to login to post a reply.