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

2013/11/16

Movement help

Banginwithflavor Banginwithflavor

2013/11/16

#
Hello. I need help with the thrusters i am making for a spaceship in my game. I am trying to move them to the appropriate spot according to the rotation of the ship. I cant figure out what i did to create them in the set position that they are in. Can i get some help/ideas? Scenario
danpost danpost

2013/11/17

#
The easiest way is to add them into the world at the location of the spaceship, set its rotation to that of the spaceship and move by the offset from that position. If there is also an offset along the perpendicular axis, the turn 90 degrees, move by that offset and return to the previous rotation.
Banginwithflavor Banginwithflavor

2013/11/17

#
Ugh i feel so silly. Made a stupid mistake. Thanks.
Banginwithflavor Banginwithflavor

2013/11/17

#
However while were at it i also had another problem. I couldn't figure out a way to create the turrets once and have them follow the shit as well.
danpost danpost

2013/11/17

#
Since each ship will have a turret, each ship (which I will say is a Ship class object) should know which turret (which I will say is a Turret class object) belongs to it (or each turret should know which ship it belongs to). That means that either the Ship class will need an instance Turret field or the Turret class will need an instance Ship field. It would be easiest if the instance field was in the class that contains the movement code so both can be moved simultaneously. Whenever you create a new set of Ship and Turret objects, set the instance field to its paired object.
Banginwithflavor Banginwithflavor

2013/11/17

#
oops meant to say ship not the other word. Do you mean something like this?
    public ship()
    {
        Turret turret1 = new Turret(); 
        getWorld().addObject(turret1,getX(),getY());
    }
danpost danpost

2013/11/17

#
Not quite. I meant that when you create the ship using 'new Ship', you should create its turret along with it using 'new Turret' and inform one object of the other one by either passing it as an argument in the constructor call or calling a method in the class to set the instance object field to the other object.
// in world or wherever you create the objects
Turret turret = new Turret();
Ship ship = new Ship();
ship.setTurret(turret);
// in Ship class
// add instance object field
private Turret turret;
// add method
public void setTurret(Turret turret)
{
    this.turret = turret;
}
Banginwithflavor Banginwithflavor

2013/11/17

#
Aha! Got it. Thanks again!
You need to login to post a reply.