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

2020/10/15

how to spawn a Actor twice and give them diffrend movement

EddiMitY EddiMitY

2020/10/15

#
I had a question, i want to spawn a car in my wold, if the car touch the end of the map it will spawn on the other side. this works perfekt. But i want to spawn a seccond car(same actor) on the other side who moves in the other direction and do the same like the first car. My english is pretty bad, soory for that but i tryed my best. Eddy
RcCookie RcCookie

2020/10/15

#
What you want is to have multiple instances of the same class. Say you have a class Car:
Car c1 = new Car();
Car c2 = new Car();
System.out.println(c1 == c2); // Prints false, different instances of the same type/ class
You can add each of them to the world just like normal:
// In your world class
addObject(c1, 100, 100); // Adds p1 at the coordinates (100|100)
But you want the second instance to move in the other direction. So you need to pass some kind of argument when creating the player instance that tells it which way to go. Therefore, you can use an integer that describes the objects rotation:
// In Car class

public Car(int rotation) {
    setRotation(rotation);
}
Now when you create a new car, you have to specify the car’s rotation. For a car driving to the right, the argument should be 0 or a multiple of 360 (-360,0,360...). For cars driving to the left pass 180. Example:
// In your world class:

Player carLeft = new Car(180);
Player carRight = new Car(0);

addObject(carLeft, 598, 200);
addObject(carRight, 1, 200);
danpost danpost

2020/10/15

#
EddiMitY wrote...
I had a question, i want to spawn a car in my wold, if the car touch the end of the map it will spawn on the other side. this works perfekt. But i want to spawn a seccond car(same actor) on the other side who moves in the other direction and do the same like the first car.
// in MyWorld
private void addCarGoingRight()
{
    Car car = new Car();
    addObject(car, 0, getHeight()/2+50);
}

private void addCarGoingLeft()
{
    Car car = new Car();
    car.setRotation(180);
    addObject(car, getWidth(), getHeight()/2-50);
}

// in Car
public void act()
{
    move(2);
    if (isAtEdge()) setLocation(getRotation() == 0 ? 0 : getWorld().getWidth(), getY());
}
DatHeroAndy DatHeroAndy

2020/10/15

#
Assign the Constructor of your Car with this variable: public Car(int turner) Car Code:
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class Car extends Actor
{
    private final int turner;

    /**
     * 
     */
    public Car(int turner)
    {
        this.turner = turner;
        this.turn(turner);
    }

    /**
     * Act - do whatever the Car wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        this.move(3);
        if (this.isAtEdge() && this.turner == 0) {
            this.setLocation(getX() - (getX() - 1), getY());
        }
        if (this.isAtEdge() && this.turner == 180) {
            this.setLocation(getX() + getWorld().getWidth(), getY());
        }
    }
}
In Act, I did that If the Rotation is 0, the car should teleport back to the beginning of the world when he hits the edge. And If the Rotation is 180, the car teleports to the other end of the world. Then add the Rotation as integers to your cars in the MyWorld Class. MyWorld Code:
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     */
    public MyWorld()
    {
        super(600, 400, 1);
        this.addObject( new  Car(0), this.getWidth() / 2, 200);
        this.addObject( new  Car(180), this.getWidth() / 2, 300);
    }
}
This code only works if you want your cars to go horizontally. If you want your cars to go vertically, you have to change the code.
You need to login to post a reply.