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

2013/6/3

How to make an actor recognize different objects and either add or subtract points based on the object.

1
2
Nsalhan01 Nsalhan01

2013/6/3

#
Hi so im having some trouble with my game. It won't allow me to compile because it says that I already defined the actor once, and when i remove my Actor actor = getOneIntersectingObject(Junk.class); portion of the code it says that if(actor != null) is an unreachable statement. Also I know that it adds points when an apple falls, but I don't think i formated it correctly for subtracting points when junk falls. Thank you!
public int checkCatch()
    {
        Actor actor = getOneIntersectingObject(Apple.class); 
                      
        if(actor != null)
        {           
            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return 100;

        } else
            return 0;
        
        Actor actor = getOneIntersectingObject(Junk.class);    
        if(actor != null) 
        {
            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return -100;

        } else
            return 0;
            
    }
Line 14, you made a new Actor called actor... You can't have the same variable name twice in a local block of code (e.g. methods/loops). Name it something else like "actor2" or "junk"
Nsalhan01 Nsalhan01

2013/6/3

#
I made it Junk junk = getOneIntersectingObject(Junk.class); but now they're saying incompatible type - found greenfoot.Actor but expected junk. I then tried it with actor 2 and they said that they couldn't find an actor2. I want to make one actor recognize both objects.
1. getOneIntersectingObject() returns an actor, no matter what .class you put in there. There's two ways to solve your problem:
//1.
Junk junk = (Junk)getOneIntersectingObject(Junk.class); //If you don't know what this does, it changes the result of getOneIntersectingObject from an Actor object to a Junk object.

//2.
Actor junk = getOneIntersectingObject(Junk.class)  //Just a different variable name, not different Object.
2. I don't understand quite what you're saying. It seems that you said to remove (or use) actor2, but you never declared a variable named actor2 (maybe it was named junk instead?)
Nsalhan01 Nsalhan01

2013/6/4

#
Sorry, here let me show you what my code looks like now. The point to my game is that theres an actor and two objects. When the apple object falls the actor catches it then he gains point, but if he catches the junk object then he looses points.
    
public int checkCatch()
    {
        Actor actor = getOneIntersectingObject(Apple.class); 
                      
        if(actor != null)
        {
            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return 100;

        } else
            return 0;
        
        Actor junk = getOneIntersectingObject(Junk.class); 
        if(actor != null) 
        {
            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return -100;

        } else
            return 0;
            
    }
This still isn't working, they said junk = getOneIntersectingObject(Junk.class); is an unreachable statement
Line 17 through line 21: Anytime you say "actor" you should replace that with "junk"
Nsalhan01 Nsalhan01

2013/6/4

#
I did that but they're still saying the same thing. Sorry, I don't mean to be a bother.
public int checkCatch()
    {
        Actor actor = getOneIntersectingObject(Apple.class); 
                      
        if(actor != null)
        {

            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return 100;

        } else
            return 0;
        
        Actor junk = getOneIntersectingObject(Junk.class); 
        if(actor != null) 
        {

            getWorld().removeObject(junk);
            Greenfoot.playSound("khowb.wav");
            return -100;

        } else
            return 0;
            
    }
What's saying the same thing? An error? Something else?
Nsalhan01 Nsalhan01

2013/6/4

#
They're still saying junk = getOneIntersectingObject(Junk.class); is an unreachable statement
And you're not really a bother, just a new programmer trying to learn how to make a game. I asked lots of questions before I made a good game (Not on this site, to my teacher and peers). A lot of people have trouble with programming when they first start doing it. Game designing is even harder. But once you learn how to solve a problem, you'll know what to do if you run into it again, or how to avoid it in the future. The most part of programming knowledge comes from experiences/problems with previous programming.
Ah, because you have the following code
if (actor != null)
...
else
return 0;
...
meaning if actor is equal to null, the code will return 0, and will never, ever, EVER reach the next if statement:
if (junk != null)
...
Just erase your code else and return lines.
Nsalhan01 Nsalhan01

2013/6/4

#
Thank you so much! One problem fixed now onto the next. So I wanted to subtract 100 points if he catches the junk object but I don't think i formatted it correctly. My last } was highlighted and it said that i'm missing a return statement.....
   public int checkCatch()
    {
        Actor actor = getOneIntersectingObject(Apple.class); 
                      
        if(actor != null)
        {

            getWorld().removeObject(actor);
            Greenfoot.playSound("khowb.wav");
            return 100;

        } 
        
        Actor junk = getOneIntersectingObject(Junk.class); 
        if (junk != null) 
        {

            getWorld().removeObject(junk);
            Greenfoot.playSound("khowb.wav");
            return -100;

        }
            
    }
yep, you have two if statements that both return something different. But what happens if neither of them are true? What will the method return? You need to add a return statement at the end of the method (in case either of the if statements don't return something). I'm guessing you want it to return 0, since nothing has happened.
Nsalhan01 Nsalhan01

2013/6/4

#
OMG thank you so much! Everything works now :) You don't know how much this helped my game!
Your welcome. I love helping other programmers figure out their problems. :)
There are more replies on the next page.
1
2