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

2021/10/10

Location problem

mik. mik.

2021/10/10

#
Their are 2 classes, namely generator which gets added to the world automatically after every 10 seconds and a class named locate which gets addded upon generator class when new generator is added to world. The problem which i m facing i.e. when a new generator is again added to world after 10 seconds, the locate is not getting added upon the generator class. It gets added upon the previous generator. I want locate to get added upon every generator class when its added newly
//in world
for(sec>=10) 
{
addObject(new Generator(), 350,300) ;
addObject(new Locate(), 0,0) ;
}
//in locate class
Actor gen= getWorld().getObjects(Generator.class) .get(0) ;
if(gen! =null) 
 setLocation(gen.getX(), gen. getY()) ;
danpost danpost

2021/10/10

#
Remove everything after line 4. In Generator class, add the following method:
protected void addedToWorld(World world)
{
    world.addObject(new Locate(), getX(), getY());
}
mik. mik.

2021/10/11

#
Its not adding upon the generator class But i modified the code little bit but it started working as it was working before
//in generator class
protected void addedToWorld(World world)
{
    world.addObject(new Locate(), getX(), getY());
}
//in locate class
Actor gen= getWorld().getObjects(Generator.class) .get(0) ;
if(gen! =null) 
 setLocation(gen.getX(), gen. getY()) ;
danpost danpost

2021/10/11

#
Remove lines indicated in Locate class (lines 7 thru 9 above).
mik. mik.

2021/10/11

#
danpost wrote...
Remove lines indicated in Locate class (lines 7 thru 9 above).
If i will remove then no locate class is added or can be seen in the world
danpost danpost

2021/10/11

#
mik. wrote...
If i will remove then no locate class is added or can be seen in the world
Show Generator class codes.
You need to login to post a reply.