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

2014/11/19

Do i understand it right?

Erdrag1 Erdrag1

2014/11/19

#
do i understand the code right now? or am i getting something wrong? private void createNewLettuce() { Lettuce newLettuce; = Variable for a short time newLettuce = new Lettuce(); = Creating a new Object (stores in the Lettuce newLettuce variable) World world; = Variable for the world world = getWorld(); = Get the world that the actor is in int x = Greenfoot.getRandomNumber(1600); = get random X coordinate in the width of 1600 int y = Greenfoot.getRandomNumber(900); = get random Y coordinate in the height of 900 world.addObject(newLettuce,x ,y); = adds new Object into world (in any X or Y coordinate) (you need 2 variables to do this (int X or Y) }
danpost danpost

2014/11/19

#
I thought you were creating new lettuce from the class that extends World. The code above looks ok if it was to be in a class that extends Actor.
Erdrag1 Erdrag1

2014/11/19

#
yea at first i was going to creat new lettuce from extend world, but i changed to extend Actor, is it better to create Lettuce from extend world or is it the same thing as to do it in Actor?
danpost danpost

2014/11/20

#
It may or may not make a difference. Using extends World seems more logical since that object has to check how many lettuce are in it and add the new lettuce into it. The only up-side of having it in the extends Actor is that when the actor is removed from the world, automatically no more lettuce will spawn; but if your game ends there, then the question is, "Will it matter?".
Erdrag1 Erdrag1

2014/11/20

#
hmm for the game i do now, it doesnt matter, but if i would do it in "extend world" would the code be the same?
danpost danpost

2014/11/20

#
Erdrag1 wrote...
hmm for the game i do now, it doesnt matter, but if i would do it in "extend world" would the code be the same?
Minus the 'getWorld' and 'world' references. Simplified it would be like this:
private void createNewLettuce()
{
    int x = Greenfoot.getRandomNumber(1600); // get random X coordinate
    int y = Greenfoot.getRandomNumber(900); // get random Y coordinate
    addObject(new Lettuce(), x, y); // adds new lettuce object into world        
}
Actually this is equivalent:
private void createNewLettuce()
{
    Lettuce lettuce;
    lettuce = new Lettuce();

    World world;
    world = this;

    int x = Greenfoot.getRandomNumber(1600);
    int y = Greenfoot.getRandomNumber(900);

    world.addObject(lettuce, x, y);
}
Erdrag1 Erdrag1

2014/11/20

#
and i allso have to Call "createNewLettuce();" in ifcansee method, to make the Lettuce spawn. So if i move the code to spawn lettuce to world, i also need to move ifcanSee and that isnt possible right? because the ifCanSee need to be in the Actors class, if im correct
danpost danpost

2014/11/21

#
I thought you wanted to add lettuce if no lettuce as left in the world. However, if you only have one lettuce in the world then at the time it is eaten, there will be none. So, either way, still. That is, in the "extends Actor", you can either call the method to add lettuce in the 'ifcansee' method OR you can still use separate from the 'ifcansee' method 'if (getWorld().getObjects(Lettuce.class).isEmpty())' to qualify when to call the 'createNewLettuce' method.
You need to login to post a reply.