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

2019/11/19

Turn Object to the direction of the mouse

SemDeBakkero SemDeBakkero

2019/11/19

#
Hi, in my game is a actor which I want to let it turn to the direction of the mouse. The code that I used kind of worked out. It follows the mouse, but the side of actor is following the mouse en not the top. I've already tried to turn the actor. But tht didn't work. What should I do? This is my code:
public class Speler extends Actor
{
    int speed = 10; //snelheid
    
    public Speler()
     {
        setImage("mainship.png"); //invoegen foto
        getImage().scale(80,80); //grootte foto
    }
    
    public void act() 
    {
        moveAround(); //de public moveAround laten afspelen
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            int x = mouse.getX(), y = mouse.getY();
            turnTowards(x, y);
        }
    }    
    public void moveAround()
    {
        if(Greenfoot.isKeyDown("down")) //als pijltje omlaag dan actor omlaag
         {
            setLocation(getX(),getY()+ speed);  
        }
        if(Greenfoot.isKeyDown("up")) //als pijltje omhoog dan actor omhoog
        {
            setLocation(getX(),getY()- speed);
        }
        if(Greenfoot.isKeyDown("left")) //als pijltje naar links actor naar links
        {
            setLocation(getX() - speed,getY());
        }
        if(Greenfoot.isKeyDown("right")) //als pijltje naar rechts actor naar rechts
        {
            setLocation(getX() + speed,getY());
        }
    }
}
Super_Hippo Super_Hippo

2019/11/20

#
Maybe you can simply use a ”turn(90)” or “turn(-90)” right after line 18 to turn the actor by 90 degrees. As an alternative, you could turn the image which you use for the actor.
SemDeBakkero SemDeBakkero

2019/11/20

#
Thanks this worked!
SemDeBakkero SemDeBakkero

2019/11/20

#
Hi, I'd like to turn the bullets this actor shoots to the direction of the mouse. So that if I click somewhere on the screen the bullet goes to that point and the bullet turns to that place. This is my code of the actor wich is shooting now:
public class Speler extends Actor
{
    int speed = 10; //snelheid
    
    public Speler()
     {
        setImage("mainship.png"); //invoegen foto
        getImage().scale(80,80); //grootte foto
    }
    
    public void act() 
    {
        moveAround(); //de public moveAround laten afspelen
        Fire(); //de public Fire uitvoeren
        MouseInfo mouse = Greenfoot.getMouseInfo(); //muis informatie ophalen
        if (mouse != null)
        {
            int x = mouse.getX(), y = mouse.getY(); //variable x en y instellen op de x en y van de muis
            turnTowards(x, y); //object laten draaien richten de muis
        } 
        
    }    
    public void moveAround()
    {
        if(Greenfoot.isKeyDown("down")) //als pijltje omlaag dan actor omlaag
         {
            setLocation(getX(),getY()+ speed);  //dan naar beneden bewegen
        }
        if(Greenfoot.isKeyDown("up")) //als pijltje omhoog dan actor omhoog
        {
            setLocation(getX(),getY()- speed); // dan naar boven bewegen
        }
        if(Greenfoot.isKeyDown("left")) //als pijltje naar links actor naar links
        {
            setLocation(getX() - speed,getY()); //dan naar links bewegen
        }
        if(Greenfoot.isKeyDown("right")) //als pijltje naar rechts actor naar rechts
        {
            setLocation(getX() + speed,getY()); //dan naar rechts bewegen
        }
    }
    public void Fire()
    {
      if(Greenfoot.mousePressed(null))  //als de muis geklikt wordt
      {
          shoot(); //dan shoot uitvoeren
      }
    }
    public void shoot()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse == null) return;
        int x = mouse.getX(); //variable x instellen op de x van de muis
        int y = mouse.getY(); //variable y instellen op de y van de muis
        bullet bullet = new bullet(); //bullet betekent nu nieuw bullet
        getWorld().addObject(bullet, getX(), getY()); //bullet toevoegen aan de wereld
        bullet.turnTowards(x, y); //bullet draaien richting de muis
    }
}
And this is the one of my bullet:
public class bullet extends Actor
{
   public bullet()
   {
       setImage("bullet.png"); //invoegen foto
        getImage().scale(35,35); //grootte foto
              
   }
   public void act() 
    {
     move(20);   //bewegen met snelheid 10
     kill(); //de public "kill" uitvoeren
     ifAtWorldEdge(); //de public "ifAtWorldEdgde" uitvoeren
     
   }
   public void kill()
    {
        Actor Enemy; //het gaat over de actor Enemy
        Enemy = getOneObjectAtOffset(0, 0, Enemy.class); 
        if (Enemy !=null)
        {
            World world;
            world = getWorld();
            world.removeObject(Enemy); //de enemy laten verdwijnen
        }
   }
   public void ifAtWorldEdge()
    {
        if (isAtEdge()) //als dit object aan de buitekant is
        {
            getWorld().removeObject(this);  //verwijder dan de bullet
        } 

    }
}
What should I change??
Super_Hippo Super_Hippo

