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

2020/4/10

need help with my file dialog

1
2
3
4
Cats123 Cats123

2020/4/12

#
Oh i just got it thanks so much for your help!!
Cats123 Cats123

2020/4/12

#
Hey so i was wondering if i could get some help with shooting. Ive been working on it for awhile and cant seem to get it. Im supposed to shoot the birds from the crcle meter and the rectangle meter is supposed to tell it how hard to shoot it. ∗ set its initial x speed to be 15 times the power fill ratio value from the RectangleMeter times the cosine2 of the angle (in radians) from the CircleMeter ∗ set its initial y speed to be 15 times the power fill ratio value from the RectangleMeter times the sine3 of the angle (in radians) from the CircleMeter i cant seem to get the actual shot to work and for it to be at the right position rectangle meter public class RectangleMeter extends Actor { private int currVal; // the currently filled amount of the rectangle private int height; // the maximum filled amount of the rectangle /** * Build a rectangle of specifed height (and 20 width). Initially, the rectangle is empty. * * @param height - the requested height */ public RectangleMeter(int height) { currVal=0; // initial filled amount is 0 this.height=height; // use specifed height redraw(); // build and use the rectangel image } // // redraw is a private method that will rebuild the image for a RectangleMeter private void redraw() { // build an appropriate (blank) image GreenfootImage img = new GreenfootImage(20,height); // draw the unfilled rectangle img.setColor(Color.GRAY); img.fill(); // draw a red outline around the rectangle img.setColor(Color.RED); img.drawRect(0,0,19,height-1); // fill in the currently filled portion of the rectangle img.setColor(Color.GREEN); img.fillRect(1,height-currVal+1,18,currVal-2); // use the newly generated image setImage(img); } /** * getFilledRatio returns a double representing the amount the meter is full as a ratio * (i.e. a value between 0 and 1 inclusively) * * @return the ratio */ public double getFilledRatio() {return currVal/(double)height;} /** * getHeight returns the height of this RectangleMeter in pixels * * @return number of pixels high the meter image is */ public int getHeight() {return height;} /** * getWidth returns the width of this RectangleMeter in pixels * * @return number of pixels wide the meter image is */ public int getWidth() {return getImage().getWidth();} /** * act - check user keypress status and modify meter accordingly */ public void act() { // modify the filled amount based on user keypresses if (Greenfoot.isKeyDown("Up") && currVal<height) { currVal++; redraw(); } else if (Greenfoot.isKeyDown("Down") && currVal>0) { currVal--; redraw(); } } } circle meter public class CircleMeter extends Actor { private int degFull; // how full is the meter? from 0 to maxDegree private int radius; // how big is the "sweeping" radius of this meter? private int maxDegree; // what is the maximum number of degrees in this meter? /** * constructs a 180 degree (half circle) meter with specified radius * * @param radius the size of the semicircle's radius */ public CircleMeter(int radius) { degFull=0; // starts out empty this.radius=radius; // use specified radius maxDegree=180; // by default, a half circle redraw(); // redraws the circle image, based on above. } /** * constructs a semicircle meter with specified radius and maximum degree. WARNING: this is really * intended to work with only multiples of 90 degrees. * * @param radius the size of the semicircle's radius * @param the sweeping range of the meter. Intended to work in multiples of 90 degrees. */ public CircleMeter(int radius, int maxDegree) { degFull=0; // starts out empty this.radius=radius; // use specifed radius // use specifed sweeping range (carefully) this.maxDegree=Math.min(180, Math.max(maxDegree, 10)); redraw(); // redraws the meter, based on above } // // redraw is a private method that will rebuild the image for a CircleMeter private void redraw() { // get a new (empty) image of the needed size. int imgWidth = radius + (maxDegree>90?radius:0); imgWidth = Math.max(imgWidth, radius); GreenfootImage img = new GreenfootImage( imgWidth , radius); // get underlying java awt image (need direct java awt rendering) java.awt.image.BufferedImage awtVersion = img.getAwtImage(); java.awt.Graphics2D g = (java.awt.Graphics2D) awtVersion.getGraphics(); // draw the blue filled backgound, indicating "unfilled" portion Shape arc = new Arc2D.Float(-(180-maxDegree)/90*radius, 0, 2*radius-1, 2*radius-1, 0, maxDegree, Arc2D.PIE); g.setPaint(java.awt.Color.BLUE); g.fill(arc); // draw the orange filled foregound, indicating "filled" portion arc = new Arc2D.Float(-(180-maxDegree)/90*radius, 0, 2*radius-1, 2*radius-1, 0, degFull, Arc2D.PIE); g.setPaint(java.awt.Color.ORANGE); g.fill(arc); // now that we have built the image, use it as this Actor's image. setImage(img); } /** * getAngle specifies (in radians) the current fill value of the meter * * @return the angle value in radians of the meter. */ public double getAngle() {return Math.toRadians(degFull);} /** * getDegrees specifies the current fill value of the meter in degrees * @return the angle value in degrees */ public int getDegrees() {return degFull;} /** * getArcX gives the x value of the perimeter of the circle where the meter is currently filled to * * @return the x location of the filled portion of the meter */ public int getArcX() { // find the "origin" x value of the meter int originX = getX() - (maxDegree<=90?radius/2:0); //we'll be an appropriate offset from that origin. return (int) (originX+Math.cos(Math.toRadians(degFull))*radius); } /** * getArcY gives the y value of the perimeter of the circle where the meter is currently filled to * * @return the y location of the filled portion of the meter */ public int getArcY() { // find the "origin" y value of the meter int originY = getY()+getImage().getHeight()/2; //we'll be an appropriate offset from that origin. return (int) (originY-Math.sin(Math.toRadians(degFull))*radius); } /** * Act - do whatever the CircleMeter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // if the left arrow key is being pressed and we can still grow, if (Greenfoot.isKeyDown("Left") && degFull<maxDegree) { degFull++; // a bit more is full redraw(); // rebuild image to note the updates fill amount } // if the right arrow key is being pressed and we could still shrink our fill ... if (Greenfoot.isKeyDown("Right") && degFull>0) { degFull--; // a bit less is full redraw(); // rebuild image to reflect the new fill amount } if(Greenfoot.isKeyDown("space")) { fire(); } } private void fire() { ViralLoad viralload = new ViralLoad(); getWorld().addObject(viralload,getX(),getY()); viralload.setRotation(getRotation()); viralload.move(20);//RectangleMeter.getHeight() * 15); } } and i have nothing in the viral load class
danpost danpost

2020/4/12

#
For a shot to be put in the world at the right position change 2nd line in fire method to this:
getWorld().addObject(viralload, getX(), getY()+radius/2);
Cats123 Cats123

2020/4/12

#
you're supposed to press the arrow keys left and the circle meter fills and as it fill the viral load goes with it. Its supposed to be sitting at the intersection of the two colors at the top
danpost danpost

2020/4/12

#
Cats123 wrote...
you're supposed to press the arrow keys left and the circle meter fills and as it fill the viral load goes with it. Its supposed to be sitting at the intersection of the two colors at the top
Then the last line in fire method needs to be:
viralload.move(radius);
Cats123 Cats123

2020/4/12

#
i did that and it just moves a little bit at the bottom of the screen?
danpost danpost

2020/4/12

#
Cats123 wrote...
i did that and it just moves a little bit at the bottom of the screen?
Oh, yeah. Line 3 needs to be:
viralload.setRotation(-degFull);
Cats123 Cats123

2020/4/12

#
Ok that actually makes it shoot in the right direction!! thank you!! do u know how i would make it shoot depending on how much the circle meter fills up??
danpost danpost

2020/4/12

#
Cats123 wrote...
Ok that actually makes it shoot in the right direction!! thank you!! do u know how i would make it shoot depending on how much the circle meter fills up??
I thought the circle meter was used to determine the direction the shot was to move and the rectangle was to determine its speed.
Cats123 Cats123

2020/4/12

#
yes like when i run it now the virus just moves a little bit forward. I need the shot to be based off the rectangle meter like yes its "speed" but more of like length of shot. Its supposed to be 15X however much the rectangle meter fills up.
danpost danpost

2020/4/12

#
If all else is working fine, you should be able to get a reference to the rectangle with:
RectangleMeter rectangle = (RectangleMeter)getWorld().getObjects(RectangleMeter.class).get(0);
and acquire the necessary info from the rectangle using:
double rectRatio = rectangle.getFilledRatio();
You can use the getRotation method to get the angle in degrees that the Circle gave it. You use that information to compute x and y speeds and use them for moving using the setLocation method in the act method. It would be best to use fields for the x and y speeds and compute their values in an addedToWorld(World) method.
Cats123 Cats123

2020/4/13

#
Im a little confused on where to put that?? any help with that??
Cats123 Cats123

2020/4/13

#
private void fire() { ViralLoad viralload = new ViralLoad(); getWorld().addObject(viralload,getArcX(),getArcY()); } how would i make this only put one in the world. When i hit the arrow keys it leaves a trail of them.
danpost danpost

2020/4/13

#
Cats123 wrote...
Im a little confused on where to put that?? any help with that??
Maybe make use of the addedToWorld(World) method in the ViralLoad class.
Cats123 wrote...
<< Code Omitted >> how would i make this only put one in the world. When i hit the arrow keys it leaves a trail of them.
See discussions listed upon searching "shoot once".
Cats123 Cats123

2020/4/13

#
could i put it in my prepare method because i need it to be there all the time?? How would i use the ArcX and ArcY variables from the CircleMeter class in the world??
There are more replies on the next page.
1
2
3
4