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

2015/4/9

Firing projectile

jmstinson221 jmstinson221

2015/4/9

#
I am trying to get the character to fire a projectile at the coordinates of the mouse when the mouse is clicked. Currently, the projectile will fire and move at the right speed (using move() instead of a vector) but the direction stays 0. My math for firing from the Character.class:
1
2
3
4
5
6
7
8
9
10
if(charclass == 2)
         
        {
            MouseInfo info = Greenfoot.getMouseInfo();
            double dxcalc = (info.getX()-getX());
            double dycalc = (info.getY()-getY());
            double dir = Math.atan2(dycalc,dxcalc);
             
            getWorld().addObject(new Projectile(dir, true), getX(), getY());
        }
And the constructor for Projectile.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Projectile(double direction, boolean fromCharacter)
    {
         
        super(new Vector (direction, Character.Arrowspeed));
        if(fromCharacter)
        {
            power = Character.actualdamage;
            setImage(arrow);
            arrow.rotate(45);
            setRotation((int) direction);
            arrow.scale(25,25);
        }
         
    }
davmac davmac

2015/4/9

#
Where does your Vector class come from? Does the constructor require an angle in degrees? The angle you are calculating (using Math.atan2) is in radians.
jmstinson221 jmstinson221

2015/4/9

#
Curse the time elapsed since my last calculus class, thank you for pointing out the radians problem. I am using the default imported vector class, although I may just remove it altogether and use setRotation and move() so it would look more like
1
2
3
4
5
6
7
8
9
10
if(charclass == 2)
          
        {
            MouseInfo info = Greenfoot.getMouseInfo();
            double dxcalc = (info.getX()-getX());
            double dycalc = (info.getY()-getY());
            double dir = Math.toDegrees(Math.atan2(dycalc,dxcalc));
              
            getWorld().addObject(new Projectile(dir, Character.actualdamage, true), getX(), getY());
        }
and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  public Projectile(double direction, double power, boolean fromCharacter)
     
    {
        if(!fromCharacter)
        {
            setImage(spear);
            spear.rotate(57);
            setRotation((int)direction);
            spear.scale(40,40);
        }       
        if(fromCharacter)
        {
            setImage(arrow);
            arrow.rotate(45);
            setRotation((int)direction);
            arrow.scale(25,25);
        }       
    }
     
    public void act()
    {
       if(fromCharacter == false)
       {
           if(getOneIntersectingObject(Character.class) != null)
           {
               getWorld().removeObject(this);
            }
           move(2);
        }
       if(fromCharacter)
       {
           if(getOneIntersectingObject(Enemy.class) != null)
           {
               getWorld().removeObject(this);
            }
           move(Character.Arrowspeed);
       }
        
}
jmstinson221 jmstinson221

2015/4/9

#
But now when I shoot an arrow it is placed over the character and then instantly removed. Where did I mess up the conditions for removal?
davmac davmac

2015/4/9

#
You're not saving the 'fromCharacter' parameter value (in the Projectile constructor) into the 'fromCharacter' instance variable. You need a line something like:
1
this.fromCharacter = fromCharacter;
You need to login to post a reply.