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

2011/11/4

Mouse info?

darkmist255 darkmist255

2011/11/4

#
I just created a simple gravity/momentum function, and just use a ball to test it. I would like to be able to click anywhere to move the ball to where I clicked, but I'm having trouble. It's getting the mouse info(coordinates and click status). I know about this part of the API (http://www.greenfoot.org/files/javadoc/greenfoot/MouseInfo.html) but don't know how to use any of it. Do I need to create public class MouseInfo to get the information? If I do, I think I would then need to store the information in a public variable, which is still a matter I don't understand much. Any help is greatly appreciated :D! Especially if I can figure out public variables!
darkmist255 darkmist255

2011/11/4

#
Found out how to use the mouse: public void checkMouse() { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse!=null){ button = mouse.getButton(); if(button == 1) { mouseX=mouse.getX(); mouseY=mouse.getY(); setLocation(mouseX, mouseY); momentum = 0; } } } But still don't know public variables :D. I am thinking of making it just add a new ball instead of moving the current one, but I can't seem to get it to add the ball (I could add objects other times o.O). I made a new class called CheckMouse: ______________________________________________________ import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class CheckMouse { int button, mouseY, mouseX = 0; public void Act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse!=null){ button = mouse.getButton(); if(button == 1) { mouseX=mouse.getX(); mouseY=mouse.getY(); Ball ball = new Ball(); getWorld().addObject(ball, mouseX, mouseY); } } } _________________________________________ But it tells me "Cannot find symbol, method getWorld()" I know that getWorld() only works when it is used within the actor's class, but I want to reference to the world from a class not in the world. Any advice is greatly appreciated PS: Haha! This is my third day of working on Java and I already know 9 billion times as much as I did before :D. Way much more left to learn :D.
Frederik Frederik

2011/11/4

#
You could better add the mouse code in the ball class, you don't need to make a class for it. Just add it in the act() loop of your ball, see the code bellow:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Ball
{
    int button, mouseY, mouseX = 0;
    
    public void Act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse!=null){
            button = mouse.getButton();
            if(button == 1 && Greenfoot.mouseClicked(null)) //Add this else it will be transferred to your mouse all the time, now just when you click.
            {
            mouseX=mouse.getX();
            mouseY=mouse.getY();
            setLocation(mouseX, mouseY);
            momentum = 0;
            }
    }
}
Hope I helped you :) P.S. Sorry for bad english ..
delmar delmar

2011/11/4

#
It's better to turn this around: check for the mouse click first, and then get the mouse info only if there was a click. Something like this: if (Greenfoot.mouseClicked(null)) { // null means click on any object MouseInfo mouse = Greenfoot.getMouseInfo(); ... // check mouse button and get x/y here and set the location } If you write this code inside your ball (or any other Actor subclass) the getWorld() method will work. You can then either set the current ball to the x,y coordinates: setLocation(x, y); or create a new ball at that location: getWorld().addObject( new Ball(), x, y); As far as I can see (unless I am overlooking something) I don't think you need a public variable.
darkmist255 darkmist255

2011/11/5

#
Thanks for all the help! I put a new public void called CheckMouse in ball.class and called it in Act(), and it worked perfectly . I also am making a limit to the ball count so that it doesn't lag. For some odd reason though, this isn't having the effect I thought it would. if(ballCount < 6) { getWorld().addObject(new Ball(), mouseX, mouseY); ballCount = (ballCount + 1); }
You need to login to post a reply.