2
\$\begingroup\$

I was currently developing tower of defense game and I want to implement 2x feature for my game. Game usually run with 1x speed that was normal speed of the game.

Here what 1x and 2x mean : 1x - mention normal speed of the game, 2x - mention the game object moves with double speed means user experience the fast game play.

I want to implement such functionality for my game. The functionality that I want contains in the game Medieval Castle game that was available in the market.

https://play.google.com/store/apps/details?id=com.nova.root&feature=search_result#?t=W251bGwsMSwxLDEsImNvbS5ub3ZhLnJvb3QiXQ..

The screen shot also shows the 1x and 2x button in that game. I think for 2x speed of the game I have to increase the speed of each object that were in the game.

So any member please help what to do for that implementation. Only idea become enough for me.

\$\endgroup\$
1
  • \$\begingroup\$ If you use a value when you move objects, simply multiply everything with the speed. So if you have method MoveObject(int x, int y) and in it you have a global variable that multiply the speed . Player.Location.X += (x*GameSpeed) for example. Then, whenever you need to change the speed you just increase or decrease the global value (GameSpeed). \$\endgroup\$
    – Deukalion
    Dec 13, 2012 at 8:00

3 Answers 3

5
\$\begingroup\$

In most update methods there is a delta, which provides the number of milliseconds (or some other measurement of time) since the last update was called. By calculating all of your movement calculations using both a delta and a movement speed, you get movement that is unaffected by a fluctuating frame rate.

The following is code pulled from the movement system of an Asteroids clone I'm working on (but modified to make my point more clear):

    public void process(Entity e, int delta) {
       // Imagine that velocity is a vector
       x += e.velocity.getX() * (world.delta / (float) 1000);
       y += e.velocity.getY() * (world.delta / (float) 1000);
    }

Notice that the actual change of coordinates happens based on the amount of time passed. By modifying the delta passed into your system with some multiplier, you can make things in your game move more quickly or slowly:

    public void process(Entity e, int delta, float timeScale) {
       // if timeScale were .5, the game would slow. If it were 2, the game would speed up.
       x += e.velocity.getX() * (world.delta / (float) 1000) * timeScale;
       y += e.velocity.getY() * (world.delta / (float) 1000) * timeScale;
    }
\$\endgroup\$
3
\$\begingroup\$

I'd just like to add another answer here.

When performing speedup by changing the timestep, it might mean the behaviour is different. For example, if you have some physics in there with euler integration, the behaviour at double speed will diverge over time.

This is not a problem in 99% of cases, but it is something you should be aware of.

If you are multiplying speed by an integer as you are in your example, I would suggest doing multiple updates per frame with fixed timestep. This will ensure your simulation always behaves exactly the same way but at double, triple or quadruple speed.

This is of course more cpu intensive because you are doing double the simulation processing.

One very important thing to note: your update and drawing functionality should be completely separate, but this should ALWAYS be the case anyway, in any project. What I mean is, your display function should not change ANY state, it should ONLY draw the scene at the current time.

I'm not saying this is how you should do it, but it is very simple to implement and is another method to think about.

void mainLoop() 
{
    for(int i=0; i<NUMBER_OF_UPDATE_STEPS; ++i) update(FIXED_TIME_STEP);
    draw();
}
\$\endgroup\$
2
\$\begingroup\$

I know the thing that I am answering my own question but I found suitable answer so I think I have to post it for other user and save their time.

  public class BaseScene extends Scene {

    private float speed_factor = 1f;

        public float getSpeedFactor() {
            return speed_factor;
        }

        public void setSpeedFactor(float new_factor) {
            this.speed_factor = new_factor;
        }

        @Override
        protected void onManagedUpdate(float pSecondsElapsed) {
            super.onManagedUpdate(pSecondsElapsed * speed_factor);
        }
    }

Develop this type of subclass and use this one for you game then on click of 1x and 2x button apply the value that you want suitable for your game.

There is no need to handle each character manually only handle scene and all the thing done for you.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .