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

2011/12/5

Actors doesn't show in Level2

1
2
raniye raniye

2011/12/5

#
Now I fixed my first problem, I couldn't get into my level2 world. This is fixed. BUT NOW: I am in my level 2 world but it doesn't load my Actors (walls, the actor himself, coins to pick up). Here is my level2 World code have I put it in some wrong place maybe? :
public class level2 extends World
{
    public int score = 0;
    private int item = 10;
    public Score scoreboard;
    
    /**
     * Default constructor. If we don't receive a game character, create one.
     */
    public level2()
    {     
        this(new haai());
        plaatsCoins();
        blokken();
        haaiTonen();
        scoreboard = new Score();
        addObject(scoreboard, 10, 1);
    }

    /**
     * Constructor used during play - the game character is passed in.
     */
    public level2(haai Haai)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(60, 60, 10); 
    }
    
    private void blokken()
    {
        //linkerkant
        addObject(new zand(), 1, 6);
        addObject(new zand(), 1, 12);
        addObject(new zand(), 1, 18);
        addObject(new zand(), 1, 24);
        addObject(new zand(), 1, 30);
        addObject(new zand(), 1, 36);
        addObject(new zand(), 1, 42);
        addObject(new zand(), 1, 48);
        addObject(new zand(), 1, 54);
        addObject(new zand(), 1, 60);
        
        //bovenkant
        addObject(new zand(), 6, 2);
        addObject(new zand(), 12, 2);
        addObject(new zand(), 18, 2);
        addObject(new zand(), 24, 2);
        addObject(new zand(), 30, 2);
        addObject(new zand(), 36, 2);
        addObject(new zand(), 42, 2);
        addObject(new zand(), 48, 2);
        addObject(new zand(), 54, 2);
        addObject(new zand(), 60, 2);
        
        //rechterkant
        addObject(new zand(), 58, 6);
        addObject(new zand(), 58, 12);
        addObject(new zand(), 58, 18);
        addObject(new zand(), 58, 24);
        addObject(new zand(), 58, 30);
        addObject(new zand(), 58, 36);
        addObject(new zand(), 58, 42);
        addObject(new zand(), 58, 48);
        addObject(new zand(), 58, 54);
        addObject(new zand(), 58, 60);
        
        //onderkant
        addObject(new zand(), 6, 60);
        addObject(new zand(), 12, 60);
        addObject(new zand(), 18, 60);
        addObject(new zand(), 24, 60);
        addObject(new zand(), 30, 60);
        addObject(new zand(), 36, 60);
        addObject(new zand(), 42, 60);
        addObject(new zand(), 48, 60);
        addObject(new zand(), 54, 60);
        addObject(new zand(), 60, 60);
    }
    private void plaatsCoins()
    {
        addObject(new coin(), 50, 50);
        addObject(new coin(), 40, 40);
        addObject(new coin(), 30, 30);
        addObject(new coin(), 20, 20);
        addObject(new coin(), 10, 10);
        addObject(new coin(), 50, 10);
        addObject(new coin(), 40, 10);
        addObject(new coin(), 30, 10);
        addObject(new coin(), 20, 10);
        addObject(new coin(), 50, 20);
        addObject(new coin(), 40, 20);
        addObject(new coin(), 30, 20);
        addObject(new coin(), 10, 20);
        addObject(new coin(), 50, 30);
        addObject(new coin(), 40, 30);
        addObject(new coin(), 20, 30);
        addObject(new coin(), 10, 30);
        addObject(new coin(), 50, 40);
        addObject(new coin(), 30, 40);
        addObject(new coin(), 20, 40);
        addObject(new coin(), 10, 50);
        addObject(new coin(), 20, 50);
        addObject(new coin(), 30, 50);
        addObject(new coin(), 40, 50);
    }
    
    private void haaiTonen()
    {
        addObject(new haai(), 10,40);
    }
    
    
      public Score getScoreObject()
    {
        return scoreboard;
    }
}
Thanks already!
davmac davmac

2011/12/5

