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

2018/12/18

Hit detection.

Will5230 Will5230

2018/12/18

#
Right now I am working on hit detection in Greenfoot and have 3 out of the 4 sides of the wall done. I can't seem to get the last one because when I mess with the right wall code the left wall stops working a vice versa. here is my pacman 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Pacman here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Pacman extends Actor
{
 
    /**
     * Act - do whatever the Pacman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
 
    public Pacman()
    {
        GreenfootImage image = getImage(); 
        image.scale(35, 35);
        setImage(image);
    }
 
    public void act()
    {
        controls();
        move(-3);
        eat();
        checkRightWalls();
        platformAbove();
        checkLeftWalls();
        platformBelow();
    }
 
    private void controls()
    {
        if ( Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D") ) {
            setRotation(-180);
        }
        if ( Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A") ) {
            setRotation(-0);
        }
        if ( Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W") ) {
            setRotation(-270);
        }
        if ( Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S") ) {
            setRotation(-90);
        }
    }
 
    private boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / -2 );
 
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Wall.class);
        if(ceiling != null)
        {
 
            bopHead(ceiling);
            return true
        }
        else
        {
 
            return false;
        }
    }
 
    public void bopHead (Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() +(ceilingHeight + getImage().getWidth())/2;
 
        setLocation(getX() , newY);
    }
 
    private boolean platformBelow()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / 2 );
 
        Actor floor = getOneObjectAtOffset(0, yDistance, Wall.class);
        if(floor != null)
        {
 
            bopHeads(floor);
            return true
        }
        else
        {
 
            return false;
        }
    }
 
    public void bopHeads (Actor floor)
    {
        int ceilingHeight = floor.getImage().getHeight();
        int newYL = floor.getY() -(ceilingHeight + getImage().getWidth())/2;
 
        setLocation(getX() , newYL);
    }
 
    private boolean checkRightWalls()
    {
        int spritewidth = getImage().getWidth();
        int xDistance = (int)(spritewidth/2);
 
        Actor rightwall = getOneObjectAtOffset(xDistance, 0, Wall.class);
        if(rightwall == null)
        {
            return false
        }
        else
        {
            stopByRightWall(rightwall);
            return true;
        }
    }
 
    public void stopByRightWall (Actor rightwall)
    {
        int wallWidth = rightwall.getImage().getWidth();
        int newX = rightwall.getX() -(wallWidth + getImage().getWidth())/2;
 
        setLocation(newX , getY());
    }
 
    private boolean checkLeftWalls()
    {
        int spritewidth = getImage().getWidth();
        int xDistance = (int)(spritewidth/2);
 
        Actor leftwall = getOneObjectAtOffset(xDistance, 0, Wall.class);
        if(leftwall == null)
        {
            return false
        }
        else
        {
            stopByLeftWall(leftwall);
            return true;
        }
    }
 
    public void stopByLeftWall (Actor leftwall)
    {
        int wallWidth = leftwall.getImage().getWidth();
        int newXL = leftwall.getX() -(wallWidth + getImage().getWidth())/2;
 
        setLocation(newXL , getY());
    }
 
    public void eat()
    {
 
        Actor Pellet = getOneObjectAtOffset(0, 0,Pellet.class);
        if(Pellet != null)
        {
            World world = getWorld();
            world.removeObject(Pellet);
            ((MyWorld)world).counter.add(1);
        }
    }
}
here is my wall 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Wall here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Wall extends Actor
{
    /**
     * Act - do whatever the Wall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       
    }  
     
    public Wall()
    {
       GreenfootImage image = getImage(); 
        image.scale(60, 40);
        setImage(image);
    }
}
and here is the world 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends greenfoot.World
{
    Counter counter;
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Pacman pacman = new Pacman();
        addObject(pacman,780,100);
 
        counter = new Counter();
        addObject(counter,728,26);
 
        Wall wall = new Wall();
        addObject(wall,770,66);
 
        Wall wall2 = new Wall();
        addObject(wall2,710,66);
 
        Wall wall3 = new Wall();
        addObject(wall3,650,66);
 
        Wall wall4 = new Wall();
        addObject(wall4,590,66);
         
    }
}
It seems that the left side is working (from my angle) but the right is setting the location on the other side but when I change the right side code the left stops working. Thanks
danpost danpost

2018/12/18

#
You have the sign of the offset wrong in line 150.
Will5230 Will5230

2018/12/20

#
Sorry , could you be more clear? I tried to change signs in line 150 but that mixed up both of the sides. thanks
danpost danpost

2018/12/20

#
Will5230 wrote...
Sorry , could you be more clear? I tried to change signs in line 150 but that mixed up both of the sides.
Changing something in one should not effect the other at all. What is your new line 150? Show what you changed.
You need to login to post a reply.