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

2017/12/19

Adding my old move method but with new variables?

Recorsi Recorsi

2017/12/19

#
How can i make the first code function like the second but with the xVel,yVel variables?
private void accelerateByKeys()
   {
       if(Greenfoot.isKeyDown("r"))
       {
           xVel=0;
           yVel=0;
       }
       if(Greenfoot.isKeyDown("d"))
       {
           xVel+=0.06;
           setRotation(180);
       }
       if(Greenfoot.isKeyDown("a"))
       {
           xVel-=0.06;
            setRotation(0);
       }
       if(Greenfoot.isKeyDown("s"))
       {
           yVel+=0.06;
           setRotation(-90);
       }
       if(Greenfoot.isKeyDown("w"))
       {
           yVel-=0.06;
           setRotation(90);
           EngineSound.playLoop();
           EngineSound.setVolume(100);
       } else 
       {
           EngineSound.pause();
       }
   }
  private void MoveShip()
   {
       if (Greenfoot.isKeyDown("a"))
        {
             turn(-4);
        } 
       if (Greenfoot.isKeyDown("d"))
        {
             turn(4);
        }
       if (Greenfoot.isKeyDown("w"))
        { 
           move(3);
       if (Greenfoot.isKeyDown("w"))
       {
          EngineSound.playLoop();
          EngineSound.setVolume(100);
       } else 
       {
          EngineSound.pause();
       }
   }
protected double xVel,yVel;
danpost danpost

2017/12/19

#
First, it does no good to use double values for velocity unless you are tracking the current location of the actor using double values. So, beginning with the second code above, add:
protected double xLoc, yLoc;
You should then override the 'addedToWorld' method so you can set the initial values of these fields:
protected void addedToWorld(World world)
{
    xLoc = getX();
    yLoc = getY();
}
Next, if you are going to use the rotation to determine the accelerated direction, you only need one velocity field. The "a" and "d" keys should still turn the actor and the "w" key should accelerate it (I am presuming that is what you want, otherwise why add the double value fields for velocity at all -- unless you really just wanted smooth movement at a constant velocity). You could add some drag value to the speed for slowing down or utilize the "d" key for that purpose. You will then need to limit the slowing down or drag so that the speed does not go below zero. You may also want to limit the speed at some upper value when accelerating. Moving will need to be unconditional and trig will need to be used to determine the adjusted double values for the new location that the actor will be set at with the values converted to int values (not to be set to the fields).
Recorsi Recorsi

2017/12/22

#
Somehow overriding the 'addedToWorld' method messes up my world.
danpost danpost

2017/12/22

#
Recorsi wrote...
Somehow overriding the 'addedToWorld' method messes up my world.
What class does the class you put the method in extend? if not the Actor class, does it also have an 'addedToWorld(World)' method in it?
Recorsi Recorsi

2017/12/23

#
The class is a subclass of the "ScrollingObjects" class. And yes it has an addedToWorld method in it.
protected void addedToWorld(World world)
    {
       xCoord=getX();
       yCoord=getY();
       drawMe();
    }
danpost danpost

2017/12/23

#
In the overriding method add the following line:
super.addedToWorld(world);
You need to login to post a reply.