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

2015/12/18

creating a rectangle

Blubberfish007 Blubberfish007

2015/12/18

#
im trying to draw a rectangle in my game to show the player when the tactical nuke is ready, but its not showing plz help
Blubberfish007 Blubberfish007

2015/12/18

#
this is the code
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
public class NukeBar extends Actor
{
 
    int acts = 0;
    int NukeBarWidth = 15;
    int NukeBarHeight = 0;
    public NukeBar()
 
    {
        update();
    }
 
    public void act()
    {
        acts = acts + 1;
         
        update();
        if (acts == 150)
        {
            NukeBarHeight = NukeBarHeight + 20;
            acts = 0;
        }
 
        if (NukeBarHeight == 100)
        {
            if (Greenfoot.isKeyDown("c"))
            {
                background Background = (background)getWorld();
                getWorld().removeObjects(getWorld().getObjects(enemy1.class));
                 
 
            }
            if (Greenfoot.isKeyDown("c"))
            {
                background Background = (background)getWorld();
                getWorld().removeObjects(getWorld().getObjects(enemy2.class));
                 
 
            }
            if (Greenfoot.isKeyDown("c"))
            {
                background Background = (background)getWorld();
                getWorld().removeObjects(getWorld().getObjects(enemy3.class));
                 
 
            }
            if (Greenfoot.isKeyDown("c"))
            {
                background Background = (background)getWorld();
                getWorld().removeObjects(getWorld().getObjects(enemy4.class));
                 
 
            }
        }
        if (NukeBarHeight == 120)
        {
            NukeBarHeight = NukeBarHeight - 20;
        }
    }   
 
    public void update()
    {
        setImage(new GreenfootImage(NukeBarWidth +2, NukeBarHeight +2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0,0,NukeBarWidth, NukeBarHeight);
        myImage.setColor(Color.RED);
 
    }   
}
in the bar class
Blubberfish007 Blubberfish007

2015/12/18

#
what should i do now?
fejfo fejfo

2015/12/19

#
well in the update method you should use the setImage function like this
1
2
3
4
5
6
7
{
        GreenfootImage myImage = new GreenfootImage(NukeBarWidth +2, NukeBarHeight +2));
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0,0,NukeBarWidth, NukeBarHeight);
        myImage.setColor(Color.RED);
        setImage(myImage);
    }
danpost danpost

2015/12/19

#
@fejfo, the first line (line 63) sets the image to the actor and then the next line gets a reference to it. There is nothing horribly wrong with that. The problem is that no RED is being placed into the image (after line 67). Also, the limiting of the value of NukeBarHeight should be done immediately after changing its value (lines 55 through 58 should be cut and inserted at line 21). You will find, because you are using a varying height for the bar itself, that the bar will appear to "drop" in the world, while its value increases. This is because the center of the bar is fixed in the world, not the base, Also, I have never seen a bar that changes its image in both the horizontal and the vertical (in reference to line 63).
Blubberfish007 Blubberfish007

2015/12/20

#
width doesnt change i think that is a white outline
Blubberfish007 Blubberfish007

2015/12/20

#
is there anything wrong with the bit where it removes all enemies on screen?
Blubberfish007 Blubberfish007

2015/12/20

#
and how would i keep it in place then?
Blubberfish007 Blubberfish007

2015/12/20

#
changed it to horizontal works better now ,except its only a white outline. plz help
danpost danpost

2015/12/20

#
Blubberfish007 wrote...
width doesnt change i think that is a white outline
Line 63 uses both 'NukeBarWidth+2,' and 'NukeBarHeight+2'. Both dimensions will change.
danpost danpost

2015/12/20

#
Blubberfish007 wrote...
is there anything wrong with the bit where it removes all enemies on screen?
No. But it should be cleaned up and condensed. None of the 'background' variables in each part are being used; so, their declaration lines can be removed. The condition for each part are the same; so, the removals can be put in a single 'if' block. Then, you have two conditions that are needed to remove the enemies, which can then be combined. The final result is to replace lines 24 through 54 with this:
1
2
3
4
5
6
7
if (NukeBarHeight == 100 && Greenfoot.isKeyDown("c"))
{
    getWorld().removeObjects(getWorld().getObjects(enemy1.class));
    getWorld().removeObjects(getWorld().getObjects(enemy2.class));
    getWorld().removeObjects(getWorld().getObjects(enemy3.class));
    getWorld().removeObjects(getWorld().getObjects(enemy4.class));
}
danpost danpost

2015/12/20

#
Blubberfish007 wrote...
and how would i keep it in place then?
Blubberfish007 wrote...
changed it to horizontal works better now ,except its only a white outline. plz help
Maybe you can learn something from my Value Display Tutorial. The last section deals with displaying bars.
You need to login to post a reply.