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

2014/5/13

How to Spawn a New Object that is Away from You?

Jesus_LOVES_You Jesus_LOVES_You

2014/5/13

#
I am still very new to programming...so hopefully I can get some help here :) I am now making a small game where you (Fish) want to eat Badfish. There is only 1 Badfish in the world at once, and when that one is eaten, a new one will spawn in a random spot. The thing is, I want the new one to spawn at least 100 pixels away from me (Fish). I now already have a some codes that allow the Badfish to spawn randomly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
     * Whenever the Badfish has been eaten, a new one will randomly generated in the world
     */
    private void createNewBadfish()
    {
        Badfish newBadfish;
        newBadfish = new Badfish();
         
        World world;
        world = getWorld();
         
        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
         
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
         
        world.addObject(newBadfish, x, y);
    }
danpost danpost

2014/5/13

#
Since you are taking a 200x200 area away from that which a badfish can be spawned and it can be restricted either horizontally or vertically, then the direction of restriction should be chosen randomly.
1
2
3
4
5
6
7
8
9
10
11
int x = 0, y = 0;
if (Greenfoot.getRandomNumber(2) == 0)
{
    x = Greenfoot.getRandomNumber(worldWidth);
    y = ((getY()+100)+(Greenfoot.getRandomNumber(worldHeight-200)))%worldHeight;
}
else
{
    x = ((getX()+100)+(Greenfoot.getRandomNumber(worldWidth-200)))%worldWidth;
    y = Greenfoot.getRandomNumber(worldHeight);
}
Jesus_LOVES_You Jesus_LOVES_You

2014/5/21

#
Thanks for your help. After seeing your codes, there are still something that I don't understand why you did that: 1. Why did you restrict the spawning of badfish in 2 different ways and not together? What would happen if I don't use the "if" statement and combine both possible actions?
1
2
x = ((getX()+100)+(Greenfoot.getRandomNumber(worldWidth-200)))%worldWidth;
            y = ((getY()+100)+(Greenfoot.getRandomNumber(worldHeight-200)))%worldHeight;
2. What does this "%worldHeight" do? 3. Why did you put (Greenfoot.getRandomNumber(worldWidth-200)? Doesn't that allow badfish to spawn back near me? What I think here is that...let say I am at y=100, (getY()+100) will make the badfish spawn 100 pixels above me, and if the random number gets 100, which would be -100 after -200. Where the badfish will spawn at where I am after adding the 100 to it. Not sure if you can understand what I am trying to ask here...sorry for my bad explanation.
danpost danpost

2014/5/21

#
Answers: (1) Let me explain it this way -- assume your Fish was in the middle square of a Tic-Tac-Toe grid and you want the spawning of Badfish to occur in any of the other grid-squares. If you did what you suggested by 'combining the actions', the spawns will only occur in the corner squares (restricted BOTH ways instead of restricted in only ONE of the two ways). (2) The '%' symbol is the arithmetic 'modulus' operator. It returns the remainder of a division operation. This will produce the following for any 'int' division. For int division using the formula 'A/B=CrD : A/B=C and A%B = D. Applying the modulus of the world dimensions to the expression, we are limiting the result to valid values for coordinates within the world. It actually performs a 'wrapping' operation when used this way. (3) Like I said earlier, you are not allowing a range of values to be valid for where a spawn can occur. The 'getY()+100' is the starting point for the valid range and (when the world is wrapped upon itself, the valid range is 'worldWidth-200'. BTW, if you are at y=100, 'getY()+100 would be 200 (100 pixels below you), then adding the random number between 0 and worldWidth-200 gives you a range of 100 to worldWidth-100 from you.
You need to login to post a reply.