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

2013/2/14

Why isn't it working?

Korgaz Korgaz

2013/2/14

#
Hey guys, i have to program something for school and i want to add a shield, after the Starship collected a powerup, but its simply not working. Could you help me pls???
import greenfoot.*; 

public class Starship extends Actor
{ 
    private int count = 2;
    private boolean gotPowerup1=false;
    private boolean gotPowerup2=false;
    private boolean gotPowerup3=false;

    public void act()
    {
        move(0);
        checkKeys();        
        Powerup();

    }

    /**
     * Press Keys to Move
     * getOneObjectAtOffset (x,y,Name.class) == null     prüft ob Objekt der Klasse neben Starship ist
     */
    public void checkKeys()
    {   
        if (Greenfoot.isKeyDown("left"))
        {setLocation(getX() -6,getY());
        }

        if (Greenfoot.isKeyDown("right")
        && getOneObjectAtOffset(5,0, Menu.class) == null) // wenn rechts Menu dann stoppe
        {setLocation(getX() +6,getY());
        }

        if (Greenfoot.isKeyDown("up"))
        {setLocation(getX(),getY() -6);
        }

        if (Greenfoot.isKeyDown("down"))
        {setLocation(getX(),getY() +6);
        }

        if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
    }

    public void fire()
    { Bullet bullet = new Bullet();                         //schießen (object create)
        getWorld().addObject(bullet,getX(),getY());         
        bullet.setRotation(-90);//objektrichtung anch oben richten
    }

    public void getShield()
    {
        count--;
        if (count == 0) 
        {getWorld().addObject ( new Shield(), getX(), getY());
            count=2;
        }
    }

    public void Powerup()
    {
        Actor Powerup1 = this.getOneObjectAtOffset (1,1, Powerup1.class); //Code verkürzen
        Actor Powerup2 = this.getOneObjectAtOffset (1,1, Powerup2.class); //Code verkürzen
        Actor Powerup3 = this.getOneObjectAtOffset (1,1, Powerup3.class); //Code verkürzen 
        if (Powerup1 != null)
        {
            getWorld().removeObject(Powerup1); //Powerup1 entfernen

        }

        else if (Powerup2 != null)
        {
            getWorld().removeObject(Powerup2); //Powerup2 entfernen
            gotPowerup2 = true;
        }

        else  if (Powerup3 != null)
        {
            getWorld().removeObject(Powerup3); //Powerup3 entfernen

        }
    }

    public void Powerup2_activated()
    {if (gotPowerup2 =true)
        {getShield();
        }

    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/2/14

#
It isn't working because you never call any method that could activate your shields. You have to call a method that checks whether the shield should be activated. Probably the method public void Powerup2_activated() should be called from the act method. And there is a mistake in the method Powerup2_activated() In line 87. It should be if (gotPowerup2 == true) or if (gotPowerup2).
Korgaz Korgaz

2013/2/14

#
Thanks dude! got it to work with your help now. And i got another question, how do i spawn objects at the edge of the world (top), and let them "fall" down? so it's like Asteroids where you have to shoot the asteroids coming from the top. i don't want them to spawn permanently, but in intervals.
danpost danpost

2013/2/14

#
The falling is simply a matter of increasing the y-coordinate of the object each act cycle by some amount (call it 'speed'): setLocation(getX(), getY()+speed); The spawning should be done from in the world class code using 'addObject(new ObjectToSpawn(), Greenfoot.getRandomNumber(getWidth()), 0); To spawn in intervals, you will need a field to track how many need to be spawned, decreasing it as each one is spawned. If it is zero, do not spawn until all have been removed from the world; then reset the number to be spawned to a new value to start the next barrage. You could add another field to track the number of barrages and determine how many to spawn from that number (increasing the number of spawnings on each successive barrage).
Korgaz Korgaz

2013/2/14

#
Ah I see, thank you! This help in the forum is just great. I really appreciate that.
You need to login to post a reply.