#
First, your classes should begin with a capital and your variable names should begin with a lower case letter (this is just convention, but it will make your code easier to read). So your "haai" class should be called "Haai" instead. Then, your constructor which takes a haai parameter:
    /** 
     * Constructor used during play - the game character is passed in. 
     */  
    public level2(haai Haai)  
    {      
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.  
        super(60, 60, 10);   
    }  
... doesn't actually do anything. It doesn't add the haai object into the world, and it doesn't call plaatsCoins() which would put the coins in the world, and it doesn't call blokken() which would add the blocks.
raniye raniye

2011/12/5

#
Yes I'm aware of my mistake with the capital letters and non-capital letters. In my next project I will use it correctly but now I have used it too much to change it throughout my whole project. So I just want the block and the coins and my actor to be in the world, so could you help me with this, please? Maybe edit my code so it might work? Thanks
nccb nccb

2011/12/5

#
Hi raniye. Read the second part of davmac's message. By passing a parameter to the world constructor "new level2(new haai())", you are calling the level2(haai Haai) constructor which does not add any objects to the world. If you instead used "new level2()" then the first constructor will be called, which does add all the objects to the world.
davmac davmac

2011/12/5

#
I've already said what you need to do. In the second constructor (the one I re-posted), add Haai into the world, and call the plaatsCoins() and blokken() methods. You also need to create the scoreboard and add it into the world. You already have most of this code in the other constructor so you can just copy and paste it from there. The only difference is, instead of calling haaiTonen(), you should just add the actor that you received as a parameter into the world.
raniye raniye

2011/12/5

#
Ok I tried putting blokken() and plaatsCoins() to the public level2(haai Haai) and it worked so far. But when I added haaiTonen() , which should actually show my Actor , it stopped working. Why can't it show my Actor?
davmac davmac

2011/12/5

#
This:
The only difference is, instead of calling haaiTonen(), you should just add the actor that you received as a parameter into the world
That means: don't call haaiTonen(). Instead, just add the Haai object into the world. And: if it still "stops working", explain exactly what you mean by "stopped working". :)
raniye raniye

2011/12/5

#
How can I add the Haai object into the world? Code ?
danpost danpost

2011/12/5

#
There are two ways to accomplish what you need to do. (1) You can create another haaiTonen method that gets passed to it the object being added into the world, or (2) Add the statement directly in the constructor of the world. Your current haaiTonen method has the code statement that adds an object into the world. You just need to adjust it by replacing what is going into the world (instead of a new object, one that is already created).
danpost danpost

2011/12/5

#
raniye, are you aware that in a world that is created 60 x 60 of 10 pixels per cell (using your example, but can be any size cell and any number of cells) that the top-left cell is at location(0, 0), not (1, 1); and the bottom-right cell is at location (59, 59), not (60, 60)?
davmac davmac

2011/12/6

#
How can I add the Haai object into the world? Code ?
I don't want to give the answer in code specifically because it seems like something you need to figure out yourself. Here's some hints, in the form of questions: 1. What method do you call to add an object into the world? (you already use this method elsewhere). 2. What parameters are required for that method? (look it up in the documentation).
raniye raniye

2011/12/6

#
I'm sorry I tried almost every possibility and it still gives errors or it just doesn't work... I looked up the parameters I needed for the addObject, and I tried every possibility as I already said, nothing works. And I still have to fix my scoreboard to because that one doesn't want to work either. Getting so tired of this ...
davmac davmac

2011/12/6

#
Well, what parameters do you need for addObject - and what did you try? Give one or two examples of things you tried. If you can at least show that you tried something, people will be much more willing to help. For the scoreboard you can copy these two lines from your first constructor into the other constructor: scoreboard = new Score(); addObject(scoreboard, 10, 1);
raniye raniye

2011/12/6

#
I tried : addObject(Haai, 10, 40); addObject(haai, 10, 40); addObject(new haai(), 10, 40); addObject(new Haai(), 10, 40); addObject(Haai(), 10, 40); addObject(haai(), 10, 40); and so on and so forth... I know some of them are impossible and will give syntax errors, but I tried every combination... Thanks for the scoreboard fix, it worked :-)
davmac davmac

2011/12/6

#
As far as I can tell, this: addObject(Haai, 10, 40); ... should have worked. What error did you get with that?
There are more replies on the next page.
1
2