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

2013/1/13

Object removing

DarthBane DarthBane

2013/1/13

#
Hello everybody, I want my programm to remove an object if the number of collected coins get 7. Can you help me please? Additionally, my counter doesn't show the score, although i used the code from another programm where it's working.
Gevater_Tod4711 Gevater_Tod4711

2013/1/13

#
if you want to remove an object use the mehtod removeObject(Actor actor). for more help we need to see the code you already got.
DarthBane DarthBane

2013/1/13

#
All right here's the code, i think this extract is enough to understand the system.. code]public class Playground extends World { private Counter counter; private You you; private int score; /** * Constructor for objects of class Playground. * */ public Playground() { super(600, 600, 1); score = 0; counter = new Counter(); populateworld(); counter.setScore(score); } . . . public void addPoint() { score += 1; counter.setScore(score); } _________________ public You() { image1 = new GreenfootImage("YOU.png"); image2 = new GreenfootImage("YOU1.png"); setImage(image1); } public void act() { checkKeypress(); if(canSee(Gold.class)); { collect(); } } public void collect() { { Gold g = (Gold)getOneIntersectingObject (Gold.class); if (g != null) { g.goldSammeln(); Playground t = (Playground) getWorld(); t.addPoint(); Greenfoot.playSound("coin-01.wav"); } }} public void checkKeypress() { if(Greenfoot.isKeyDown("left")) { turn(-5); } if(Greenfoot.isKeyDown("right")) { turn(5); } if(Greenfoot.isKeyDown("up")) { move(5); switchImage(); } if(Greenfoot.isKeyDown("down")) { move(-5); switchImage(); } } public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } }} ___________________________ import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.awt.Font; //*@author MrCohen() public class Counter extends Actor { private GreenfootImage scoreBoard; private Color backgroundColor = new Color (108, 108, 108); private Color textColor = new Color (176,0,0); private Font textFont = new Font ("Arial", Font.BOLD, 28); public Counter() { scoreBoard = new GreenfootImage(150, 40); scoreBoard.setFont (textFont); scoreBoard.setColor (backgroundColor); scoreBoard.fill(); this.setImage(scoreBoard); } public void setScore (int score) { String s = "Score: " + score; // erase everything in score and start again scoreBoard.clear(); scoreBoard.setColor (backgroundColor); scoreBoard.fill(); scoreBoard.setColor (textColor); scoreBoard.drawString (s, 5, 32); } } ________________________ .
DarthBane DarthBane

2013/1/13

#
Oh, i forgot the obkect i want to remove:
public class Flames1 extends Animal
{ private GreenfootImage image1;
  private GreenfootImage image2;
  
  public Flames1()
  {
      image1 = new GreenfootImage("Flammen.png");
      image2 = new GreenfootImage("Flammen2.png");
      setImage(image1);
      
      
    }
    
   public void act() 
    {
        if(canSee(You.class));
        { eat();
           }
        switchImage();
    }
 
  public void switchImage()
        { if (getImage() == image1)
        {
                setImage(image2); }
                else
                {
                    setImage(image1); }
                
    }
 public void eat()
   {  
       Actor you;
       you = getOneObjectAtOffset(0, 0, You.class);
       if (you != null)
       {
           World world;
           world = getWorld();
           world.removeObject(you);
           Greenfoot.playSound("705902_SOUNDDOGS__fi.mp3"); 
        System.out.println("Sorry, start again!"); }}
You need to login to post a reply.