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

2019/11/27

enemy throwing objects to the left and right

MsWoof MsWoof

2019/11/27

#
Hey everybody! I'm fairly new to Greenfoot and I'm currently trying to program a Super Mario game where an enemy is capable of moving to the left and right on the ground while throwing balls to the left and to the right. My enemy can move around, but somehow throwing those balls at Mario isn't working out. They all fly to the right side. Could anyone please check my code and maybe tell me an easy fix that I'll be able to understand at my low skill level? Thanks in advance! This is the code for the ball:
public class Schneeball extends Gegner
{
    private int a;
    public void act() 
    {  
        if (Greenfoot.getRandomNumber(10) < 5) {
            setRotation(0);
            move(a);
        } else {
            setRotation(180);
            move(-a);
        }
        a++;
        if(isAtEdge())
        {
            getWorld().removeObject(this);

        }
    }
}
And this is the code for the enemy:
public class Häschen extends Gegner
{
    private int speed = 10;        //Legt Geschwindigkeit fest
    private int leftTurn = 50;     //Legt die Umkehrpunktefest
    private int rightTurn = 1250;

    int count = 0;
    int count2 = 0;

    private GreenfootImage Häschen1 = new GreenfootImage("HäschenGegner100.png");
    private GreenfootImage Häschen2 = new GreenfootImage("HäschenGegner100_2.png");

    public boolean atTurningPoint()
    {
        return(getX() <= leftTurn || getX()>= rightTurn);
    }

    public void act() 
    {
        setLocation (getX() + speed, getY() );

        if (atTurningPoint() )
        {
            speed = - speed; 
        }

        count++;
        count2++;
        changeFoot();

        if (count2 > 50) { //erzeugt Schneebälle
            Schneeball shot = new Schneeball();
            shot.setRotation(this.getRotation());
            getWorld().addObject(shot, this.getX(), this.getY());
            count2 = 0;

        }

    }  

    //Hier wird nicht mit For-Schleife gearbeitet, da sonst keine andere Aktion!
    public void changeFoot()
    {
        if(count<5)
            setImage(Häschen2);
        else{
            if(count<10)
            {
                setImage(Häschen1);  //Lässt am Wendepunkt das Bild spiegeln
            }
            else
            {
                count = 0;   
            }
        }
    } 

}
danpost danpost

2019/11/27

#
MsWoof wrote...
My enemy can move around, but somehow throwing those balls at Mario isn't working out. They all fly to the right side. << Code Omitted >>
In your Schneeball class, replace lines 6 through 12 (or 13) with:
if (Greenfoot.getRandomNumber(10) < 5) setLocation(getX()+a, getY());
MsWoof MsWoof

2019/11/28

#
Thanks for your quick help! Unfortunately, then the snowballs stop moving completely. The enemy just drops them on the floor. :/
public class Schneeball extends Gegner
{
    private int a;
    public void act() 
    {  
        if (Greenfoot.getRandomNumber(10) < 5) setLocation(getX()+a, getY());
        if(isAtEdge())
        {
            getWorld().removeObject(this);

        }
    }
} 
danpost danpost

2019/11/28

#
MsWoof wrote...
Thanks for your quick help! Unfortunately, then the snowballs stop moving completely. The enemy just drops them on the floor. :/ << Code Omitted >>
What code do you have in the Gegner class?
MsWoof MsWoof

2019/11/28

#
It's empty:
public class Gegner extends Actor
{
    /**
     * Act - tut, was auch immer Gegner tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    public void act() 
    {
        // Ergänzen Sie Ihren Quelltext hier...
    }    
}
danpost danpost

2019/11/28

#
MsWoof wrote...
It's empty: << Code Omitted >>
Drats ... I should have seen it. Your Häschen objects never have anything but zero rotation. So, line 33 in that class does nothing to the rotation of the Schneeball objects. You need to impart a condition to change their rotation to 180 -- something like checking for a negative speed value. Sorry I did not spot that earlier.
MsWoof MsWoof

2019/11/29

#
Thank you for your help. :) How could I check for a negative speed value? I'm afraid that's beyond my current coding skills. ;)
danpost danpost

2019/11/29

#
MsWoof wrote...
Thank you for your help. :) How could I check for a negative speed value? I'm afraid that's beyond my current coding skills. ;)
No it is not. It is coded pretty much in the way you would state it -- "if speed is less than zero, then take the new shot and set its rotation to 180".
MsWoof MsWoof

2019/11/29

#
Thank you for your patience :). I'm still stumped, even though your description makes sense ... I tried changing it to
        if (count2 > 50) { //erzeugt Schneebälle
            Schneeball shot = new Schneeball();
            if (speed < 0) {
                shot.setRotation(180);
            }
            getWorld().addObject(shot, this.getX(), this.getY());
            count2 = 0;
        }
... but the snowballs still only get dropped and aren't thrown.
danpost danpost

2019/11/29

#
You will need to assign a positive value to the a field in your Schneeball class. Also, remove the random condition to move and use:
move(a);
instead of:
setLocation(getX()+a, getY());
MsWoof MsWoof

2019/12/1

#
Thanks! :) You made my day! It's working now, hooray!
You need to login to post a reply.