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

2012/8/12

It should Work...

LolaLuna LolaLuna

2012/8/12

#
Why does this not work? Rocket rocket = new Rocket(); public Label() { GreenfootImage img =new GreenfootImage(100,50); img.drawString("Shots: " +rocket.getShot(),2,20); setImage(img); } public void act() { GreenfootImage img =new GreenfootImage(100,50); img.drawString("Shots: " +rocket.getShot(),2,20); setImage(img); } Shot is an attribute from the Rocket class, but I always get the same value here although it`s changing.
erdelf erdelf

2012/8/12

#
Without code of rocket I don't think we can help you. btw. if you post code click on code under the reply box
danpost danpost

2012/8/12

#
I think the problem is due to you rocket, or should I say rockets. Each time you create an object of this class, you are creating a 'new' rocket. That rocket is NOT the one you probably created in your sub-class of world.
danpost danpost

2012/8/12

#
Though the way you are going about 'updating' the Label would work, it is not really optimal. Since you will know when the value of Shot changes, you can call the label to reset the value at that time. In other words, instead of having a 'public void act()' in the Label class, put a 'public void setValue(int newVal)' method in it and call it each time a shot is fired after decrementing the Shot value. Also, you could create the label with a String parameter for the label and an int parameter for the initial value of the label in the constructor. Example follows:
import greenfoot.*;

public class Label extends Actor
{
    String text = "";

    public Label(String label, int value)
    {
        text = label;
        setValue(value);
    }

    public setValue(int newVal)
    {
        GreenfootImage img =new GreenfootImage(100, 50);
        img.drawString(text + ": " + newVal, 2, 20);
        setImage(img);
    }
}
Now, the Label class is re-usable. You can create one for "Lives: 3", one for "Health: 37", one for "Shots: 10", etc. Next, since the shots, and shot count, come from the rocket, it would be best to have the rocket create whatever labels you need for it (you can include a 'public void addedToWorld(World world)' method to add the label for 'Shots' into the world. You shoud have a reference to it as an instance variable of type Label.
//in the Rocket class (instance variable)
int shots = 10; // however many you start with
Label shotLabel = new Label("Shots", shots);
// addedToWorld method
public void addedToWorld(World world)
{
    world.addObject(shotLabel, 50, 30);
}
// then when a shot is triggered, where you have the first line, add the second
shots--;
shotLabel.setValue(shots);
LolaLuna LolaLuna

2012/8/13

#
This is the whole code of class rocket: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.JOptionPane; import java.awt.Point; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.awt.TextArea; /** * Write a description of class Rocket here. * * @author (your name) * @version (a version number or a date) */ public class Rocket extends Actor { private int shots; private boolean start=(true); private int lvl=1; SimpleTimer timer = new SimpleTimer(); Label shotLabel = new Label("Shots", shots); public Rocket() { shots=10; } /** * Act - do whatever the Rocket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("down")) { moveDown(); } if(Greenfoot.isKeyDown("up")) { moveUp(); } if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } //System.out.println("Shots: " +getShot()); // Ausgabe(); shotLabel.setValue(shots); checkCollision(); //Schuss if("space".equals(Greenfoot.getKey())) { if(shots>0) { getWorld().addObject(new Shot(),getX()+50, getY()); shots=shots-1; } } } //Kollision public void checkCollision() { Actor meteor = getOneIntersectingObject(Meteor.class); if(meteor != null) { int x; int y; x =this.getX(); y =this.getY(); Explosion ex; ex = new Explosion(); ex.setImage("explosion.png"); getWorld().addObject(ex,x,y); Greenfoot.delay(10); Greenfoot.stop(); showDialogue(); } if (start==(false)&&timer.millisElapsed() < 60000) { } else {Actor barrel = getOneIntersectingObject(Barrel.class); if(barrel != null) { shots = shots +5; //Greenfoot.playSound("Bonus.wav"); getWorld().removeObject(barrel); start = (false); timer.mark(); } } } public void setLvl() { if (timer.millisElapsed() < 1250000) { lvl=1; } if (timer.millisElapsed() < 2500000) { lvl=2; } if (timer.millisElapsed() < 3500000) { lvl=3; } } public int getLvl() { return lvl; } public void showDialogue() { JOptionPane.showMessageDialog(null,"GAME OVER!"); } public void moveDown() { switch(getLvl()) { case 1: setLocation(getX(),getY()+2); break; case 2: setLocation(getX(),getY()+3); break; case 3: setLocation(getX(),getY()+4); break; } } public void moveUp() { switch(getLvl()) { case 1: setLocation(getX(),getY()-2); break; case 2: setLocation(getX(),getY()-3); break; case 3: setLocation(getX(),getY()-4); break; } } public void moveLeft() { switch(getLvl()) { case 1: setLocation(getX()-2,getY()); break; case 2: setLocation(getX()-3,getY()); break; case 3: setLocation(getX()-4,getY()); break; } setLocation(getX()-2,getY()); } public void moveRight() { switch(getLvl()) { case 1: setLocation(getX()+2,getY()); break; case 2: setLocation(getX()+3,getY()); break; case 3: setLocation(getX()+4,getY()); break; } } void Ausgabe() { GreenfootImage img = new GreenfootImage(50,100); img.drawString("Shots: " +shots,2,20); setImage(img); } public int getShot() { return shots; } } Unfortunatly the example above does not work at my project...
danpost danpost

2012/8/13

#
Change 'private int shots;' to 'private int shots = 10;' and remove 'shots = 10;'. The following should not be in the Rocket class: void Ausgabe() { GreenfootImage img = new GreenfootImage(50,100); img.drawString("Shots: " +shots,2,20); setImage(img); } And this line: shotLabel.setValue(shots); should be immediately after this line: shots = shots - 1; Hope that helps..
LolaLuna LolaLuna

2012/8/13

#
Now I got it like this, but it also doesn`t work.. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.JOptionPane; import java.awt.Point; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.awt.TextArea; /** * Write a description of class Rocket here. * * @author (your name) * @version (a version number or a date) */ public class Rocket extends Actor { private int shots =10; private boolean start=(true); private int lvl=1; SimpleTimer timer = new SimpleTimer(); Label label = new Label("Shots", shots); public Rocket() { } /** * Act - do whatever the Rocket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("down")) { moveDown(); } if(Greenfoot.isKeyDown("up")) { moveUp(); } if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } //System.out.println("Shots: " +getShot()); checkCollision(); //Schuss if("space".equals(Greenfoot.getKey())) { if(shots>0) { getWorld().addObject(new Shot(),getX()+50, getY()); shots=shots-1; } label.setValue(shots); } } //Kollision public void checkCollision() { Actor meteor = getOneIntersectingObject(Meteor.class); if(meteor != null) { int x; int y; x =this.getX(); y =this.getY(); Explosion ex; ex = new Explosion(); ex.setImage("explosion.png"); getWorld().addObject(ex,x,y); Greenfoot.delay(10); Greenfoot.stop(); showDialogue(); } if (start==(false)&&timer.millisElapsed() < 60000) { } else {Actor barrel = getOneIntersectingObject(Barrel.class); if(barrel != null) { shots = shots +5; //Greenfoot.playSound("Bonus.wav"); getWorld().removeObject(barrel); start = (false); timer.mark(); } } } public void setLvl() { if (timer.millisElapsed() < 1250000) { lvl=1; } if (timer.millisElapsed() < 2500000) { lvl=2; } if (timer.millisElapsed() < 3500000) { lvl=3; } } public int getLvl() { return lvl; } public void showDialogue() { JOptionPane.showMessageDialog(null,"GAME OVER!"); } public void moveDown() { switch(getLvl()) { case 1: setLocation(getX(),getY()+2); break; case 2: setLocation(getX(),getY()+3); break; case 3: setLocation(getX(),getY()+4); break; } } public void moveUp() { switch(getLvl()) { case 1: setLocation(getX(),getY()-2); break; case 2: setLocation(getX(),getY()-3); break; case 3: setLocation(getX(),getY()-4); break; } } public void moveLeft() { switch(getLvl()) { case 1: setLocation(getX()-2,getY()); break; case 2: setLocation(getX()-3,getY()); break; case 3: setLocation(getX()-4,getY()); break; } setLocation(getX()-2,getY()); } public void moveRight() { switch(getLvl()) { case 1: setLocation(getX()+2,getY()); break; case 2: setLocation(getX()+3,getY()); break; case 3: setLocation(getX()+4,getY()); break; } } public int getShot() { return shots; } } if I put the Line immidiatly after the shots= shots-1; I might get a problem whith the bonusbarrel.
danpost danpost

2012/8/13

#
That should not be a problem. Move it back to immediately after the 'shots = shots = 1;', then add the same line in immediately after 'shots = shots + 5;'. Also, I do not see the 'addedToWorld(World world)' method in the Rocket class. You need it to add the shot label into the world.
LolaLuna LolaLuna

2012/8/14

#
Thank you, now it finally works :)
LolaLuna LolaLuna

2012/8/14

#
Oh no! Could somebody help me with this part? public void setLvl() { if (timer.millisElapsed()/1000 < 60) { lvl=1; Greenfoot.setSpeed(50); lvlLabel.setValue(lvl); } if (60<timer.millisElapsed()/1000&&timer.millisElapsed()/1000< 120) { lvl=2; Greenfoot.setSpeed(58); lvlLabel.setValue(lvl); } if (120>timer.millisElapsed()/1000 ) { lvl=3; Greenfoot.setSpeed(65); lvlLabel.setValue(lvl); } }
LolaLuna LolaLuna

2012/8/14

#
Got it!!! Thank you for your help! :)
danpost danpost

2012/8/15

#
If you do not mind the speed at level 3 being 66, instead of 65, you could code this:
public void setLvl()
{
    if (lvl < 3 && timer.millisElapsed() / 60000> lvl)
    {
        lvl++;
        Greenfoot.setSpeed(42 + 8 * lvl);
        lvlLabel.setValue(lvl);
    }
}
Also, your movement methods could be simplified like the one below:
public void moveRight()
{
    setLocation(getX() + 1 + getLvl(), getY());
}
You need to login to post a reply.