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

2015/12/27

canSee() / isTouching() doesn't work

Ramin Ramin

2015/12/27

#
I want that if my injection hits a sick Man (Man with image 2) the Man is healed (back to image1). But if I touch him sometimes nothing happens and sometimes if I'm lucky it works. Why does it not react when I'm touching it? It doesn't work with the isTouching() method either. Maybe I have to touch him with a special spot of my object? The code is in class MAN. Ich will das ein kranker Mann (mit dem image2) wieder gesund wird (zurück zu image1), wenn dieser von einer Spritze berührt wird. Ich hab das mit canSee() und mit isTouching() versucht, aber bei beiden ist es eine Glückssache, ob es klappt oder nicht. Ich kann förmlich im Mann drinnen stehen mit meiner Spritze und es tut sich trotzdem nichts und manchmal berühr ich es nicht mal wirklich und es klappt aber plötzlich. Ich hab gedacht ich müsse ihn mit einer bestimmten Stelle meines Objekt treffen... vielleicht dem Mittelpunkt. Aber es klappt ja nicht mal wenn ich wirklich in ihm stehe....
1
2
3
4
5
6
7
public void changeImage()
{
 if (canSee(injection.class) && getImage() == image2)
       {
            setImage(image1);
        }
}
danpost danpost

2015/12/28

#
The 'canSee' method is not a member of the Actor class; however, if you added one to that particular subclass of Actor or you have that class extend the Animal class, you might get it to work. The 'isTouching' method, on the other hand, should work (provided you have a greenfoot version of 2.4.0 or higher. There are other things that could be looked at that might cause problems; however, you would need to show the entire class code (to make it easy) for checking.
Ramin Ramin

2015/12/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Write a description of class Man here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Man extends Human
{
private GreenfootImage image1;
private GreenfootImage image2;
 
public boolean canSee(Class clss)
{
    return getOneObjectAtOffset(0, 0, clss) != null;
  }
/**
     * Act - do whatever the Mann wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       image2 = new GreenfootImage("ManIll.png");
       image1 = new GreenfootImage("Man.png");
       move(1);
       atEdge();
       infect();
        
    }   
/**
     * Turns at the Edge.
     */
    public void atEdge()
    {
        if (isAtEdge())
        {
           turn(45);
         }
    }
/**
     * Infects humans.
     */
    public void infect()
    {
       if (canSee(Virus1.class) && Greenfoot.getRandomNumber(100)<10)
       {
           setImage(image2);
        }
       if (canSee(Virus2.class) && Greenfoot.getRandomNumber(100)<25)
       {
           setImage(image2);
        }
       if (Greenfoot.getRandomNumber(100)<90 && getImage() == image2)
       {
           getWorld().removeObject(this);
           peopleDied = peopleDied +1 ;
        }
    }
/**
     * Heals infected humans.
     */
    public void heal()
    {
      if (isTouching(Injection.class) && getImage() == image2)
       {
          setImage(image1);
          World myWorld = getWorld();
          City city = (City)myWorld;
          Counter counter = city.getCounter();
          counter.addScore();
        }
    }
 
}
Ramin Ramin

2015/12/28

#
the score doesn't work either
danpost danpost

2015/12/28

#
Okay. I see why healing and dying will never work with the code given. It is because every act cycle you are changing the GreenfootImage objects in 'image1' and 'image2'. So, you set 'image2' with one image and then later, on a different act cycle, compare it to a different 'image2' (a copy). To correct this issue, add a constructor to the class to set the images there:
1
2
3
4
5
6
public Man()
{
    image1 = new GreenfootImage("Man.png");
    image2 = new GreenfootImage("ManIll.png");
    setImage(image1);
}
Do the same in the Woman class. As far as the counter not working -- will need to see the City class code.
Ramin Ramin

2015/12/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class City extends World
{
Counter counter = new Counter();
    /**
   * Constructor for objects of class City.
   *
   */
  public City()
  
    super(800, 750, 1);
    populateWorld();
}
public Counter getCounter()
{
    return counter;
}
  /**
   * Populates the world.
   */
  private void populateWorld()
  {
    addObject(new hospital(), 71, 157);
    addObject(new hospital(), 37, 720);
    addObject(new Injection(), 120, 150);
    addObject(new Counter(), 74, 39);
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Man(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Woman(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus1(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus2(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus2(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
    addObject(new Virus2(), Greenfoot.getRandomNumber(750), Greenfoot.getRandomNumber(700));
}
}
danpost danpost

2015/12/28

#
The Counter object added into the world (line 25) is not the same Counter object that is stored in the 'counter' field (line 3) and return by the 'getCounter' method (line 15).
Ramin Ramin

2015/12/28

#
Why not? The score counter is there but it just doesn't update. How can i make it the same?
danpost danpost

2015/12/28

#
Ramin wrote...
Why not?
Because you create one Counter object in line 3 and assign it to 'counter'; but, then you create another (different) one at line 25 and add that one into the world.
How can i make it the same?
Just add the one already assigned to 'counter' into the world at line 25:
1
addObject(counter, 74, 39);
You need to login to post a reply.