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

2023/2/27

How do can i make a class follow another class around the world?

Rainescap Rainescap

2023/2/27

#
Im new to coding and im making a "game" and i want to have a shield follow around my spaceship for a period of time and absorb damage. any tips on where and how i should start..
danpost danpost

2023/2/27

#
Rainescap wrote...
i want to have a shield follow around my spaceship for a period of time and absorb damage. any tips on where and how i should start..
In Spaceship class:
// field
private Shield shield = new Shield();

// at end of act method (to have shield follow spaceship)
if (shield.getWorld() != null) shield.setLocation(getX(), getY());
}
When shield is activated, add it to the world:
getWorld().addObject(shield, 0, 0);
The Shield objects should control their own time in world.
Rainescap Rainescap

2023/3/3

#
is there anyway i can just press a button and it would pop up. i have a code example.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shield here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shield extends Actor
{
    /**
     * Act - do whatever the Shield wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        shieldUp();
        followHero();
        mortality();
        absorbDamage();
    }
    
    public void shieldUp()
    {
        if(Greenfoot.isKeyDown("g"))
        {
            
        }
    }
    
    public void followHero()
    {
        
    }
    
    public void mortality()
    {
        
    }
    
    public void absorbDamage()
    {
        
    }
}
danpost danpost

2023/3/4

#
Rainescap wrote...
is there anyway i can just press a button and it would pop up. << Code Omitted >>
Yes -- but you would not put it in the Shield class. The only thing the shield should do is absorb damage and remove itself. Use the Spaceship class' act method to put it in:
if (shield.getWorld() == null && Greenfoot.isKeyDown("g")) getWorld.addObject(shield, 0, 0);
Rainescap Rainescap

2023/3/29

#
i have now figured out how to make my shield follow my hero(spaceship) but now i want the shield to be able to absorb damage. how i would i get it started?
danpost danpost

2023/3/29

#
Rainescap wrote...
i have now figured out how to make my shield follow my hero(spaceship) but now i want the shield to be able to absorb damage. how i would i get it started?
Start by adding collision codes into your Shield class. Check for those things that the shield will protect the spacecraft from.
Rainescap Rainescap

2023/3/31

#
how would i start the code for the collision?
danpost danpost

2023/4/1

#
Rainescap wrote...
how would i start the code for the collision?
What type of actors is the Shield instance protecting the spaceship from?
You need to login to post a reply.