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

2013/4/19

How to detect collision with mouse cursor, or move items accordingly to mouse position?

RadikulRAM RadikulRAM

2013/4/19

#
I want to create a bullet hell type of game, and would like to use the tip (final pixel) of my cursor as the player, which moves along with the rest of the mouse. In the asteroids tutorial, we make a class for the rocket ship, in that the collision part looks like this: /** * Check whether we are colliding with an asteroid. */ private void checkCollision() { Actor a = getOneIntersectingObject(Asteroid.class); if (a != null) { Space space = (Space) getWorld(); space.addObject(new Explosion(), getX(), getY()); space.removeObject(this); space.gameOver(); } } I think the piece I need to pay attention too is “getOneIntersectingObject”. This method is described as “Return an object that intersects this object. This takes the graphical extent of objects into consideration.”. My problem is that my cursor is not such a graphic. Would it be possible for me to create a single pixel graphic, and permanently attach it to the end of my cursor? Or better yet would it be possible for me to create an object, such as a spaceship, which I can move around as I move the mouse, without requiring to hold the mouse button down over the object? It seems all of the mouseinfo functions require clicking/holding down to move. If you have played the game Frantic, a flash game, that is exactly what I want. Thank you -RAM P.S: If there is a tutorial I can use to create a game like Frantic, please recommend it.
davmac davmac

2013/4/19

#
It seems all of the mouseinfo functions require clicking/holding down to move.
I'm not sure how you got this idea; see the documentation for Greenfoot.mouseMoved(...).
RadikulRAM RadikulRAM

2013/4/19

#
Yeah, I've got no idea how I completely missed that, I'll try to work with it. Thanks.
RadikulRAM RadikulRAM

2013/4/19

#
Ok I can't achieve this due to my lack of knowledge. My idea is something like this: if mouseMoved on Rocket Rocket pos x = mouse pos; Rocket pos y = mouse pos; How will I achieve something like this? Since I have very little understanding of the syntax of Java, I can't figure out for myself unfortunately. Thanks
davmac davmac

2013/4/19

#
In the Rocket class:
if (Greenfoot.mouseMoved(this)) {
    MouseInfo info = Greenfoot.getMouseInfo();
    setLocation(info.getX(), info.getY());
}
However, do you really need the mouse to have been moved on the rocket? Wouldn't you rather that the rocket always follows the mouse wherever it moves? In that case change the first line to:
if (Greenfoot.mouseMoved(null)) {
RadikulRAM RadikulRAM

2013/4/24

#
    /**
     * 
     */
    public void noClickInterruption()
    {
        if (Greenfoot.mouseClicked(this)) {
            MouseInfo info = Greenfoot.getMouseInfo(); 
            setLocation(info.getX(), info.getY());     
        }
    }
Thanks for your code Dav! I'll try to figure out how to hide the cursor myself, but my current problem is that when I do any mouse click the player stops following the mouse. I need my player model to follow the mouse at all times, even when the mouse button is held down but again I'm clueless on this matter. I tried doing: /** * */ public void noClickInterruption() { if (Greenfoot.mouseClicked(this)) { MouseInfo info = Greenfoot.getMouseInfo(); setLocation(info.getX(), info.getY()); } } I tried mouseClicked and mousePressed, but couldn't get neither one to work. I'm sure it's an easy solution that I am blind too.
You need to login to post a reply.