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

2013/3/18

Eating an object bug

1
2
Gingervitis Gingervitis

2013/3/18

#
I am not sure why this is happening, but when I want my actor to eat another actor, it only works when my actor has turned. Specifically, it only removes the object when you turn the actor when it is right over the actor you want to be removed. Is there a reason for this? I didn't have this problem before.
danpost danpost

2013/3/18

#
Show the code for your actor.
Gingervitis Gingervitis

2013/3/18

#
Which one? The one that does the eating?
danpost danpost

2013/3/18

#
Yes. That one.
Gingervitis Gingervitis

2013/3/18

#
Here it is. It is a subclass of the 'animal' class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Turtle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Turtle extends Animal
{
    private int timer = 100;  
    private int counter;
    private boolean isBlasterType2;
    private int debrisPickedUp;
    private int counter1;
    public void act()  
    {  
        ifInIntro();
        ifInMainMenu();
        ifInLevel1();
        ifInLevel1_1();
    }  

    public void ifInIntro()
    {
        World world = getWorld();

        if (world instanceof Intro)
        {
            if(timer==0)  
            {  
                move(2);  
                if(getRotation() == 0 && getX()>=355) timer = 136;  
                if(getRotation() == 180 && getX()<=202) Greenfoot.setWorld(new MainMenu());  
            }  
            else  
            {  
                timer--;  
                if(getX()>=355 && timer<36) setRotation(getRotation()-5);  
            }

            if (canSee(Turtle.class))
            {
                eat(Turtle.class);
            }
        }
    }

    public void ifInMainMenu()
    {
        World world;
        world = getWorld();

        if (world instanceof MainMenu)
        {
            counter++;
            timer = 100;
            if (counter == 1)
            {
                turnTowards(300, 0);
                counter++;
            }

            if (Greenfoot.isKeyDown("up"))
            {
                move(3);
            }

            if (Greenfoot.isKeyDown("down"))
            {
                move(-3);
            }

            if (Greenfoot.isKeyDown("left"))
            {
                turn(-5);
            }

            if (Greenfoot.isKeyDown("right"))
            {
                turn(5);
            }

            if (getX() >= 245 && getX() <= 365 && getY() <= 115)
            {
                Greenfoot.delay(75);
                Greenfoot.setWorld(new Level1());
            }
        }
    }

    public void ifInLevel1()
    {
        World world = getWorld();

        if (world instanceof Level1)
        {
            move(3);      
            if (Greenfoot.isKeyDown("left"))
            {
                turn(-5);
            }

            if (Greenfoot.isKeyDown("right"))
            {
                turn(5);
            }

            if (Greenfoot.isKeyDown("x"))
            {
                turn(25);
                shoot();
                shoot();
                shoot();  
            }

            String key=Greenfoot.getKey();  
            if (key == null) return;  
            if ("z".equals(key) ) isBlasterType2= !isBlasterType2;  
            if ("space".equals(key)) shoot();

            Actor arrow = getOneIntersectingObject(Arrow.class); 
            if (arrow != null ) 
            {

                world.removeObjects(world.getObjects(Rock.class));
                world.removeObjects(world.getObjects(Text.class));
                world.removeObjects(world.getObjects(Arrow.class));
                setLocation(300, 490);
                Debris debris = new Debris();
                world.addObject(debris, 83, 449);

                Debris debris2 = new Debris();
                world.addObject(debris2, 246, 400);

                Debris debris3 = new Debris();
                world.addObject(debris3, 387, 342);

                Debris debris4 = new Debris();
                world.addObject(debris4, 532, 387);

                Debris debris5 = new Debris();
                world.addObject(debris5, 408, 195);

                Debris debris7 = new Debris();
                world.addObject(debris7, 497, 99);

                Debris debris8 = new Debris();
                world.addObject(debris8, 300, 99);

                Debris debris9 = new Debris();
                world.addObject(debris9, 135, 93);

                Debris debris10 = new Debris();
                world.addObject(debris10, 114, 275);
            }

            Actor deb = getOneIntersectingObject(Debris.class);
            if (canSee(Debris.class))
            {
                eat(Debris.class);
                debrisPickedUp++;
                if(debrisPickedUp ==9) 
                {
                    ArrowDown arrowd = new ArrowDown();
                    world.addObject(arrowd,300,470);                    
                    debrisPickedUp++;
                }
            }

            if (canSee(ArrowDown.class))
            {
                Greenfoot.setWorld(new Level1_1());
            }
        }       
    }

    public void ifInLevel1_1()
    {
        World world = getWorld();
        if (world instanceof Level1_1)
        {
            
            move(3);      
            if (Greenfoot.isKeyDown("left"))
            {
                turn(-5);
            }

            if (Greenfoot.isKeyDown("right"))
            {
                turn(5);
            }
            String key=Greenfoot.getKey();
            if ("space".equals(key)) shoot2();
        }
    }

    public void shoot()
    {
        Actor blaster = null;  
        if (isBlasterType2)   
        {  
            blaster = new Bullet2();  
            blaster.setImage("BBPurge.png");   
        }  
        else  
        {  
            blaster = new Bullet();  
        }  

        blaster.setRotation(getRotation());  
        getWorld().addObject(blaster, getX(), getY());  
    }

    public void shoot2()
    {
        DebrisBullet dbullet = new DebrisBullet();
        getWorld().addObject(dbullet, getX(), getY());
        dbullet.setRotation(getRotation());
    }

    public void tryToEat()
    {
        if (canSee(Turtle.class))
        {
            eat(Turtle.class);
        }
    }

    public void createNewBullet()
    {
        Bullet b;
        b = new Bullet();

        World world;
        world = getWorld();

        world.addObject(b,getX() ,getY() );
    }

    /**
     * Method createNewCRock
     *
     */
    public void createNewCRock()
    {
        Universal cr;
        cr = new Universal();

        World world;
        world = getWorld();

        int x = getX();
        int y = getY();
        cr.setImage("CRock.png");
        world.addObject(cr,475,441);
    }
}
danpost danpost

2013/3/18

#
Which world is active and what is the size of the world?
Gingervitis Gingervitis

2013/3/18

#
the Level 1 world.... super(600, 500, 1);
danpost danpost

2013/3/18

#
Try removing line 118. You can get away with this as you are comparing Strings explicitly to the value returned from 'Greenfoot.getKey()'.
Gingervitis Gingervitis

2013/3/19

#
That worked! What exactly from that line caused the issue?
danpost danpost

2013/3/19

#
If no key was detected, the rest of the method was being ignored (the 'return' statement causes an immediate exit out of the method).
Gingervitis Gingervitis

2013/3/19

#
Oh okay. Thank you. it a previous discussion a couple weeks ago, you said you would help me go back to a world that the actor was in, but it has different things..... Can you help me now? After not programming for two weeks, I just finished level 1 today.
danpost danpost

2013/3/19

#
This was the 'MainMenu' world. It that correct? I may need reminding as to the specifics, but sure.
Gingervitis Gingervitis

2013/3/19

#
Yes. If you help me with MainMenu world, would I be able to use that as an example for other worlds? Or would something like be different for each world?
danpost danpost

2013/3/19

#
You should be able to use the one class to either (1) use the same world between all levels; or (2) use a different instance of that world between all levels. I would think the only differences would be the current score and the level number; both of which can be passed to the menu world from the calling level world; and then the score can be passed to the next level world. If your main character has references to all the main bits of information that need passed, just pass the actor itself.
Gingervitis Gingervitis

2013/3/19

#
Ok........ Where do we begin? Would you like me to re-post my game so we are on the same page? Then I will remove it.
There are more replies on the next page.
1
2