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

2014/2/15

How do I add an object where the mouse is?

Equinio Equinio

2014/2/15

#
I am making a bullet-hell-ish type game and I can't really figure out how to add an object where the mouse is. The only thing I know how to do is add a bullet from the character and tell it which direction to go in, but i need the bullet to move to where the mouse is.
danpost danpost

2014/2/15

#
Equinio wrote...
The only thing I know how to do is add a bullet from the character and tell it which direction to go in ....
How is this any different?
Equinio Equinio

2014/2/15

#
Sorry, I should have worded that better. The only thing I can do is make the bullet fire in one direction when you click. I need it to go where the mouse is on the screen, not just up. I'm sorry if im ignorant, I've only been learning for a couple of weeks in my spare time.
Nitrouspiggy Nitrouspiggy

2014/2/15

#
At the very start of your bullet class (assuming you have no constructor already), write:
public bullet()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if(mouse != null) 
    {
        double dx = mouse.getX() - getX();
        double dy = mouse.getY() - getY();
        double angle = Math.atan2(dy,dx)*180.0/Math.PI;
        setRotation( (int)angle );
    }
}
This will make it turn towards the mouse when the object is added to the world. If you already have a constructor for the bullet, just add the code from inside that method into it.
Nitrouspiggy Nitrouspiggy

2014/2/15

#
Sorry, I've read your post again and it seems my answer might not be that relevant. Do you want the object to be added where the mouse is or turn to face the mouse?
Equinio Equinio

2014/2/15

#
I want the projectile to start at the character and go in the direction of the cursor. Here is a horrible MS paint visual aid: http://i.imgur.com/lumT65u.png The bullet will be removed at the edge of the screen unless it hits an enemy, if that makes a difference. I can write that part, though. Thanks for the help!
danpost danpost

2014/2/15

#
Get the location of the mouse using MouseInfo class methods and use the Actor class method 'turnTowards' to have the bullet face the proper direction when creating the bullet and adding it to the world. Make sure you use the Actor class 'move' method in the Bullet class to make the bullet move.
Equinio Equinio

2014/2/15

#
Thanks, danpost. This might sound dumb but I understand what i need to do, I just don't know what to type specifically.
danpost danpost

2014/2/15

#
What code do you have now, as far as detecting mouse click and adding the bullet into the world?
Equinio Equinio

2014/2/15

#
I have this in the world sub-class so it is constantly being checked:
    if (Greenfoot.mouseClicked(null))
     {
          MouseInfo mouseInfo = Greenfoot.getMouseInfo();
          int x = mouseInfo.getX();
          int y = mouseInfo.getY();
          addObject(new FireBullet(), 100, 100);
     }
    }
and then
    public void act() 
    {
        setLocation(getX(),getY()-5);
        removeMissile();
    }
    public void removeMissile()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }
    public boolean atWorldEdge()
    {
        if(getX() < 2 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 2 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
in the missile class. The setLocation(getX(),getY()-5); is just there until I figure out what I need to do.
danpost danpost

2014/2/15

#
Ok, in order to make reference to the new FireBullet object that is created, we need to change the code to this.
if (Greenfoot.mouseClicked(null))
{
    MouseInfo mouseInfo = Greenfoot.getMouseInfo();
    int x = mouseInfo.getX();
    int y = mouseInfo.getY();
    Actor bullet = new FireBullet(); // this gets a ref to the object created
    addObject(bullet, 100, 100); // this adds the ref'ed object into world
    bullet.turnTowards(x, y); // this makes it face the mouse
}
// then in the act of the missile class
public void act()
{
    move(5);
    removeMissile();
}
Equinio Equinio

2014/2/15

#
Awesome! I cannot thank you enough!
You need to login to post a reply.