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

2013/12/5

Cannot Find canSee Method

UltimateCoder UltimateCoder

2013/12/5

#
Hey guys, I am new to coding but I am making a game where there is a game about a ball and the paddle. If the ball touches the paddle, the ball needs to disappear so I used an eat method. It says cannot fin canSee. Here is my code so far: /** * Write a description of class Paddle here. * * @author (your name) * @version (a version number or a date) */ public class Paddle extends Actor { public void act() { CheckKeys(); lostBall(); } public void CheckKeys() { if (Greenfoot.isKeyDown("left")) { move(-5); } if (Greenfoot.isKeyDown("right")) { move(5); } } public void lostBall() { if (canSee(Ball.class)); { eat(Ball.class); } } } I am only 13, so please explain what to do in simple terms and if there is any other way for the ball to disappear when it touches the paddle, please let me know!
Gevater_Tod4711 Gevater_Tod4711

2013/12/5

#
The problem is that the method canSee is not visible in your Paddle class. The easiest way to fix this is just adding the method to your class.
1
2
3
4
5
6
7
8
9
10
11
public boolean canSee(Class clss) {
    return getOneObjectAtOffset(0, 0, clss) != null;
}
 
//if there is the same problem with the eat method you can also add this method;
public void eat(Class clss) {
    Actor actor = getOneObjectAtOffset(0, 0, clss);
    if(actor != null) {
        getWorld().removeObject(actor);
    }
}
UltimateCoder UltimateCoder

2013/12/5

#
Thanks, but where should I add this. I know I seem like an idiot, but I am a teenager!
UltimateCoder UltimateCoder

2013/12/5

#
Also, the 'eat' method it cannot find. Could someone tell me the code for the 'eat' method|?
UltimateCoder UltimateCoder

2013/12/5

#
Could someone please give me the code so the paddle can eat the ball.
Gevater_Tod4711 Gevater_Tod4711

2013/12/5

#
You can add this code anywhere in your class (not in another method but everywhere else). Try adding the methods underneath the act method. I already gave you the code for the eat method in the first post.
UltimateCoder UltimateCoder

2013/12/5

#
The code is compiling but the paddle does not eat the ball. Here is the code: { public void act() { CheckKeys(); } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } public void CheckKeys() { if (Greenfoot.isKeyDown("left")) { move(-5); } if (Greenfoot.isKeyDown("right")) { move(5); } } }
Gevater_Tod4711 Gevater_Tod4711

2013/12/5

#
You have to call your lostBall method from your act method like you did in the first code you posted.
UltimateCoder UltimateCoder

2013/12/5

#
could you please copy my previous code and insert your code correctly inside my code. Thank You
Gevater_Tod4711 Gevater_Tod4711

2013/12/5

#
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
public class Paddle extends Actor {
 
    public void act() {
        CheckKeys();
        lostBall();
    }
 
    public boolean canSee(Class clss) { 
        return getOneObjectAtOffset(0, 0, clss) != null
    
       
    public void eat(Class clss) { 
        Actor actor = getOneObjectAtOffset(0, 0, clss); 
        if(actor != null) { 
            getWorld().removeObject(actor); 
        
    
 
    public void CheckKeys() {
       if (Greenfoot.isKeyDown("left")) {
          move(-5);
       }
       if (Greenfoot.isKeyDown("right")) {
          move(5);
       }
    }
 
    public void lostBall() {
        if (canSee(Ball.class)); {
            eat(Ball.class);
        }
    }
}
UltimateCoder UltimateCoder

2013/12/5

#
Thanks a lot!!!! It finally works :)
ovirahman ovirahman

2014/11/3

#
Thanks. was facing the same problem .
You need to login to post a reply.