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

2023/12/11

Making a Asteroids Game (help needed, 2).

yesineedhelp yesineedhelp

2023/12/11

#
Hello, I ran into another problem I cannot solve for my Asteroid Game, making a working ForceField. Just some basic, when the key, "a," is pressed down, the ForceField is made and follows the Rocket. I can create the ForceField wtih this code:
private static final int ffReloadTime = 100;
private int ffReloadDelayCount;

public void act()
{
ffReloadDelayCount++;      
if(Greenfoot.isKeyDown("a"))
        {
           useForce();
           
        }
}

private void useForce()
{
        if(ffReloadDelayCount >= ffReloadTime)
        {
            ForceField ff = new ForceField();
            getWorld().addObject(ff, getX(), 
            getY());
            
           
            
            ffReloadDelayCount = 0;
            
        }
}
I do have a checkKey method, which I wonder if I should put the "if(Greenfoot.isKeyDown("a")) there. Anyways, the problem with this code is that if "a" is pressed, it creates ForceField; however, it does not follow the Rocket. I know for sure I have to use getX() and getY(), but I'm not sure where to put it. Another problem is then how long the Rocket can use the ForceField, which I think I would just need to create a counter, right? Examples of the code will help me out. Thank you!
danpost danpost

2023/12/11

#
yesineedhelp wrote...
if "a" is pressed, it creates ForceField; however, it does not follow the Rocket. I know for sure I have to use getX() and getY(), but I'm not sure where to put it.
Since the ForceField objects created belong to the Rocket objects that create them, maybe the ForceField class should be inside the Rocket class. Either way, the ForceField object created by a Rocket object should be referenced in the Rocket class and controlled by the Rocket object it belongs to.
Another problem is then how long the Rocket can use the ForceField, which I think I would just need to create a counter, right?
You could, but to avoid clutter, make use of the counter you already have for the forcefield.
You need to login to post a reply.