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

2016/4/21

Using getX & getY in world?

5
6
7
8
davmac davmac

2016/5/15

#
I think you need to work on explaining what you want. It's unclear to me.
idk1234 idk1234

2016/5/16

#
Ok so there are Survivors at the start of the round in the world. After a certain amount of time 1 person is randomly infected (Change of class). And can infect other people by running over them. I want the people are are "infected" (virus class) to be able to respawn if they are killed by the remaining survivors.
davmac davmac

2016/5/16

#
I want the people are are "infected" (virus class) to be able to respawn if they are killed by the remaining survivors.
You want them to respawn when (immediately when they are killed, some time after they are killed, immediately when all of them are killed, some time after all of them are killed)...?? Where should they respawn? When you say respawn do you mean you want a Survivor to spawn, or another instance of the Virus? Please try to explain fully, and carefully, so that it is not ambiguous.
idk1234 idk1234

2016/5/16

#
Ok so this method should only work after the initial infect() method occurs (when a random survivor is selected to be virus). Now let's say Survivor1 is picked to be infected at the start. He would turn into Virus1. Now when Virus1 is killed he should be instantly respawned as Virus 1 at 1 of the defined locations that are set (different locations I set) .
danpost danpost

2016/5/16

#
idk1234 wrote...
when Virus1 is killed he should be instantly respawned as Virus 1 at 1 of the defined locations that are set (different locations I set) .
Maybe, instead of removing and replacing the actor, you should just randomly set its location at one of the defined locations.
davmac davmac

2016/5/16

#
idk1234:
Now when Virus1 is killed he should be instantly respawned as Virus 1 at 1 of the defined locations that are set (different locations I set).
Ok, then you should probably do the respawn from the same piece of code which handles the killing. (As danpost suggested, you might just move the existing object instead of removing it and then adding a new one). By "1 of the defined locations" you mean one chosen at random? The code posted before for that was correct:
        int rand = Greenfoot.getRandomNumber(locs.length);
        int x = locs[rand][0];
        int y = locs[rand][1];
        addObject(new VirusWASD(), x, y);  // or setLocation of existing object
idk1234 idk1234

2016/5/17

#
By 1 of the defined locations I mean like I would define locations (eg. 234, 235. | 524, 1020) and it would pick at random which location. I guess the smart thing to do is, as you guys said, is to just set its location. But how would I go about it selecting my defined locations?
davmac davmac

2016/5/17

#
But how would I go about it selecting my defined locations?
As I just said, the code posted for this before was correct (I even re-posted it in my previous message). You can change the call to `addObject` to a suitable call to `setLocation`.
idk1234 idk1234

2016/5/17

#
I understand but how am I able to tell 'setLocation' what objects location it is meant to be setting?
danpost danpost

2016/5/17

#
idk1234 wrote...
I understand but how am I able to tell 'setLocation' what objects location it is meant to be setting?
Where is the code that kills a Virus1 or Virus2 object? You should have a reference to it there. You would move the "respawn" (reset location) method there and prefix 'setLocation' with that reference.
davmac davmac

2016/5/17

#
how am I able to tell 'setLocation' what objects location it is meant to be setting?
The same way you tell any method what object to act on :) If you have a reference to an Actor in a variable called 'a', then you write:
  a.setLocation(x, y);
So for instance in your Lazer class, you kill method could become:
    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null)
        {
            actor.setLocation(300, 400);
        }
    }
Obviously that's not a complete solution: you want to incorporate the random selection of location, and you need to handle killing of infected differently to killing survivors.
idk1234 idk1234

2016/5/18

#
Hi thanks for the help! As you know I'm trying to allow it to set one of the "random locations". Is this how I would go about doing it? As I am getting null pointer exceptions:
    public void respawnLocation()
    {
        int[][] locs = { { 585, 201}, { 1148, 161 }, { 574, 536 } };
        int rand = Greenfoot.getRandomNumber(locs.length);
        int x = locs[rand][0];
        int y = locs[rand][1];
        virusARROW.setLocation(x, y);  // <---- NULL POINTER EXCEPTION
    }

    public void respawn(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null)
        {
            respawnLocation();
        }
    }
}
SPower SPower

2016/5/18

#
The only reason why you could get a null pointer exception at that line is because you haven't initialised virusARROW, and you can't call methods on non-initialised objects.
idk1234 idk1234

2016/5/19

#
Sorry, what do you mean by initialised? Do you mean this?:
    private VirusARROW virusARROW;
    private VirusPL virusPL;
    private VirusWASD virusWASD;
    private VirusYGHJ virusYGHJ;
davmac davmac

2016/5/19

#
what do you mean by initialised? Do you mean this?
No. Those statements declare the variables, but do not initialise them (give them a value). So, they get the default value for reference variables, which is null (essentially meaning "no object"). That is why you later get an exception - you're trying to call setLocation on no object. It is difficult to help you because it's not clear what class the "respawn" and "respawnLocation" methods are in. If it is in the Virus class, you don't need to call setLocation on another object. If its in some other class, I suggest that they should probably take an object as a parameter specifying which object to respawn (relocate).
You need to login to post a reply.
5
6
7
8