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

2018/3/21

My actor gets moved to the right edge if it hits the left edge

Recorsi Recorsi

2018/3/21

#
Hello, I made an actor called "mothership", it's something like a boss enemy. It constantly moves to the left till it touches the left edge then i remove it with
if (getX() <= 1) 
       {
           canShoot = false;
           getWorld().removeObject(this);
       }
But since i added this code to my world class, it just gets moved to the right egde again and i don't know why:
private boolean MSSpawned = false;
    int alertTimer = 3*60;
    int MSSoundTime = 20*60;
    GreenfootSound MothershipSound = new GreenfootSound("Mothership.mp3");
    GreenfootSound MothershipAlarm = new GreenfootSound("MothershipAlarm.mp3");
private void addMothership()
    {
        if (!MSSpawned && Greenfoot.getRandomNumber(2500) == 1)
        {
            MSSpawned = true;
            MothershipAlarm.play();
            addObject(msalert, 470, 300);
        }
        if (MSSpawned)
        {
            alertTimer--;
        }
        if (alertTimer <= 0)
        {
            removeObject(msalert);
            MothershipAlarm.stop();
            addObject(mothership, 895, 450);
            MothershipSound.play();
        }
        if (MSSpawned && alertTimer <= 0) 
        {
            MSSoundTime--;
        }
        if (MSSoundTime <= 0)
        {
            MothershipSound.stop();
        }
    }
Maybe you can find the error. Thanks :)
danpost danpost

2018/3/21

#
There seems to be excess if blocks in the code (or, at least, conditions that are asked about multiple times). I am not saying that is the cause of your issue, and I really doubt it is. Anyway, the addMothership method can probably be re-written as follows:
private void addMothership()
{
    if (!MSSpawned && Greenfoot.getRandomNumber(2500) == 1)
    {
        MSSpawned = true;
        MothershipAlarm.play();
        addObject(msalert, 470, 300);
    }
    if (MSSpawned)
    {
        if (alertTimer > 0)
        {
            if (--alertTimer == 0)
            {
                removeObject(msalert);
                MothershipAlarm.stop();
                addObject(mothership, 895, 450);
                MothershipSound.play();
            }
        }
        else if (MSSoundTime > 0 && --MSSoundTime == 0)
        {
            MothershipSound.stop();
        }
    }
}
I did add a couple if's in there for a bit more control over what is happening. Notice however that no condition is asked multiple times (see your line 20, which asks the same two conditions on line 9 and 13.
Recorsi Recorsi

2018/3/21

#
I don't know why, but that fixed the issue. Thank you :)
You need to login to post a reply.