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

2013/10/31

Collision problem

1
2
Baenefin Baenefin

2013/10/31

#
I would like my alice to detect collision on mutiple Shroom classes. But I can;t get it right. Any help will be appreciated. Is there a way to detect collision for all the shrooms in 1 method?
public void checkCollisionShroom()
    {
        Actor collided = getOneIntersectingObject(Shroom.class);
        if (collided !=null)
        {
            Greenfoot.playSound("ShroomPickup.wav");
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
            getWorld().removeObject(collided); 
        }
        getOneIntersectingObject(shroom2.class);
        if (collided !=null)
        {
            Greenfoot.playSound("ShroomPickup.wav");
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
            getWorld().removeObject(collided); 
        }
        getOneIntersectingObject(shroom3.class);
        if (collided !=null)
        {
            Greenfoot.playSound("ShroomPickup.wav");
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
            getWorld().removeObject(collided); 
        }
    }
danpost danpost

2013/10/31

#
Using a 'for' loop should do the trick:
for (Actor shroom : getIntersectingObjects(Shroom.class))
{
    getWorld().removeObject(shroom);    
    Greenfoot.playSound("ShroomPickup.wav");
    ((ShroomScore)getWorld().getObjects(ShroomScore.class).get(0)).add(1);
    delay(30); // adjust or remove
}
Anytime you need to do the same thing repeatedly, look for ways to incorporate a loop.
Baenefin Baenefin

2013/10/31

#
So I implement it like this?
 public void checkCollisionShroom()
    {

        for (Actor shroom : getIntersectingObjects(Shroom.class))
        {
            Greenfoot.playSound("ShroomPickup.wav");
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
            getWorld().removeObject(collided); 
            delay(30);
        }
        for (Actor shroom : getIntersectingObjects(shroom2.class))
        {
            Greenfoot.playSound("ShroomPickup.wav");
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
            getWorld().removeObject(collided); 
        }
}
danpost danpost

2013/10/31

#
If you had a superclass for all shroom classes you could use one loop using that superclass name.
Baenefin Baenefin

2013/10/31

#
I have not used for loops yet. But if I use the code as above it says Shroom.class is an incompatible class Would this work as a superclass for shroom?
public class Shrooms extends Objects
{

     GreenfootImage one = new GreenfootImage("Shroom.png");
     GreenfootImage two = new GreenfootImage("shroom2.png");
     GreenfootImage three = new GreenfootImage("shroom3.png");
     GreenfootImage four = new GreenfootImage("shroom4.png");
     GreenfootImage five = new GreenfootImage("shroom5.png");
     
danpost danpost

2013/10/31

#
NO. public class Shrooms extends Actor public class Shroom extends Shrooms public class shroom2 extends Shrooms public class shroom3 extends Shrooms etc.
Baenefin Baenefin

2013/10/31

#
Ok tgat is done...so how do I acces them in the for loop? I am sorry if i don't grasp it. it's really late at night.
danpost danpost

2013/10/31

#
Just substitute 'Shrooms.class' for 'Shroom.class' in the 'for' loop code I supplied above.
Baenefin Baenefin

2013/10/31

#
public void checkCollisionShroom()
    {
        for (Actor shroom : getIntersectingObjects(Shrooms.class))  
        {  
            getWorld().removeObject(shroom);      
            Greenfoot.playSound("ShroomPickup.wav");  
            ((ShroomScore)getWorld().getObjects(ShroomScore.class).get(0)).add(1);  
            delay(30); // adjust or remove  
        }  
    }
'Shroom' is an incompatible type it says
danpost danpost

2013/10/31

#
What is being highlighted when you get this message? I do not see 'Shroom' anywhere.
Baenefin Baenefin

2013/10/31

#
danpost wrote...
Just substitute 'Shrooms.class' for 'Shroom.class' in the 'for' loop code I supplied above.
I posted the wrong code Line 3 Shroom.class is incompatable
    public void checkCollisionShroom()
    {
        for (Actor shroom : getIntersectingObjects(Shroom.class))  
        {  
            getWorld().removeObject(shroom);      
            Greenfoot.playSound("ShroomPickup.wav");  
            ((ShroomScore)getWorld().getObjects(ShroomScore.class).get(0)).add(1);  
            delay(30); // adjust or remove  
        }  
}
danpost danpost

2013/10/31

#
What is wrong with the previous code you posted (the one with 'Shrooms.class')
Baenefin Baenefin

2013/10/31

#
also incompatible
danpost danpost

2013/10/31

#
You may need to change it to the following (because getIntersectingObjects returns a list of Object objects and not a list of Actor objects):
public void checkCollisionShroom()
{
    for (Object obj : getIntersectingObjects(Shrooms.class))  
    {
        Actor shroom = (Actor) obj;
        getWorld().removeObject(shroom);      
        Greenfoot.playSound("ShroomPickup.wav");  
        ((ShroomScore)getWorld().getObjects(ShroomScore.class).get(0)).add(1);  
        delay(30); // adjust or remove  
    }  
}
Baenefin Baenefin

2013/10/31

#
And that was the problem! So now I can make some more if loops, thanks danpost.
There are more replies on the next page.
1
2