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

2019/6/3

Need help with Shooting Direction of my Actor ( Car fight)

Wellvyre Wellvyre

2019/6/3

#
In the code below you can see my code for the Actor Car that is moving by pressing W (forward) / s (backwards) and turning to the direction of the Mouse. It is shooting by pressing the space button and has a delay wich is great and I even needed 2 days to find that out so my last problem for the start of this schoolproject is the direction it shoots : IT goes directly up no matter where i drive / look:
public class Car extends Actor
{
    private int ShotTimer = 0; 
 public void act()
  {
    if (ShotTimer > 0) {
            ShotTimer = ShotTimer - 1;
        }
        else if (Greenfoot.isKeyDown("space")) {
            getWorld().addObject(new Shot(this), getX(), getY());
            ShotTimer = 200; // delay next shot
        }
      
    MouseInfo mi = Greenfoot.getMouseInfo(); 
    if(mi!=null)
       turnTowards(mi);
    if (Greenfoot.isKeyDown("w"))
       move(2);
    if (Greenfoot.isKeyDown("s"))
       move(-2);
  }
  
 public void turnTowards (int x, int y)
   {
        double dx = x-getX();
        double dy = y-getY();
        double angle = Math.atan2(dy,dx)*180.0/Math.PI;
        setRotation ( (int)angle );
   }
  
 public void turnTowards (MouseInfo mi)
   {
            turnTowards(mi.getX(), mi.getY());
   }
  
}
Super_Hippo Super_Hippo

2019/6/3

#
Show the code of the Shot class. You already pass the Car to the Shot for some reason, so in the constructor of the Shot class, you could set the rotation of the Shot to the Car's rotation. Usually it moves to the right if it isn't changed at all, so I wonder what you have. I am bit lazy to check what the original is, but what did you change in the turnTowards method in relation to the Actor's turnTowards method?
Wellvyre Wellvyre

2019/6/4

#
Well this is my code for the Shot class and I well made the code for the Shot 3 days ago and if im honest im just blind or dumb rn cause I cant see it as well...
public class Shot extends Actor
{
 private Car myCar;
    public Shot (Car myCar)
{
    this.myCar = myCar;
}

    public void act()
 {
      int ypos = getY();
       if (ypos > 0) {
        ypos =ypos - 5;
        setLocation(getX(), ypos);
        Actor Car = getOneIntersectingObject(CPU.class);
        if (Car !=null) {
           
           getWorld().removeObject(Car);
           getWorld().removeObject(this);
        }
    }
    else { 
        getWorld().removeObject(this);
  }
}
}
would be nice if sb could help me out even after im being well not rly helpfull as well at answering as example ur question super_hippo
Super_Hippo Super_Hippo

2019/6/4

#
You set yPos to the current Y-coordinate in the world. Then decrease it by 5 and move the Shot there. So obviously it is moving straight up. You could try this:
public Shot(Car myCar)
{
    this.myCar = myCar;
    setRotation(myCar.getRotation());
}

public void act()
{
    move(5);
    if (isTouching(CPU.class))
    {
        removeTouching(CPU.class);
        getWorld().removeObject(this);
    }
    else if (isAtEdge()) getWorld().removeObject(this);
}
If you don't need the reference to the car for anything, then you could also remove the constructor and use this when adding the Shot:
Shot shot = new Shot();
shot.setRotation(getRotation());
getWorld().addObject(shot, getX(), getY());
Wellvyre Wellvyre

2019/6/4

#
THANK YOU the reference is needed for sth. later on but im glad you helped me there this school project was about to stress me out since i have limited time and i needed that rly fast to keep on going with the rest.
You need to login to post a reply.