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

2018/3/20

Problem dabei etwas an einem zufälligem Ort spawnen zu lassen aber auf bestimmten koordinaten nicht

1
2
Yehuda Yehuda

2018/3/21

#
...just like the World would not be able to access the private variables from Healkit (even though they were created in that class).
Vercility Vercility

2018/3/21

#
Yehuda wrote...
Healkit can access them but not the World, just like the World can't access the private variables from Healkit.
And how is that related to my code o.o Dan basically did the same but gave the world as an argument to the method whereas I called getWorld() BTW why are you using the world as a parameter but then use getWorld anyway?
danpost danpost

2018/3/21

#
Iiuz wrote...
sorry but i am confused now which part should come now in which class?
The spawning code goes in your world class. Your Healkit class should look how I last posted it as.
Yehuda Yehuda

2018/3/21

#
Vercility wrote...
Dan basically did the same but gave the world as an argument to the method whereas I called getWorld()
Danpost placed the code into the Healkit class itself, or said to anyway.
Vercility Vercility

2018/3/21

#
Yehuda wrote...
Vercility wrote...
Dan basically did the same but gave the world as an argument to the method whereas I called getWorld()
Danpost placed the code into the Healkit class itself, or said to anyway.
Well so was mine meant to be, why would I call getWorld in the world class? :o A static method in Healkit that adds a Healkit to the world. I admit that's not elegant but it does the trick ¯\_(ツ)_/¯
danpost danpost

2018/3/21

#
Vercility wrote...
Am I missing something here lol. Healkit is a subclass of actor so it should have access to getOneIntersectingObject which should then return any objects of the Hindernis class?
All subclasses of Actor have access to the getOneIntersectingObject method -- yes; but only to be used on objects create from that class or a subclass of that class. You cannot have a Player object, an Actor subclass instance, check to see if a Bullet object, also an instance of an Actor subclass, intersects an Enemy object, again, an instance of a subclass of Actor. As in code, this is not allowed in Player class:
if (!getWorld().getObjects(Bullet.class).isEmpty())
{
    Actor bullet = (Actor)getWorld().getObjects(Bullet.class).get(0);
    Actor enemy = bullet.getOneIntersectingObject(Enemy.class); // ***** error occurs here ********
    if (enemy != null)
    {
        getWorld().removeObject(enemy);
        getWorld().removeObject(bullet);
        score++;
    }
}
Vercility Vercility

2018/3/21

#
Well that's interesting. I'll never stop learning from you lmao.
danpost danpost

2018/3/21

#
Vercility wrote...
BTW why are you using the world as a parameter but then use getWorld anyway?
Habit. Machts nichts. The parameter was not of choice -- it is required.
Iiuz Iiuz

2018/3/21

#
import greenfoot. *; public class Healkit extends Actor { long lastAdded = System.currentTimeMillis (); int randomX; int randomY; public Healkit() { randomX = Greenfoot.getRandomNumber (18) +1; randomY = Greenfoot.getRandomNumber (14) +1; } protected void addedToWorld(World world) { if (getOneIntersectingObject(Baum.class) != null ) getWorld().removeObject(this); } public void act() { long curTime = System.currentTimeMillis(); if (curTime == lastAdded + 20000) //20000ms = 20s { Healkit healkit = new Healkit (); while (healkit. getWorld () == null) { healkit = new Healkit(); int randomX = Greenfoot.getRandomNumber(getWorld (). getWidth ()); int randomY = Greenfoot.getRandomNumber (getWorld (). getHeight ()); getWorld (). addObject (healkit, randomX, randomY); lastAdded = curTime; } } } } i have only this in healkit and nothing in the word class but it seems to work so
Vercility Vercility

2018/3/21

#
danpost wrote...
Vercility wrote...
BTW why are you using the world as a parameter but then use getWorld anyway?
Habit. Machts nichts. The parameter was not of choice -- it is required.
How so? The only reasons I can think of right now are an interface or an already implemented method
danpost danpost

2018/3/21

#
Vercility wrote...
How so? The only reasons I can think of right now are an interface or an already implemented method
It is an already implemented method. Check out the Actor class API documentation.
Vercility Vercility

2018/3/21

#
danpost wrote...
Vercility wrote...
How so? The only reasons I can think of right now are an interface or an already implemented method
It is an already implemented method. Check out the Actor class API documentation.
Fair enough, used to seeing @override in that case so I kinda subconsciously ignored that haha.
danpost danpost

2018/3/21

#
Vercility wrote...
Fair enough, used to seeing @override in that case so I kinda subconsciously ignored that haha.
The act method is also implemented in the Actor class, but rarely (if ever) do I see @overridde with it.
Iiuz Iiuz

2018/3/21

#
import greenfoot. *; public class Healkit extends Actor { long lastAdded = System.currentTimeMillis (); int randomX; int randomY; public Healkit() { randomX = Greenfoot.getRandomNumber (18) +1; randomY = Greenfoot.getRandomNumber (14) +1; } protected void addedToWorld(World world) { if (getOneIntersectingObject(Baum.class) != null ) getWorld().removeObject(this); } public void act() { long curTime = System.currentTimeMillis(); if (curTime == lastAdded + 20000) //20000ms = 20s { getWorld().addObject(new Healkit(), randomX, randomY); lastAdded = curTime; } } } this little bit shorter version should work to or?
You need to login to post a reply.
1
2