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

2015/10/16

Manipulating a list of objets

Vellyxenya Vellyxenya

2015/10/16

#
Hi, In my world, I create Magnets:
public void createMagnets()
    {
        if(counter >= 60 && counter< 400 && counter%60 == 0)
        {
            Magnet magnet = new Magnet();
            getWorld().addObject(magnet, this.getX(), this.getY());
        }
    }
It creates 6 magnets and then I have the following code:
public void getMagnets()
    {
        if(counter> 400 && counter%200 == 0)
        {
            List<Magnet> magnets = getWorld().getObjects(Magnet.class);
            //Magnet magnet = magnets.get(Greenfoot.getRandomNumber(magnets.size()));
            Magnet magnet = (Magnet)getWorld().getObjects(Magnet.class).get(Greenfoot.getRandomNumber(magnets.size()));
            magnet.counter2 = vulnerability;
        }
    }
And then in my Magnet class I have this code:
public static int counter2;
    public static int counter = 0;
    private int speed = 2;

    public Magnet()
    {
        setRotation(Greenfoot.getRandomNumber(360));
    }

    public void act()
    {
        if(isPaused() == false)
        {
            counter++;
            counter2--;
            beVulnerable();
            movements();
            bounceAtEdge(speed, counter);
            match();
            takeDamageForBoss((Weapon2.weapon2Damage/10), Weapon2.class);
            takeDamageForBoss(Lightning.damage, Lightning.class);
        }
    }

public void beVulnerable()
    {
        if(this.counter2 <= Boss6.vulnerability && this.counter2 >0)
        {
            this.setImage("Magnet2.png");
        }
        else if(this.counter2<= 0)
        {
            this.setImage("Magnet.png");
        }
    }
The idea is that I want a random magnet among the six to change its image for a while... but with my code all six magnets change their Image. How to fix that please?^^
danpost danpost

2015/10/16

#
Your state of vulnerability is based on the 'counter2' field which is static. As a static field, it is a single field within the class that all objects created from the class can use (the value will be the same for all objects). For each magnet to have a separate field to hold a value specific for that object, the field cannot be declared static. You may need to reassess your other fields that are declared static to see if they should really be as such or not. At the beginning of your post, you said:
Vellyxenia wrote...
Hi, In my world, I create Magnets < Code Omitted >
I understand what you meant; however, it is somewhat ambiguous. When I first read it, I was thinking that the code what placed in your world subclass (which is usually where actors are initially added into the world), where you meant that the magnets were created and placed into your world. Now, I wonder why you have the code in an Actor subclass. Why, when, how, and where is this actor that adds magnets added into the world? What behavior and state does this actor have? There may be a much easier way to accomplish what you are doing. For possible assistance, post the code for your World subclass and also for the Actor subclass where the code that adds magnets into the world is located.
Vellyxenya Vellyxenya

2015/10/16

#
Hi and thank you for you answer (I finally understand what static mean :D In fact, at a given moment of the game, my sixth Boss spawn, which is basically only an actor that can take damage and when he dies, the game continues. The particularity of this boss is that he periodically creates magnets which create like an electricty field that the player must juke. I wanted to change the image of a single magnet to make it harder: in fact, to damage the Boss, you must first damage the magnets; and only the magnet whose image is changed is vulnerable to the damage. I put the code which creates the magnets in the boss class to structure my code (it'll be up to 10 bosses with a lot of code)... so if i put it in the world class it'll be even more messy there^^ I just took the "static" away from my vulnerability class and it works perfectly! And yes like you said, i put almost all my variables static, everything seems to work but gonna try to reevaluate each variable the next days Again, thank you very much ! PS: The only part in my world subclass that's related to the boss 6 is:
public void createBoss6()
    {
        if(Counter == 120 && bossCreated == false)
        {
            Boss6 boss6 = new Boss6();
            addObject(boss6, getWidth(), getHeight()/2);
            bossCreated = true;
        }
    }
You need to login to post a reply.