2019/11/20

#
What is happening? I guess the 20 movement speed is too high and the bullet will move through the enemy most of the time.
SemDeBakkero SemDeBakkero

2019/11/20

#
Super_Hippo wrote...
What is happening? I guess the 20 movement speed is too high and the bullet will move through the enemy most of the time.
No that's not the problem. It will kill the opponents. But the bullet itself. How can I turn that to the direction of the mouse in the addObject command??
danpost danpost

2019/11/21

#
SemDeBakkero wrote...
How can I turn that to the direction of the mouse in the addObject command??
The addObject command has no codes in it dealing with the rotation of the actor. You simply cannot use it to set the actor's rotation. What you currently have coded in your shoot method is about the best possible way to have the bullet initialized to facing the mouse.
SemDeBakkero SemDeBakkero

2019/11/21

#
danpost wrote...
SemDeBakkero wrote...
How can I turn that to the direction of the mouse in the addObject command??
The addObject command has no codes in it dealing with the rotation of the actor. You simply cannot use it to set the actor's rotation. What you currently have coded in your shoot method is about the best possible way to have the bullet initialized to facing the mouse.
That's unfortunate, thanks for helping tho!
Super_Hippo Super_Hippo

2019/11/21

#
Maybe the image is only turned by 90 degrees again?
danpost danpost

2019/11/21

#
Just to be clear, an actor with zero rotation is considered to be facing (or turned towards) the right. If the image given the actor is not one that naturally "faces" right, then you will need to do something extra to make it appear properly. Let us say that you have a rocket image which "faces" up as it would when in a landed orientation. You could set it as the default image and change its orientation in the constructor of the class:
public Rocket extends Actor
{
    public Rocket()
    {
        GreenfootImage wrong = getImage();
        int w = wrong.getWidth(), h = wrong.getHeight();
        GreenfootImage correct = new GreenfootImage(h, w);
        for (int j=0; j<h; j++) for (int i=0; i<w; i++)
        {
            correct.setColorAt(h-1-j, i, wrong.getColorAt(i, j));
        }
        setImage(correct)
    |
}
SemDeBakkero SemDeBakkero

2019/11/21

#
danpost wrote...
Just to be clear, an actor with zero rotation is considered to be facing (or turned towards) the right. If the image given the actor is not one that naturally "faces" right, then you will need to do something extra to make it appear properly. Let us say that you have a rocket image which "faces" up as it would when in a landed orientation. You could set it as the default image and change its orientation in the constructor of the class:
public Rocket extends Actor
{
    public Rocket()
    {
        GreenfootImage wrong = getImage();
        int w = wrong.getWidth(), h = wrong.getHeight();
        GreenfootImage correct = new GreenfootImage(h, w);
        for (int j=0; j<h; j++) for (int i=0; i<w; i++)
        {
            correct.setColorAt(h-1-j, i, wrong.getColorAt(i, j));
        }
        setImage(correct)
    |
}
Ohhh I understand, thanks!
You need to login to post a reply.