![]() In the act method when I try to print out the score it shows it is increasing but when I print it out with JOptionPane or as normal at the end of the program it gives me 0. Your score was " + score, "You lose!", JOptionPane.WARNING_MESSAGE) JOptionPane.showMessageDialog(null, "Try again next time. * the 'Act' or 'Run' button gets pressed in the environment. * Act - do whatever the Message wants to do. Message Class: public class Message extends Crab JOptionPane.showMessageDialog(null, newscore, "You lose!", JOptionPane.WARNING_MESSAGE) When I try to print it it changes to zero for some reason.Ĭrab Class: public class Crab extends Animal tLocation(hook.getX(),hook.I am trying to output a score in the greenfoot IDE and all is working fine (the score is increasing) until I try to print it. If (Greenfoot.isKeyDown("down") & hook.height < getWorld().getHeight()-getY()-5) //move hook down If (Greenfoot.isKeyDown("up") & hook.height > 5) //move hook up If (Greenfoot.isKeyDown("right")) //move boat and hook to the right side If (Greenfoot.isKeyDown("left")) //move boat, line and hook to the left side GetWorld().addObject(hook, getX(), getY()+10) //add the hook to the world GetWorld().addObject(line, getX(), getY()) //add the line to the world Line = new Line(10) //create the line with a start-length of 10. I'm new to programming for sure, but I have added multiple levels, different types of fish worth different points, fish that spawn other fish, a system to buy upgrades for the ship (increasing ship speed and increasing the hook speed), but I can't figure out how to draw a simple line. It's just seem crazy I'm having so much trouble with this. I'll give it another go making HookLine an Actor, and using the constructor to draw the line, and the act method to resize it. So if I wanted to lower the hook, which would require the line to increase, both the top and the bottom of the line would increase in length, as opposed to the just the bottom. When I ran it (using scale() to attempt to change the line) the entire image would scale, not just the bottom. I added the objects when the world was created, which placed the line at a Y offset from the boat, and the hook at the end of the line. I had previously drawn a line, well see 2pixels wide by 200 pixels. ![]() I was just being lazy and wanted the HookLine to inherit some methods from the Animal class without having to re-do the code. It is simply a black line in the middle the screen that move left or right with the boat and doesn't connect the boat to the hook or get bigger or smaller. ![]() The line is drawn, but is a static image (that does move around with the boat along the X axis) that doesn't do anything. I add it the world in the world subclass Ocean using this: Img.drawLine(boat.getX(), boat.getY(), hook.getX(), hook.getY()) GreenfootImage img = new GreenfootImage(2,100) Import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Here is the source for my HookLine class, the class that I want to use to draw a line from the boat to the hook. That way, you can clear and refill the image with black up to the current length of the line and relocate the hook at that time. Probably the easiest way to set it up is with a line image that is the length of the maximum line length and have the line location at exactly the same location as the boat plus a y-offset of half the maximum line length. No matter what you decide in the above, it would be best NOT to have act methods in either the line or the hook classes, but to call methods in those classes from the boat class, otherwise you may end up with some synchronization problems. It would probably be best to do that in the same place you control the length of the line. You would have a choice of having the boat also control the location of the hook or have the line control that. If you have not yet loaded Greenfoot and the Java JDK8 compiler onto your computer (sounds complicated but it’s not), then go to Appendix B: Setting up Greenfoot. ![]() The variable length of the line can be controlled either by the boat or by the line itself (not talking image here). If you don’t know what the Java programming language is and how Greenfoot can be used to learn Java, you can find this in Appendix A: Why Learn Java and How Does Greenfoot Help. With the line as an actor, to keep it with the boat, the boat object will have to control its location.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |