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

2016/4/21

Using getX & getY in world?

4
5
6
7
8
idk1234 idk1234

2016/5/10

#
Well I know I'm doing something wrong as it spawns 2 viruses as soon as infection starts
int[][] locs = { { 173, 429 }, { 252, 357 }, { 211, 499 } };
    public void respawn()
    {
        if (!getObjects(VirusWASD.class).isEmpty())
        {
            int rand = Greenfoot.getRandomNumber(locs.length);
            int x = locs[rand][0];
            int y = locs[rand][1];
            addObject(new VirusWASD(), x, y);
        }
    }
danpost danpost

2016/5/10

#
Whichever 'if' statement you use, you will need to regulate when it is executed or else you will have virus after virus piled on top of each other. If you are using multiple spawning locations, that may explain why 2 were spawning (if infected). Check it out. Use the first of the two 'if' statements above -- then stop the scenario and drag the viruses around to see if any are hiding underneath them.
idk1234 idk1234

2016/5/10

#
By 2 viruses I mean virus 1 = real virus (after infect) virus 2 = spawns when real virus is not dead (at infect) at one of the locations So I can't have more than 1 location? Sorry, I'm pretty bad at explaining Edit: I am also getting null pointer exceptions with the shotKeyDown method if I play with more than 2 players java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it...
danpost danpost

2016/5/10

#
idk1234 wrote...
So I can't have more than 1 location?
You can have a many locations as you want. You just need to control when the spawning takes place (add at least one more condition beside the one chosen from my 'if-// or-if' post).
Edit: I am also getting null pointer exceptions with the shotKeyDown method if I play with more than 2 players java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it...
Cannot fix errors without first seeing the code around which it occurs and any and all code related to it (field declarations of fields used; how they are assigned values; methods called; etc).
idk1234 idk1234

2016/5/11

#
Cannot fix errors without first seeing the code around which it occurs and any and all code related to it (field declarations of fields used; how they are assigned values; methods called; etc).
java.lang.NullPointerException at Lazer.act(Lazer.java:23) This error does not happen with 2 players, only with more than 2 players in world Code:
// instance field
private boolean shotKeyDown;
 
// in act method or a method it calls
if (shotKeyDown != Greenfoot.isKeyDown(shotkey)) // shotkey = "v" for player one
{
    shotKeyDown = !shotKeyDown; // save changed state
    if (shotKeyDown) // changing to down state?
    {
        shoot();
    }
}
Shoot code:
    /**
     * Shoots a lazer.
     */
    private void shoot()
    {
        Lazer lazer = new Lazer();
        getWorld().addObject(lazer, getX(), getY());
        lazer.setRotation(getRotation());
        lazer.move(25.0);
    }
Lazer code:
public class Lazer extends Mover
{
    private int life = Greenfoot.getRandomNumber(15) + 10;
    /**
     * Act - do whatever the Lazer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10.0);
        life--;
        killBadGuys();
        if (life == 0)
        {
            getWorld().removeObject(this);
        }
    }    

    public void remove()
    {
        Actor walls = getOneIntersectingObject(Collision.class);
        if(getX() <=1 || getX() >= getWorld().getWidth() -1)
        {
            getWorld().removeObject(this);
        }
        else if(walls != null)
        {
            getWorld().removeObject(this);
        }
    }

    public boolean amIshot(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor !=null;
    }

    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
    }

    public void killBadGuys()
    {
        if(amIshot(Virus.class))
        {
            kill(Virus.class);
            getWorld().removeObject(this);
        }
        else
        {
            remove();
        }
    }
}
danpost danpost

2016/5/11

#
Try moving lines 13 through 16 of the Lazer class to after line 29, the end of the remove method. Precede the moved block of code with 'else'.
idk1234 idk1234

2016/5/13

#
Thanks that worked perfectly! Now the only thing I need is to fix the problem with the Virus respawn. How exactly would I go about doing this?
You just need to control when the spawning takes place (add at least one more condition beside the one chosen from my 'if-// or-if' post).
danpost danpost

2016/5/13

#
You could get a random number before the 'if':
int rand = Greenfoot.getRandomNumber(250);
then add a condition that 'rand' is a specific value or range of values (adding extra condition to 'if'):
if (rand == 0 && getObjects(SurvivorWASD.class).isEmpty())
idk1234 idk1234

2016/5/13

#
I've added it like this, not sure if thats how you define locations because it doesn't seem to do anything:
    /**
     * Respawn method for Virus
     */
    public void respawn()
    {
        if (rand == 0 && getObjects(SurvivorWASD.class).isEmpty())
        {
            int[][] locs = { { 585, 201}, { 1148, 161 }, { 574, 536 } };
            int x = locs[rand][0];
            int y = locs[rand][1];
            addObject(new VirusWASD(), x, y);
        }
    }
danpost danpost

2016/5/13

#
idk1234 wrote...
I've added it like this, not sure if thats how you define locations because it doesn't seem to do anything: < Code Omitted >
The first line of code in my last post should be inserted at line 6 in your last post.
idk1234 idk1234

2016/5/14

#
I did that and it didn't want to compile so I just kept the "if" statement from my previous post and added the first line of your previous post before that.
    /**
     * Respawn method for Virus
     */
    public void respawn()
    {
        int rand = Greenfoot.getRandomNumber(250);
        if (rand == 0 && getObjects(SurvivorWASD.class).isEmpty())
        {
            int[][] locs = { { 585, 201}, { 1148, 161 }, { 574, 536 } };
            int x = locs[rand][0];
            int y = locs[rand][1];
            addObject(new VirusWASD(), x, y);
        }
    }
This doesn't work
Super_Hippo Super_Hippo

2016/5/14

#
With 'inserted' he didn't mean to replace the line. So it was correct that you added the line before line 6. I think the problem is that you use 'rand' as a condition for the if-block and as an array index. So you will only use the first location because 'rand' is always 0 when the block of code is executed.
idk1234 idk1234

2016/5/14

#
Oh.. So what do you recommend I use?
davmac davmac

2016/5/14

#
Your original respawn method looked like this:
public void respawn()
{
    if (!getObjects(VirusWASD.class).isEmpty())
    {
        int rand = Greenfoot.getRandomNumber(locs.length);
        int x = locs[rand][0];
        int y = locs[rand][1];
        addObject(new VirusWASD(), x, y);
    }
}
... but you have removed the 5th line (int rand = ....) - the line that chose a random location. (Nobody said you should remove this line!). So now, it doesn't select a random location. It always uses 0, the value of 'rand', which is now declared earlier and used to decide whether or not to spawn a VirusWASD. You shouldn't have removed the 5th line. You need to add it back, so that it will again select a random location. You may need to remove the 'int' at the beginning, since a 'rand' variable is already declared and you can't re-declare it, but otherwise the line should be exactly as it was:
public void respawn()
{
    int rand = Greenfoot.getRandomNumber(250);
    if (rand == 0 && getObjects(SurvivorWASD.class).isEmpty())
    {
        int[][] locs = { { 585, 201}, { 1148, 161 }, { 574, 536 } };
        rand = Greenfoot.getRandomNumber(locs.length);  // <---- HERE
        int x = locs[rand][0];
        int y = locs[rand][1];
        addObject(new VirusWASD(), x, y);
    }
}
idk1234 idk1234

2016/5/15

#
It doesn't seem to work - as I want it is if the person is "infected" and is a confirmed virus and gets killed. They should respawn as per corresponding virus. It seems very complicated
There are more replies on the next page.
4
5
6
7
8