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

2019/10/16

Actor has to place a Track.

ITStudent ITStudent

2019/10/16

#
Hello everyone. Im new at Greenfoot and i want to make a simple project. I want my actor, while he is "walking" he has to make a Track --> He has to place some Balls (name Track) , so when he touches the Balls he will turn .Like he is always going straight and my mission is that he has to touch the point at the Center (name Ball). is At edge is very helpful, but i kneed touching the balls (object) too but im not able to create them. The location where the balls will be created must be the same that the Actor is walking. Pls look at my script. I know im really bad at this, but hey im new at this. World: private void prepare() { Ball ball = new Ball(); addObject(ball,277,257); ball.setLocation(300,300); RobotMaBoi robotMaBoi = new RobotMaBoi(); addObject(robotMaBoi,0,600); robotMaBoi.setLocation(65,557); } Actor: public void act() { if (isAtEdge()){;move(-2); turn (270); move(1);} else{move(1); timer ++; if(timer == 4){ X = getX(); Y = getY();} if (timer == 5){ timer = 0;}} } Track: public class Track extends RobotMaBoi { /** * Act - do whatever the Track wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(timer == 5){Track a = new Track(); getWorld().addObject(a, 200, 400); setLocation(X,Y);} } }
danpost danpost

2019/10/16

#
I am trying to wrap my head around what you are trying to do. There seems to be some major issues that need to be dealt with if I understand correctly. One thing I immediately saw was the improper class used for what Track extends. A track is an object, so a class can be used to describe it (which means it can extend the java.lang.Object class). A track needs to be put in a world; so, it needs to be an actor (which means it should extend the greenfoot.Actor class -- which itself extends Object). However, is a track any type of RobotMaBoi object? I say, "No". It does not look like, sound like or act anything like a robot. Another potential issue is that you are having Track objects create Track objects (see first line in Track act method). The number of tracks in your world will quickly grow to where (in short order) your scenario will lag and crawl towards a seeming halt. Generally speaking, a class should not create an object of its own type ("new ClassName()" should not be used in a ClassName class).
ITStudent ITStudent

2019/10/17

#
Oh i didnt said it: So The Class Actor has 2 Subclasses: Ball (the center that should be touched by the Robot(MaBoi)); RobotMaBoi. And RobotMaBoi is the Robot who should walk and place a track behind it. Thats why it has a subclass called Track. And if my robot touches the border or the Track it has to rotate and finally arrive the center The Problem is The Track is not been created on the Locations of the Robot. 2. What you said: The number of tracks is quickly growing. And because of that the scenario lags. I just want something like a border so the bot identifies it and does the same like atEdge. Thank You for replying fast. I hope you can help me out of this situation.
danpost danpost

2019/10/17

#
ITStudent wrote...
The Class Actor has 2 Subclasses: Ball ... And RobotMaBoi
And I am saying you should have 3 Actor subclasses -- Ball, RobotMaBoi and Track. If you want Track to be somehow linked to RobotMaBoi, then put the Track class (still, extending Actor) in the RobotMaBoi class:
import greenfoot.*;

public class RobotMaBoi extends Actor
{
    // RobotMaBoi class codes
    
    private class Track extends Actor
    {
        // Track class codes
    }
}
The Problem is The Track is not been created on the Locations of the Robot. 2. What you said: The number of tracks is quickly growing. And because of that the scenario lags. I just want something like a border so the bot identifies it and does the same like atEdge.
To avoid lagging, have the RobotMaBoi object create the Track objects. After placing at current location, give the rotation of the robot to the track and move it behind the robot (so it will not be immediately touching):
Actor track = new Track();
getWorld().addObject(track, this.getX(), this.getY());
track.setRotation(this.getRotation());
track.move(-20); // adjust to not be touching
Then you can:
// change this
if (isAtEdge()) ...
// to this
if (isAtEdge() || isTouching(Track.class)) ...
ITStudent ITStudent

2019/10/18

#
Okay. I will try. :)
ITStudent ITStudent

2019/10/18

#
Thanks a lot! :) Its not like i had imagined, because of the gaps and so on, but its fine. If you have an idea how i can edit that with the big gaps i would like to hear. But afterall my school-mission is completed. Thank You. Thats my script: myworld: private void prepare() { Ball ball = new Ball(); addObject(ball,277,257); ball.setLocation(300,300); RobotMaBoi robotMaBoi = new RobotMaBoi(); addObject(robotMaBoi,80,600); robotMaBoi.setLocation(80,565); } RobotMaBoi: public class RobotMaBoi extends Actor {public boolean isAtEdge; protected boolean isTouching​; /** * Act - do whatever the RobotMaBoi wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(isTouching(Ball.class)){move(0);}else{ if (isAtEdge()||isTouching(Track.class)){ move(-35); turn (270); move(5);tracking(); }else{move(5);tracking();}} } public void tracking(){ Actor track = new Track(); getWorld().addObject(track, this.getX(), this.getY()); track.setRotation(this.getRotation()); track.move(-75);} } Ps: Thank you! Have a nice weekend!
You need to login to post a reply.