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

2018/6/7

space invader barriers

1
2
3
ogy2014 ogy2014

2018/6/7

#
space class
1
2
3
4
5
6
7
8
9
10
11
12
public Space()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(800, 600, 1);
    addObject (gameover, 400, 300);
    addObject (lifeboard, 500, 10);
    addObject (new Player(), 400, 550);
    addObject (new Invaders(), 0, 0);
    addObject (new Barriers(), 0, 0);
    addObject (scoreboard, 100, 10);
    setPaintOrder(GameOver.class,Player.class,Scoreboard.class);
}
Barrier class
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
74
75
76
77
78
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Barrier here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Barrier extends Actor
{
    /**
     * Act - do whatever the Barrier wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */   
    private GreenfootImage base1;
    private GreenfootImage base2;
    private GreenfootImage base3;
    private GreenfootImage base4;
     
    public void act()
    {
        Actor invadermissile = getOneIntersectingObject(InvaderMissile.class);
        if(invadermissile != null)
        {
            if (getImage().equals(base1))
            {
                setImage(base2);
            }
            else
            if (getImage().equals(base2))
            {
                setImage(base3);
            }
            else
            if (getImage().equals(base3))
            {
                setImage(base4);
            }
            else
            if (getImage().equals(base4))
            {
                getWorld().removeObject(this);
            }
        }
         
        Actor playermissile = getOneIntersectingObject(PlayerMissile.class);       
        if(playermissile != null)
        {
            if (getImage().equals(base1))
            {
                setImage(base2);
            }
            else
            if (getImage().equals(base2))
            {
                setImage(base3);
            }
            else
            if (getImage().equals(base3))
            {
                setImage(base4);
            }
            else
            if (getImage().equals(base4))
            {
                getWorld().removeObject(this);
            }
        }
    }   
 
    public void addedToWorld(World world)
    {
        base1 = new GreenfootImage("base1.png");
        base2 = new GreenfootImage("base2.png");
        base3 = new GreenfootImage("base3.png");
        base4 = new GreenfootImage("base4.png");
    }
}
Barriers class
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Barriers here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Barriers extends Actor
{
    /**
     * Act - do whatever the Barriers wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act()
    {
        if(getWorld() .getObjects(Barrier.class) .size() == 0)
        {
            for(int x = 450; x < 451; x++)
            {
                for(int y = 300; y < 301; y++)
                {
                    getWorld() .addObject(new Barrier(), x * 1000 + 30, y * 100+ 30);
                }
            }
        }
    }   
}
ogy2014 ogy2014

2018/6/7

#
I'm not sure what line 18 to 27 on the barriers class does entirely i do know that it places multiple barriers. does anyone know how to make 3 barriers show up on one line that i can change the image like what i was trying to do on the barrier class on line 22-44. i will try setting up 3 different classes
danpost danpost

2018/6/7

#
I thiink I mentioned before what I am about to say to you. Do not create objects of a class from within the class of that object.
ogy2014 ogy2014

2018/6/7

#
ye i remove the barriers class and am just using 3 different barrier classes however i now have the problem of the bullets phasing through it is to do with lines 22-44
danpost danpost

2018/6/7

#
ogy2014 wrote...
ye i remove the barriers class and am just using 3 different barrier classes however i now have the problem of the bullets phasing through it is to do with lines 22-44
You should probably remove an intersecting invader missile.
ogy2014 ogy2014

2018/6/7

#
but then the invaders cannot break the barrier is there no other way to sort this?
ogy2014 ogy2014

2018/6/7

#
it didnt work anyway
ogy2014 ogy2014

2018/6/7

#
is there anything wrong with the code or is it to do with how java works in general
danpost danpost

2018/6/7

#
ogy2014 wrote...
but then the invaders cannot break the barrier is there no other way to sort this?
Not if you remove it when it intersects and damages the barrier.
ogy2014 ogy2014

2018/6/7

#
ah ok i think i get it
ogy2014 ogy2014

2018/6/7

#
i dont get it sorry
ogy2014 ogy2014

2018/6/7

#
oh you mean remove the missile that hits
ogy2014 ogy2014

2018/6/7

#
i cant figure it out
ogy2014 ogy2014

2018/6/7

#
I don't think i mentioned it but it isn't changing the image either.
danpost danpost

2018/6/7

#
ogy2014 wrote...
I don't think i mentioned it but it isn't changing the image either.
Try setting the image of the barrier to base1 at the end of the addedToWorld method.
There are more replies on the next page.
1
2
3