LibGDX设置加速度错误

时间:2016-03-03 02:45:20

标签: java android android-studio libgdx

我正在学习用于游戏开发的LibGDX,我的游戏在Box2D上运行得差不多但是只剩下一件事,为我的敌人设置加速度,当我尝试这样做时(我添加了float timer = 0;值,在我的EnemyUserData类上创建一个render方法,并将以下代码添加到它

@Override
    public void render(float delta, float speed) {
        timer += Gdx.graphics.getDeltaTime(); //returns time between two frames
        if (timer > 10) { //after 10 seconds
            linearVelocity = linearVelocity * 1.1F;
            timer = 0; //reset timer
        }
    }
}

但程序开始抱怨错误,例如:

Error:(20, 25) Gradle: error: incompatible types: float cannot be converted to Vector2Error:(33, 4) Gradle: error: method does not override or implement a method from a supertypeError:(37, 44) Gradle: error: bad operand types for binary operator '*'
first type:  Vector2
second type: float

EnemyUserData类,以防您需要更多信息:

package com.avoidcrashjump.box2d;

import com.avoidcrashjump.Constants;
import com.avoidcrashjump.enums.UserDataType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;

/**
 * Created by Felipe on 3/1/2016.
 */
public class EnemyUserData extends UserData {

    float timer = 0;
    float speed = 200f;
    private Vector2 linearVelocity;

    public EnemyUserData(float width, float height) {
        super(width, height);
        userDataType = UserDataType.ENEMY;
        linearVelocity = speed;

    }

    public void setLinearVelocity(Vector2 linearVelocity) {
        this.linearVelocity = linearVelocity;
    }

    public Vector2 getLinearVelocity() {
        return linearVelocity;
    }


    @Override
    public void render(float delta, float speed) {
        timer += Gdx.graphics.getDeltaTime(); //returns time between two frames
        if (timer > 10) { //after 10 seconds
            linearVelocity = linearVelocity * 1.1F;
            timer = 0; //reset timer
        }
    }
}

有人可以向我解释我做错了什么吗?我尝试过从Vector2更改为float,但是没有用。 EnemyUserData类的新代码如下:

package com.avoidcrashjump.box2d;

import com.avoidcrashjump.Constants;
import com.avoidcrashjump.enums.UserDataType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Vector2;

/**
 * Created by Felipe on 3/1/2016.
 */
public class EnemyUserData extends UserData {

    float timer = 0;
    float speed = 200f;
    private Vector2 linearVelocity;

    public EnemyUserData(float width, float height) {
        super(width, height);
        userDataType = UserDataType.ENEMY;
        linearVelocity = speed;

    }

    public void setLinearVelocity(Vector2 linearVelocity) {
        this.linearVelocity = linearVelocity;
    }

    public Vector2 getLinearVelocity() {
        return linearVelocity;
    }


    @Override
    public void render(float delta, float speed) {
        timer += Gdx.graphics.getDeltaTime(); //returns time between two frames
        if (timer > 10) { //after 10 seconds
            linearVelocity = linearVelocity * 1.1F;
            timer = 0; //reset timer
        }
    }
}

这是我制作游戏的唯一问题。

1 个答案:

答案 0 :(得分:2)

基本物理:速度一个向量,你在每个轴(X,Y,Z ......)都有单独的速度,所以你不能只是拳头拳头速度变成简单的标量值。

似乎LibGDX的向量不会覆盖*运算符,所以你必须像这样缩放向量:

linearVelocity.scl(1.1);