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

2020/3/27

shoot direction to my fish

greenfish greenfish

2020/3/27

#
Hello there, i have a fish named "Yellowfish" and he needs to dodge all balls from "Greenfish" who spit a ball named "Giftkugel" How can i shoot to the location, where my Yellowfish is AT THE MOMENT? Do i need to write that in the ball "Giftkugel" or the one who spits it, "Greenfish"?
danpost danpost

2020/3/27

#
greenfish wrote...
i have a fish named "Yellowfish" and he needs to dodge all balls from "Greenfish" who spit a ball named "Giftkugel" How can i shoot to the location, where my Yellowfish is AT THE MOMENT? Do i need to write that in the ball "Giftkugel" or the one who spits it, "Greenfish"?
You can write that in either class. I think, however, I would do it in the Giftkugel class to avoid excess code in the Greenfish class by making use of the addedToWorld method.
greenfish greenfish

2020/3/27

#
Thank you so much. but another question, what do I need to write? Im just having this in my public void act at Giftkugel: this.move(10); if(this.isAtEdge()) this.getWorld().removeObject(this); ---
danpost danpost

2020/3/27

#
danpost wrote...
use of the addedToWorld method.
... to set rotation.
greenfish greenfish

2020/3/27

#
okay thanks!
greenfish greenfish

2020/3/27

#
Well, tried 5min to get it but the balls are just imitating me when i go somewhere. They just need to spit at the direction where my yellowfish was at this moment, not imitating me.
    Actor yellowfish = (Actor)getWorld().getObjects(Yellowfish.class).get(0);
    setRotation(yellowfish.getRotation());
    this.move(7);
    if(this.isAtEdge())
    this.getWorld().removeObject(this);
danpost danpost

2020/3/27

#
greenfish wrote...
Well, tried 5min to get it but the balls are just imitating me when i go somewhere. They just need to spit at the direction where my yellowfish was at this moment, not imitating me. << Code Omitted >>
That looks like you have it in your act method -- not the addedToWorld method. Also, you do not want to take on the rotation of the yellow fish -- you want to turn towards it.
greenfish greenfish

2020/3/27

#
yesss my bad. its working. thank you! for all, who wants to get the code:
    public void addedToWorld(World w){
    Actor _NAME_ = (Actor)getWorld().getObjects(_YOURACTOR_.class).get(0);
    turnTowards(_YOURACTOR_.getX(), _YOURACTOR_.getY());
You need to login to post a reply.