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

2020/4/15

I need some help with locations from another class

1
2
Cats123 Cats123

2020/4/15

#
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); } how would i use these two methods as a location for something else?? public void act() { if(shoot=true) { if(isTouching(Bird.class) || this.isAtEdge()) { removeTouching(ViralLoad.class); ViralLoad viralload = new ViralLoad(getArcX(),getArcY()); addObject(viralload,getArcX(), getArcY()); } } i want to put the location into the viral load
danpost danpost

2020/4/15

#
With circle being a reference to the CircleMeter object, you would use:
int x = circle.getArcX();
int y = circle.getArcY();
Cats123 Cats123

2020/4/15

#
public void act() { if(shoot=true) { if(isTouching(Bird.class) || this.isAtEdge()) { removeTouching(ViralLoad.class); int x = circle.getArcX(); int y = circle.getArcY(); ViralLoad viralload = new ViralLoad(x,y); addObject(viralload,x, y); } } if(shoot=false) { int x = circle.getArcX(); int y = circle.getArcY(); ViralLoad viralload = new ViralLoad(x,y); addObject(viralload,x,y); } i did this and i get an error for the add objects and an error for the circles
danpost danpost

2020/4/15

#
You have not declared what circle is; nor have you attempted to assign anything to it.
Cats123 Cats123

2020/4/15

#
how would i do that???
danpost danpost

2020/4/15

#
Cats123 wrote...
how would i do that???
Declare the variable ( circle ) to reference a CircleMeter object and assign it the one in the world.
Cats123 Cats123

2020/4/15

#
CircleMeter circle = (CircleMeter) getWorld(); CircleMeter circle = CircleMeter.getCircle(); like this???
danpost danpost

2020/4/15

#
Cats123 wrote...
CircleMeter circle = (CircleMeter) getWorld(); CircleMeter circle = CircleMeter.getCircle(); like this???
First line is good up to the semi-colon. Continue with a dot ( '.' ) and get the actor.
Cats123 Cats123

2020/4/15

#
CircleMeter circle = (CircleMeter) getWorld().getActor(); i get a error with this
danpost danpost

2020/4/15

#
getActor is not a method provided by the World class (a World object is what getWorld returns, so you must use something from there).
Cats123 Cats123

2020/4/15

#
Everything i try in there doesn't seem to be working???
danpost danpost

2020/4/15

#
Cats123 wrote...
Everything i try in there doesn't seem to be working???
The getObjects method is the one you will need.
Cats123 Cats123

2020/4/15

#
CircleMeter circle = (CircleMeter)getWorld().getObjects(); I get a error with that says it cannot be applied to given types
danpost danpost

2020/4/16

#
Cats123 wrote...
CircleMeter circle = (CircleMeter)getWorld().getObjects(); I get a error with that says it cannot be applied to given types
Look at the World class API documentation to see how the method is used.
Cats123 Cats123

2020/4/17

#
so i got it to show up im having trouble actually shooting it at an angle viralload class public class ViralLoad extends Actor { private boolean shoot; public double xspeed; public double yspeed; public double xspeed2; public double yspeed2; public int yspot; public int xspot; /** * Act - do whatever the Person wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public ViralLoad() { shoot=true; } public void act() { MyWorld myWorld = (MyWorld) getWorld(); if(shoot) { xspot= myWorld.circle.getArcX(); yspot= myWorld.circle.getArcY(); setLocation(xspot,yspot); setLocation(getX()+(int)xspeed, getY()+(int)yspeed); if(Greenfoot.isKeyDown("space")) { shoot=true; yspeed = yspeed = .01; xspeed2 =15 * myWorld.rectangle.getFilledRatio(); yspeed2 =-15 * myWorld.rectangle.getFilledRatio(); xspeed = xspeed2 * Math.cos(myWorld.circle.getAngle()); yspeed = yspeed2 * Math.sin(myWorld.circle.getAngle()); } else if(!isAtEdge()) { setLocation(getX()+(int)xspeed, getY()+(int)yspeed); yspeed = yspeed = .01; } if(isTouching(Bird.class) || this.isAtEdge()) { removeTouching(ViralLoad.class); myWorld.addObject(new ViralLoad(), xspot, yspot); } } } } rectangle class 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 class 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 fulll redraw(); // rebuild image to reflect the new fill amount } } } world public class MyWorld extends World { CircleMeter circle; RectangleMeter rectangle; ViralLoad viralload; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); circle = new CircleMeter(50,90); addObject(circle,20,580); rectangle = new RectangleMeter(100); addObject(rectangle,10,500); ViralLoad viralload = new ViralLoad(); addObject(viralload, 50,600); } /** * act - do whatever a MyWorld is supposed to do. (needs a better comment) */ public void act() { if (Greenfoot.isKeyDown("l")) { FileDialog fd = null; fd = new FileDialog (fd , "Open bird file" , FileDialog.LOAD); fd.setVisible(true); String fname = fd.getDirectory() + fd.getFile(); File birdFile = new File (fname); Scanner birdReader = null; try { birdReader = new Scanner(birdFile); } catch (IOException ioe) { System.out.println("no file found:" + fname); return; } while(birdReader.hasNext()) { String type = birdReader.next(); if (type.equalsIgnoreCase("bird")) { int speed = birdReader.nextInt(); int intX = birdReader.nextInt(); int intY = birdReader.nextInt(); Bird bigBird = new Bird(speed); addObject(bigBird, intX, intY); } } } } }
There are more replies on the next page.
1
2