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

2019/5/1

Change an images color.

1
2
3
Notted Notted

2019/5/1

#
Simple. All I need to do set the color of an image.
1
2
3
4
5
6
7
public Invader(Color invaderColor)
    {
        GreenfootImage image = new GreenfootImage(32, 20);
        image.setColor(invaderColor);
        image.fill();
        setImage(image);
    }
The Invaders constructor sets the image and its color. The Invader is then called in Space (a world actor).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void prepare()
    {
        removeObjects(getObjects(null));
        for (int j=0; j<3+level; j++)
        {
            rows[j] = new Row();
            addObject(rows[j], 225, 25+j*50);
            for (int i=0; i<3; i++) addObject(new Invader(), 50+i*50, 20+j*50);
        }
        addObject(new Player(), 30, 475);
        timer = 0;
        direction = 1;
        drop = false;
    }
As you can see, the Invader class is being created, but I'm not sure how to set its color (I.e call a Color actor or method to set the actors color)
danpost danpost

2019/5/1

#
In line 8, you need to put a color in the empty parentheses, '()' >> '(put color here)'.
Notted Notted

2019/5/1

#
danpost wrote...
In line 8, you need to put a color in the empty parentheses, '()' >> '(put color here)'.
So this?
1
for (int i=0; i<3; i++) addObject(new Invader((Color.Red)), 50+i*50, 20+j*50);
danpost danpost

2019/5/1

#
Notted wrote...
So this? << Code Omitted >>
Try it out.
Notted Notted

2019/5/1

#
Doesn't work. "Illegal start of type" it says.
danpost danpost

2019/5/1

#
Notted wrote...
Doesn't work. "Illegal start of type" it says.
Try this:
1
for (int i=0; i<3; i++) addObject(new Invader(Color.RED), 50+i*50, 20+j*50);
Notted Notted

2019/5/1

#
Odd. It works. It seems I was calling it the wrong way.
danpost danpost

2019/5/1

#
Notted wrote...
Odd. It works. It seems I was calling it the wrong way.
I (incorrectly) did not think the double parentheses would make a difference. That is why I said to "Try it out."
Notted Notted

2019/5/1

#
I want to set the invaders size in their constructor. How would I do that?
danpost danpost

2019/5/1

#
Notted wrote...
I want to set the invaders size in their constructor. How would I do that?
You are setting its size in the constructor. See line 3 of your initial code post (top of page).
Notted Notted

2019/5/1

#
I think I worded that wrong. I want to have arguments that state the X and Y image size of the actor.
danpost danpost

2019/5/1

#
Notted wrote...
I think I worded that wrong. I want to have arguments that state the X and Y image size of the actor.
Do it similar to the way the color is done, then.
Notted Notted

2019/5/2

#
So like this:
1
public Invader(Color invaderColor int invaderX int invaderY)
danpost danpost

2019/5/2

#
Notted wrote...
So like this: << Code Omitted >>
The parameters need to have comma seperators.
Notted Notted

2019/5/2

#
The rows don't move anymore. They used to, but I think the invader's size is the problem.
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
public Invader(Color invaderColor, int invaderX, int invaderY)
    {
        GreenfootImage image = new GreenfootImage(invaderX, invaderY);
        image.setColor(invaderColor);
        image.fill();
        setImage(image);
    }
 
public boolean moveRow(int dir)
    {
        boolean changeDir = false;
        for (Object obj : getIntersectingObjects(Invader.class))
        {
            Invader invader = (Invader) obj;
            changeDir = invader.moveInvader(dir) || changeDir;
        }
        return changeDir;
    }
 
 public void act()
    {
        if (timer == 0 && drop)
        {
            for (int i=0; i<3+level; i++)
            {
                rows[i].dropRow();
                drop = false;
            }
            direction = -direction;
        }
        if (timer%3 == 0 && timer < 3*(3+level)) drop = rows[timer/3].moveRow(direction) || drop;
        timer = (timer+1)%32;
        if (getObjects(Invader.class).isEmpty())
        {
            if (level < 5) level++;
            prepare();
        }
        if (getObjects(Player.class).isEmpty()) prepare();
    }
There are more replies on the next page.
1
2
3