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

2013/10/22

Help spawning object

1
2
bananafin bananafin

2013/10/22

#
Hello, I have few questuins as to the placing of my method and how it should look like. I have an object "Shroom" that gets placed in the world on a x and y. My player can collide with it and it dissappears and the shroomcounter adds 1. What i would like to do is that when the shroom dissappears, another one should spawn at a random location. Except it must always spawn on (Y+10) top of a block. Blocks are my level ground. Should i put the method in my player object? And how do i make it spawn on top of a block object. Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Level1 extends World
{
 
    public Level1()
    {   
        this (new Alice());
    }
 
    public Level1(Alice alice)
    {   
         
        super(800, 400, 1);
        //Player
        addObject(new Alice(),10,340);
         
        //Counter
        addObject(new Score(), 30, 10);
        addObject(new ShroomScore(), 130, 10);
         
       //Level blocks
        addObject(new Block(),16,384);
        addObject(new Block(),48,384);
        addObject(new Block(),80,384);
        addObject(new Block(),112,384);
        addObject(new Block(),144,384);
        addObject(new Block(),176,384);
        addObject(new Block(),208,384);
        addObject(new Block(),240,384);
        addObject(new Block(),272,384);
        addObject(new Block(),304,384);
        addObject(new Block(),336,384);
        addObject(new Block(),368,384);
        addObject(new Block(),400,384);
        addObject(new Block(),432,384);
        addObject(new Block(),464,384);
        addObject(new Block(),496,384);
        addObject(new Block(),528,384);
        addObject(new Block(),560,384);
        addObject(new Block(),592,384);
        addObject(new Block(),624,384);
        addObject(new Block(),656,384);
        addObject(new Block(),688,384);
        addObject(new Block(),720,384);
        addObject(new Block(),752,384);
        addObject(new Block(),784,384);
 
// objects
        addObject(new Shroom(), 250,300);
 
 
// in my player class
 
 
public void checkCollisionShroom()
    {
        Actor collided;
        collided = getOneIntersectingObject(Shroom.class);
        if (collided !=null)
        {
            ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
 
            getWorld().removeObject(collided);
            // add code for random spawning on a block?
        }
    }
danpost danpost

2013/10/22

#
Being that your block locations are hard-coded with a y-coordinate of 384 and your blocks span the whole width of the world, you can start with 384 and subtract half the width of the block and also subtract half the width of the shroom to get the y-coordinate of where a shroom can go. The x-coordinate can be any value within the width of the world, so getting a random number using the width of the world will get any one possible location. If you are worried about a shroom spawning on top of Alice, and you cannot figure that out, post back.
Baenefin Baenefin

2013/10/23

#
Hi danpost, that helped me along nicely. I have the code in the counter that counts my shrooms. I'm not sure that is the best place for it, but it works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void add(int addAmt)
{
    ShroomScore+=addAmt;
    updateImage();
    if(ShroomScore == 1)
    {
        getWorld().addObject(new Shroom(), Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(61)+300);
    }
            if(ShroomScore == 2)
    {
        getWorld().addObject(new Shroom(), Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(61)+300);
    }
            if(ShroomScore == 3)
    {
        getWorld().addObject(new Shroom(), Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(61)+300);
    }
            if(ShroomScore == 4)
    {
        getWorld().addObject(new Shroom(), Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(61)+300);
    }
    if(ShroomScore == 5)
    {
        getWorld().addObject(new Key(), Greenfoot.getRandomNumber(getWorld().getWidth()),Greenfoot.getRandomNumber(61)+300);
    }
}
What I am trying to do now is spawn the shrooms at least 100X away from the last shroom. Would I need to check where the Actor is and add a minimum of X to that before the RandomNumber is applied or can I get oldX and add a random minimum to that?
danpost danpost

2013/10/23

#
To spawn a minimum of 100 away from last shroom, you will be restricting the range of where the new shroom spawns by 200 (100 on both left and right side of ole shroom). If 'oldShroom' is the shroom to be removed, then:
1
2
3
4
5
6
7
8
public void replaceShroom(Shroom oldShroom)
{
    int wide = getWorld().getWidth();
    int x = (Greenfoot.getRandomNumber(wide-200)+oldShroom.getX()+100)%wide;
    int y = Greenfoot.getRandomNumber(61)+300;
    getWorld().addObject(new Shroom(), x, y);
    getWorld().removeObject(oldShroom);
}
Baenefin Baenefin

2013/10/24

#
And that did it :) Thank you Edit: Now with the scrolling world it goed wrong. (may be my code) After the first shroom is collided, the second one never spawns.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void replaceShroom(Shroom oldShroom) 
    
        int wide = getWorld().getWidth(); 
        int x = (Greenfoot.getRandomNumber(wide-200)+oldShroom.getX()+100)%wide; 
        int y = Greenfoot.getRandomNumber(61)+300;  
 
        if(ShroomScore == 1
        
            getWorld().addObject(new Shroom(), x, y); 
            getWorld().removeObject(oldShroom); 
        
        if(ShroomScore == 2
        
            getWorld().addObject(new Shroom(), x, y); 
            getWorld().removeObject(oldShroom);
        
        if(ShroomScore == 3
        
            getWorld().addObject(new Shroom(), x, y); 
            getWorld().removeObject(oldShroom);
        
        if(ShroomScore == 4
        
            getWorld().addObject(new Shroom(), x, y); 
            getWorld().removeObject(oldShroom);
        
        if(ShroomScore == 5
        
             getWorld().addObject(new Key(), x, y);
             getWorld().removeObject(oldShroom);
        }
    }
Baenefin Baenefin

2013/10/25

#
If I implement it as above, I cannot see a second shroom spawn. Perhaps I don't understand, but don't I need to say what happened when my shroomscore is 1,2,3,4,5?
danpost danpost

2013/10/25

#
(1) I might have something to do with how you are dealing with the 'ShroomScore' field. (2) After you fix (1), change line 6 of my code above to:
1
2
if (ShroomScore%5 != 0) getWorld().addObject(new Shroom(), x, y);
else getWorld().addObject(new Key(), x, y);
Your fix in (1) may produce a need to alter the fix in (2).
Baenefin Baenefin

2013/10/25

#
Ok so let me get this straight.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class ShroomScore extends Actor
{
    private int ShroomScore = 0;
 
    public ShroomScore()
    {
        updateImage();
    }
 
    private void updateImage()
    {
        setImage(new GreenfootImage("Shrooms: "+ShroomScore, 18, Color.black, new Color(0, 0, 0, 0)));
    }
 
 
    public void add(int addAmt)
    {
        ShroomScore+=addAmt; 
        updateImage(); 
    }
 
public void replaceShroom(Shroom oldShroom)
    {
        int wide = getWorld().getWidth(); 
        int x = (Greenfoot.getRandomNumber(wide-2200)+oldShroom.getX()+10)%wide; 
        int y = Greenfoot.getRandomNumber(61)+300
        if (ShroomScore%1 != 0) getWorld().addObject(new Shroom(), x, y);
        if (ShroomScore%2 != 0) getWorld().addObject(new Shroom(), x, y);
        if (ShroomScore%3 != 0) getWorld().addObject(new Shroom(), x, y);
        if (ShroomScore%4 != 0) getWorld().addObject(new Shroom(), x, y);
        if (ShroomScore%5 != 0) getWorld().addObject(new Shroom(), x, y);
        else getWorld().addObject(new Key(), x, y);
 
    }
My worls id now 2400. So i get the width-2200. That leaves me with 200. +100. I am missing something completely obvious I think.
danpost danpost

2013/10/25

#
The first thing that I want you to do is change either the class name or the field within in the class from 'ShroomScore' to something else, so they do not have the same name. You will probably end up with errors elsewhere in your code, but, in the long run, we should be able to get things working. Look again at what I wrote in my last post, the change you made is NOT what I suggested.
Baenefin Baenefin

2013/10/25

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import greenfoot.*;
import java.awt.Color;
 
public class ShroomScore extends Actor 
    private int ScoreCounter = 0
   
    public void ScoreCounter() 
    
        updateImage(); 
    }  
   
    private void updateImage() 
    
        setImage(new GreenfootImage("Shrooms: "+ScoreCounter, 18, Color.black, new Color(0, 0, 0, 0))); 
    
   
   
    public void add(int addAmt) 
    
        ScoreCounter+=addAmt;   
        updateImage();   
    
 
    public void replaceShroom(Shroom oldShroom) 
    
        int wide = getWorld().getWidth(); 
        int x = (Greenfoot.getRandomNumber(wide-200)+oldShroom.getX()+100)%wide; 
        int y = Greenfoot.getRandomNumber(61)+300
        if (ScoreCounter%1 != 0) getWorld().addObject(new Shroom(), x, y); 
        else getWorld().addObject(new Key(), x, y);
        getWorld().removeObject(oldShroom);
    }
Now i I start with the greenfoot no image (the greenfoot) wher the counter is 0. [Disallowed URL] When I pickup the first Shroom it is normal [Disallowed URL] I am not grasping it I think. I don't see a second shroom spawn within the level
danpost danpost

2013/10/25

#
Change line 8 to:
1
public ShroomScore()
Baenefin Baenefin

2013/10/28

#
I still dont get a new shroom. My scrolling level is now
1
super(800, 400, 1, 2400);
my first shroom is:
1
addObject(new Shroom(), 250,300,true);
And my shroomscore is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ShroomScore extends Actor 
    private int ScoreCounter = 0
   
    public ShroomScore()
    
        updateImage(); 
    }  
   
    private void updateImage() 
    
        setImage(new GreenfootImage("Shrooms: "+ScoreCounter, 18, Color.black, new Color(0, 0, 0, 0))); 
    
   
   
    public void add(int addAmt) 
    
        ScoreCounter+=addAmt;   
        updateImage();   
    
 
    public void replaceShroom(Shroom oldShroom) 
    
        int wide = getWorld().getWidth(); 
        int x = (Greenfoot.getRandomNumber(wide-200)+oldShroom.getX()+100)%wide; 
        int y = Greenfoot.getRandomNumber(61)+300;  
        if (ScoreCounter%5 != 0) getWorld().addObject(new Shroom(), x, y); 
        else getWorld().addObject(new Key(), x, y);
        getWorld().removeObject(oldShroom);
    }
The thing is, I can never see a second shroom. I don't get it.
danpost danpost

2013/10/28

#
From where are you calling 'replaceShroom'? What code do you have in the act of 'Alice' (include code in any method executed while this act method runs)?
Baenefin Baenefin

2013/10/28

#
Alice has a checkCollisionShroom() in her act.
1
2
3
4
5
6
7
8
9
public void checkCollisionShroom()
   {
       Actor collided = getOneIntersectingObject(Shroom.class);
       if (collided !=null)
       {
           ((ShroomScore) getWorld().getObjects(ShroomScore.class).get(0)).add(1);
           getWorld().removeObject(collided);
       }
   }
danpost danpost

2013/10/28

#
Nowhere in that code do I see a call to 'replaceShroom'.
There are more replies on the next page.
1
2