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

2015/1/17

Setting direction to the mouse

BrownBoii333 BrownBoii333

2015/1/17

#
So I'm making a snowball fight game and i want the character to throw a snowball when the mouse is clicked. I want the snowball to add itself where the character is and then go in the direction of the mouse. Also i'd like the snowball to go the distance of the snowball (add force according to where the mouse click occurs) and have a max as to how far the mouse can move from the character. how can i do this (sorry i realize that this is a lot). here is my code (i use the smooth mover and vector class from greenfoot) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class Snowball here. * * @author (your name) * @version (a version number or a date) */ public class Snowball extends SmoothMover { private static final Vector GRAVITY = new Vector(90 , 0.2); private double velocity; int x=0; public Snowball() { getImage().scale(13,13); int angle = Greenfoot.getRandomNumber(41) + 250; double force = Greenfoot.getRandomNumber(1000)/100.0 + 5; addForce( new Vector( angle , force ) ); } public void act() { move(); addForce(GRAVITY); turn(10); if ( atWorldEdges()||collision() ) { explosion(); getWorld().removeObject(this); } //if ( atPeak() ) //{ // explosion(); // } } public boolean collision(){ Actor wall = (Actor)getOneIntersectingObject(OG_Obstacle.class); Actor p1 = (Actor)getOneIntersectingObject(Fighter.class); Actor p2 = (Actor)getOneIntersectingObject(Fighter2.class); if(wall!=null){ return true; } if(p1!=null){ return true; } if(p2!=null){ return true; } return false; } public boolean atWorldEdges() { if ( getExactX() < 5 ) { return true; } if ( getExactY() < 5 ) { return true; } if ( getExactX() > getWorld().getWidth() - 5 ) { return true; } if ( getExactY() > getWorld().getHeight() - 5 ) { return true; } return false; } public boolean atPeak() { int angle = getMovement().getDirection(); return angle > 0 && angle < 180; } public void explosion() { for ( int i = 0; i < 50; i++ ) { getWorld().addObject( new Debris(), getX(), getY() ); } } }
-nic- -nic-

2015/1/17

#
In the character class to add the snowball:
if (c > 0) {c--;}
        if (mouseDown && Greenfoot.mouseClicked(null)) {mouseDown = false;}
        if (!mouseDown && Greenfoot.mousePressed(null)) {mouseDown = true;}
        if (mouseDown)
        {
        Snowball snowball = new Snowball(); //create a new snowball
snowball.setRotation(getRotation()); //set its rotation to the characters rotation
getWorld.addObject(snowball, getX(),getY()); //adds the snowball to the world where the character is
-nic- -nic-

2015/1/17

#
Not complete let me fix that
MouseInfo mI = Greenfoot.getMouseInfo(); //get mouse info

 if (mouseDown && Greenfoot.mouseClicked(null)) {mouseDown = false;}
        if (!mouseDown && Greenfoot.mousePressed(null)) {mouseDown = true;} check if the mouse is down/ pressed
        if (mouseDown)
        {
        Snowball snowball = new Snowball(); //create a new snowball
        snowball.turnTowards(mI.getX(),mI.getY());  //set its rotation to the mouse
        getWorld.addObject(snowball, getX(),getY()); //adds the snowball to the world where the character is
To change the force the snowball is thrown you can get the distance of the mouse to the character by using Pythagoras:
//First get the x and y distances from the moues to the character by: (in the character class)

double xDistance = mI.getX()-getX();
double yDistance = getY()-mi.getY();

//then use Pythagoras to get the direct distance:

int distance = (int)Math.sqrt(xDistance^2 + yDistance^2);//(int) converts the decimal that will get made into a normal int

//you can then use distance to set the snowball's FORCE:

snowball.FORCE = distance;
You can then limit the force that it can be thrown;
if(distance> 100)
{
     distance  = 100;
}
BrownBoii333 BrownBoii333

2015/1/18

#
I get and error for int distance = (int)Math.sqrt(xDistance^2+yDistance^2); it says i have a bad operand types for binary operate '^' also what do i set mouseDown to or is it a default with greenfoot. and in the adding snowballs part, are the other if statements within the first one?
-nic- -nic-

2015/1/18

#
Sorry I should have tested the code here is a working version:
boolean forward;
    boolean mouseDown;
    public void act() 
    {
        MouseInfo mI = Greenfoot.getMouseInfo(); //get mouse info
 
        if (mouseDown && Greenfoot.mouseClicked(null)) {mouseDown = false;}
        if (!mouseDown && Greenfoot.mousePressed(null)) {mouseDown = true;}
        if (mouseDown)
        {
            Snowball snowball = new Snowball(); //create a new snowball
            getWorld().addObject(snowball, getX(),getY()); //adds the snowball to the world where the character is
            snowball.turnTowards(mI.getX(),mI.getY());
            
            double xDistance = mI.getX()-getX();
            double yDistance = getY()-mI.getY();
            int distance = (int)Math.sqrt(xDistance*yDistance + yDistance * yDistance);
            snowball.FORCE = distance;
        }    
    }
Super_Hippo Super_Hippo

2015/1/18

#
Line 17 now is xy + y^2 now, it probably should be x^2+y^2. By the way, instead of a^b, you can write 'Math.pow(a,b)'.
-nic- -nic-

2015/1/18

#
Thanks super_hippo
BrownBoii333 BrownBoii333

2015/1/19

#
i get an error that it can't find FORCE (line 18).
-nic- -nic-

2015/1/19

#
Oh, force is a variable in the snowball class you can then use to change how powerful the snowball is thrown, so you can either change line 18 of the actor or make a variable in snowball called FORCE and use that when calculating the power its thrown with
BrownBoii333 BrownBoii333

2015/1/20

#
Ik that but I mean why can't it find the variable? also it says that mouseDown may not be initialized. what does that mean and how can I fix it?
BrownBoii333 BrownBoii333

2015/1/20

#
Also do I add this code in the character class or in the actual snowball class?
Super_Hippo Super_Hippo

2015/1/20

#
You create the double 'force' in the constructor. This value is lost once the method is left. So it isn't saved after that and can't be found.
You need to login to post a reply.