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

2012/12/13

adding a shield

1
2
3
davemib123 davemib123

2012/12/13

#
how would I go about implementing a shield in my game? I have included a boolean on the hero to activate the shield but not too sure on the collision and health bar workings. link to my game: http://www.greenfoot.org/scenarios/5799
UnclePedro UnclePedro

2012/12/13

#
If you want the shield to slowly damage and then break over time, you should go to your ship's health method and use your boolean variable to make the ship's health not go down, and instead make the shield's "health" go down. When the shield runs out of health, you can set the boolean variable to false, and maybe show that the shield broke. To determine if the shield is being hit, just use getOneIntersectingObject()... If you want the shield to only last a certain amount of time, but have no "health", use your boolean variable in the same place ^^ and use a timer. At the end of the timer, show the shield has stopped working and set the boolean variable to false. Fun game btw :p
vonmeth vonmeth

2012/12/13

#
Add checks for that boolean. If that boolean is true, don't let certain things happen (like take a hit). For example to make it so enemy fire doesn't harm your Hero. First make your boolean shieldActive static.
    public void checkHeroCollision(){
        Actor collided = getOneIntersectingObject(Hero.class);
        if (collided != null){
            City cityWorld = (City) getWorld();  // get a reference to the world
            if(!Hero.shieldActive){ // is your hero shield not active? Then take damage
            Bar bar = cityWorld.getBar();  // get a reference to the counter
            bar.subtract(10);
            }
            getWorld().addObject(new explosionSmall(), getX(), getY());
            getWorld().removeObject(this);
            return;
        } else {
            if (getX() <= - outOfBoundary) {
                getWorld().removeObject(this);
            } 
        }
    }
Just include an else after the 'if' if you wish for the shield to have health, and subtract from it for each hit.
davemib123 davemib123

2012/12/13

#
thanks for the replies. Vonmeth that works very nicely. UnclePedro i'll try and implement a timer
davemib123 davemib123

2012/12/13

#
this is what I have but the shield doesnt go away, any ideas?:
    static boolean shieldActive; 
    private int shieldCounter = 0;

    public void shield(){
        shieldActive = true; 
        setImage("shield.jpg");
        shieldCounter ++; 
        if (shieldCounter == 100) {

            setImage("Hero.png");
            shieldCounter = 0;
            shieldActive = false;
        } 
    }
vonmeth vonmeth

2012/12/13

#
I'm guessing the shield method is only called when a button is pressed? If so, it is only going to be counting up each time the shield method is called. try something like this:
//in the act method
	if(shieldCounter>0) shieldCounter--;
	if(shieldCounter==0) {
		setImage("Hero.png");
		shieldActive = false;
		}
		
// this is called once
public void shield(){  
    shieldActive = true;   
    setImage("shield.jpg");  
    shieldCounter = 100;   
}  
davemib123 davemib123

2012/12/13

#
currently it is set on click of a button, will make it as a powerup come across the screen
davemib123 davemib123

2012/12/13

#
I have this in my powerup2 class to check a collision between the powerup and the hero:
    public void checkHeroCollision(){
        Actor collided = getOneIntersectingObject(Hero.class);
        if (collided != null){
            Hero.shield();
            getWorld().removeObject(this);
            return;
        } else {
            if (getX() <= - outOfBoundary) {
                getWorld().removeObject(this);
            } 
        }
    }
but I get "non-static method cannot be referenced from a static context" error
vonmeth vonmeth

2012/12/13

#
You need to make your shield method static. "public static void shield()"Here is a bit on what static exactly does.
davemib123 davemib123

2012/12/13

#
I understand what is meant by static final - it keeps the value as a final and therefore can not change it. Not sure on "static" by itself. I tried your suggestion but it give setImage("shield.jpg"); shieldCounter = 300; the same static error.
vonmeth vonmeth

2012/12/13

#
Ah, I still have some things to learn about static myself! Forgot that I read only certain things can only happen in a static method. You are trying to change non-static items in a static method.
vonmeth vonmeth

2012/12/13

#
It would be better simply to do this, instead of making the method static.
public void checkHeroCollision(){  
    Hero collided = (Hero) getOneIntersectingObject(Hero.class);  
    if (collided != null){  
        collided.shield();  
        getWorld().removeObject(this);  
        return;  
    } else {  
        if (getX() <= - outOfBoundary) {  
            getWorld().removeObject(this);  
        }   
    }  
}  
vonmeth vonmeth

2012/12/13

#
Static means that variable/method is shared between all instances of that class. There is only one instance of that static variable/method, instead of multiple created for each instance of that class. What we attempted to do from a static method, was change a non-static class member. A static method can't access non-static members (at least, not directly.) It would simply not know which instance of the class you are wishing to change specifically. That was probably an unclear explanation, sorry.
davemib123 davemib123

2012/12/14

#
many thanks for the explanation it sort of clears it up in my head :D
davemib123 davemib123

2012/12/14

#
vonmeth wrote...
Add checks for that boolean. If that boolean is true, don't let certain things happen (like take a hit). For example to make it so enemy fire doesn't harm your Hero. First make your boolean shieldActive static.
    public void checkHeroCollision(){
        Actor collided = getOneIntersectingObject(Hero.class);
        if (collided != null){
            City cityWorld = (City) getWorld();  // get a reference to the world
            if(!Hero.shieldActive){ // is your hero shield not active? Then take damage
            Bar bar = cityWorld.getBar();  // get a reference to the counter
            bar.subtract(10);
            }
            getWorld().addObject(new explosionSmall(), getX(), getY());
            getWorld().removeObject(this);
            return;
        } else {
            if (getX() <= - outOfBoundary) {
                getWorld().removeObject(this);
            } 
        }
    }
Just include an else after the 'if' if you wish for the shield to have health, and subtract from it for each hit.
query here, I want to the explosions still to happen regardless of the shield active or not. I've tried moving the boolen check for sheildActive below the explosion but it says that the object is not in the world
There are more replies on the next page.
1
2
3