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

2020/4/12

add object arount circle

Starlord_20 Starlord_20

2020/4/12

#
in the center of the world i have an actor called "Center" and a circle with a diameter of 150 pixels around it. I want to add a "Target" actor somewhere arount the circle. then thefollowing should happen: center sets his rotation in direction to target and summons 2 new actors +45 degrees and -45 degrees on the surface of the circle. These 2 actors go on the surface to each other until they are at the same position, center looks at. I hope you'll understand what I mean... thats waht i wrote so far:
public class Center extends Actor
{
    void targeting() {
        Target t = getWorld().getObjects(Target.class).get(0);
        if(t != null) {
            int tx = t.getX();
            int ty = t.getY();
            this.turnTowards(tx,ty);
        }
    }
}
public class Main extends Actor
{
    Main() {
        GreenfootImage g = new GreenfootImage(150,150);
        g.setColor(Color.BLACK);
        g.drawOval(0,0,150,150);  
        g.fillOval(0,0,150,150);
        
        g.setColor(Color.LIGHT_GRAY);
        g.drawOval(5,5,140,140);
        g.fillOval(5,5,140,140);
        
        this.setImage(g);
    }
    
    
}
My main problems are : - spawn the 2 actors on circle's surface(get those positions) - let the actors move around the circle
danpost danpost

2020/4/12

#
As an aside -- concerning lines 4 and 5 in Center code: Line 5 asks if t is null. It will never be null if there was at least one element in the list of Target objects in the world. If no Target objects were in the world,, you would get an IndexOutOfBoundsException on line 4. Therefore, if a Target object is guaranteed to be in the world, line 5 would be an unnecessary check. Since it appears that a Target object is not always in the world and is added to cause a shot from the Center object, you will want to start the targeting method with:
if ( ! getWorld().getObjects(Target.class).isEmpty())
followed by your line 4 without having to check for a null value. For adding the two objects, add them at Center object location. Turn them to target, then turn them 45 degrees in opposite directions. Then move them a distance of 75. That will put them at their starting position. The two objects will need to know whether their paths will be clockwise or counter-clockwise. An int speed value might help them out both with determining if moving as well as in which direction. The speed can also be an angular speed (change in degrees of rotation of vector from Center location (moving by going back to Center location, adjusting rotation and moving 75 to the perimeter again).
Starlord_20 Starlord_20

2020/4/13

#
I now made it like that:
public void act() {
        java.util.List targets = getWorld().getObjects(Target.class);
        if(!targets.isEmpty()) {
            Target t = (Target) targets.get(0);
            int tx = t.getX();
            int ty = t.getY();
            Activator act1 = new Activator();
            Activator act2 = new Activator();

            this.turnTowards(tx,ty);
            this.setRotation(getRotation() +45);
            getWorld().addObject(act1,this.getX(),this.getY());
            act1.setRotation(this.getRotation());
            act1.move(75);

            this.setRotation(getRotation() -90);
            getWorld().addObject(act2,this.getX(),this.getY());
            act2.setRotation(this.getRotation());
            act2.move(75);
            this.turnTowards(tx,ty);
            if((act1.getX() == act2.getX()) && (act1.getY() == act2.getY())) {
                GreenfootImage g = getWorld().getBackground();
                g.setColor(Color.RED);
                g.drawLine(act1.getX(),act1.getY(),tx,ty);
            }
        }
    }
the Activator class corresponds to those 2 actors moving around the surface. But I don't know exactly what you mean... I think if I would make it about the way you described me, Line 21in this code won't work
danpost danpost

2020/4/13

#
If I am not mistaken, you are pretty much done in this class by line 20. Anything done after appropriately placing the Activator objects in the world will be done in the Activator class. What were you expecting to do with lines 21 thru 25 Also, should not the Target object be removed after line 6 in this code? If not, you will be continually creating Activator objects.
Starlord_20 Starlord_20

2020/4/13

#
My goal was actually to create a kind of "phaser beam". As soon as the target has been identified, the objects of the Activator class should move towards each other until they meet and then bundle a beam. I don't know if you can imagine anything about it. I solved the beam here with drawLine() after that, the target will be removed. I wanted to solve that with only one act cycle and use a loop in activator class.
danpost danpost

2020/4/13

#
Starlord_20 wrote...
I wanted to solve that with only one act cycle and use a loop in activator class.
You cannot create an animation within only one act cycle. It requires well over 10 frames (act steps) per second for the eye to be deceived into continuous motion. That is why old film reels ran between 24 and 40 fps and standard videos run between 24 and 60 fps.
Starlord_20 Starlord_20

2020/4/13

#
so what could I do instead?
danpost danpost

2020/4/13

#
Starlord_20 wrote...
so what could I do instead?
danpost wrote...
Anything done after appropriately placing the Activator objects in the world will be done in the Activator class.
the Target object be removed after line 6
You need to login to post a reply.