Android Studio,LibGDX在经过一定时间后自动增加速度

时间:2016-03-02 20:00:14

标签: java android android-studio libgdx

我正在制作一个项目,我正在学习LibGDX,但到目前为止,该项目一直很顺利,但是,教程没有显示如何在经过一定时间后提高速度,ex(10之后)秒已过,我希望我的敌人将速度提高到0.1f,这意味着10%,再经过10秒后,自动将速度提高到0.2f,依此类推。)这是代码中唯一的部分我需要改变以使他们的敌人提高他们的速度public static final Vector2 ENEMY_LINEAR_VELOCITY = new Vector2(-10f, 0);教程在这里('http://williammora.com/a-running-game-with-libgdx-part-1/')我该怎么做?有人可以帮帮我吗?我将不胜感激!

常数类:

package com.avoidcrashjump;

import com.badlogic.gdx.math.Vector2;


/**
 * Created by Felipe on 2/29/2016.
 */
public class Constants {
    public static final int APP_WIDTH = 1024;
    public static final int APP_HEIGHT = 640;

    public static final Vector2 WORLD_GRAVITY = new Vector2(0, -10);

    public static final float GROUND_X = 0;
    public static final float GROUND_Y = 0;
    public static final float GROUND_WIDTH = 50f;
    public static final float GROUND_HEIGHT = 2f;
    public static final float GROUND_DENSITY = 0f;
    public static final float PLAYER_X = 2;
    public static final float PLAYER_Y = GROUND_Y + GROUND_HEIGHT;
    public static final float PLAYER_WIDTH = 1f;
    public static final float PLAYER_HEIGHT = 1f;
    public static final float PLAYER_GRAVITY_SCALE = 2.5f;
    public static float PLAYER_DENSITY = 0.5f;
    public static final float PLAYER_DODGE_X = 2f;
    public static final float PLAYER_DODGE_Y = 1.5f;
    public static final Vector2 PLAYER_JUMPING_LINEAR_IMPULSE = new Vector2(0,13f);
    public static final float PLAYER_HIT_ANGULAR_IMPULSE = 10f;
    public static final float ENEMY_X = 25f;
    public static final float ENEMY_DENSITY = PLAYER_DENSITY;
    public static final float RUNNING_SHORT_ENEMY_Y = 1.5f;
    public static final float RUNNING_LONG_ENEMY_Y = 2f;
    public static final float FLYING_ENEMY_Y = 3f;
    public static final Vector2 ENEMY_LINEAR_VELOCITY = new Vector2(-10f, 0);


}

1 个答案:

答案 0 :(得分:1)

您的班级名为Constants,您的速度使用final宣告。课程名称告诉我们,我们不应该尝试更改这些内容,final不允许我们这样做。

但是如果您更改了变量声明(将其移动到某处并删除final关键字)。为了增加它,您可以使用Gdx.graphics.getDeltaTime()

创建一个变量来计算传递时间:

float timer = 0;

然后在你的render()方法中

timer += Gdx.graphics.getDeltaTime(); //returns time between two frames
if(timer > 10) { //after 10 seconds
    speed = speed * 1.1F;
    timer = 0; //reset timer
}