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

2016/5/7

Not properly shooting towards Mouse

TheBigBossJoshi TheBigBossJoshi

2016/5/7

#
Hi, I am working on a duck hunt type game, which use the mouse as the cursor and shoots towards it whenever the mouse is pressed. Unfortunately, the bullets never shoot the right way. Here is my Cursor Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cursor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cursor extends Level2Objects
{
    MouseInfo mi;
    HeliBullet b; 
    GreenfootImage img;
    
    public Cursor()
    {
        img = new GreenfootImage("cursor.png");
        img.scale(50, 50);
        setImage(img);
    }
    
    public void act() 
    {
       mi = Greenfoot.getMouseInfo();
       
       if(mi != null)
       {
           int x = mi.getX();
           int y = mi.getY();
       
           setLocation(x, y);
           
           Level2 theWorld = (Level2)getWorld();
           Player2nd player = (Player2nd)theWorld.getPlayer();  
           
           if(Greenfoot.mouseClicked(null))
           {
               if(player.getDirection())
               {
                   b = new HeliBullet();
                   b.turnTowards(getX(), getY());
                   getWorld().addObject(b, 420, 275);
               }
               else
               {
                   b = new LeftHeliBullet();
                   b.turnTowards(getX(), getY());
                   getWorld().addObject(b, 150, 275);
               }
           }
       }

    }    
    
    
}
What is going wrong?
danpost danpost

2016/5/7

#
I would be surprised if your bullets shot at all. It looks like you would get an IllegalStateException error before a bullet was added into the world. If you are not getting that error, maybe you should be. Lines 41 and 42 along with lines 47 and 48 should both be switched. If you are not getting the error, then the bullets are turning with respect to the wrong bullet locations (probably all turning to some angle between across to the right and down).
TheBigBossJoshi TheBigBossJoshi

2016/5/7

#
Ah, I see my error. I corrected it, and it works perfectly. Thanks!
You need to login to post a reply.