有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在这个例子中,2d精灵的非光滑运动有什么原因

正在为安卓制作一款太空入侵者风格的游戏。 我希望玩家能够触摸屏幕上角色左右的任何位置(在屏幕底部),让他朝那个方向移动。 代码编译时不会出错,游戏也会移动,但是 A) 他比我想象的要慢得多 B) 虽然我以几种不同的方式将移动速度乘以deltatime,但这个动作还是“神经过敏”

请有人看一下我的代码,告诉我哪里出了问题-

package com.moneylife.stashinvaders;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class StashInvaders extends ApplicationAdapter {
    GameManager gameManager;
    @Override
    public void create () {
        gameManager = new GameManager();

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        gameManager.update();
        gameManager.draw();

    }

    @Override
    public void dispose () {
        gameManager.spriteBatch.dispose();

    }
}

GameManager类:

package com.moneylife.stashinvaders;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/**
 * Created by Dave on 12/08/2016.
 */
public class GameManager {
    SpriteBatch spriteBatch;
    Player player1;

    public GameManager(){
        spriteBatch = new SpriteBatch();
        player1 = new Player();
    }

    public void update(){
        player1.update();
    }

    public void draw(){
        spriteBatch.begin();

        player1.draw(spriteBatch);

        spriteBatch.end();
    }
}

玩家等级:

package com.moneylife.stashinvaders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;

/**
 * Created by Dave on 12/08/2016.
 */
public class Player {
    Vector2 position;
    Texture texture;
    int speed = 50;
    float deltaTime;

    public Player(){
        Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector()));
        texture = new Texture("bazookaman.png");
        position = new Vector2(Gdx.graphics.getBackBufferWidth() / 2 - texture.getWidth() / 2, 0);
    }

    public void update(){
        deltaTime = Gdx.graphics.getDeltaTime();
    }

    public void draw(SpriteBatch spriteBatch){
        spriteBatch.draw(texture, position.x, position.y);
    }


    public class MyGestureDetector implements GestureDetector.GestureListener {
        @Override
        public boolean touchDown(float x, float y, int pointer, int button) {
            return false;
        }

        @Override
        public boolean tap(float x, float y, int count, int button) {
            return false;
        }

        @Override
        public boolean longPress(float x, float y) {
            return false;
        }

        @Override
        public boolean fling(float velocityX, float velocityY, int button) {
            return false;
        }

        @Override
        public boolean pan(float x, float y, float deltaX, float deltaY) {
            if (x > position.x){
                position.x += speed * deltaTime;
            }
            if (x < position.x){
                position.x -= speed * deltaTime;
            }
            return false;
        }

        @Override
        public boolean panStop(float x, float y, int pointer, int button) {
            return false;
        }

        @Override
        public boolean zoom(float initialDistance, float distance) {
            return false;
        }

        @Override
        public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
            return false;
        }

        @Override
        public void pinchStop() {

        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    仔细看看你的逻辑

    if (x > position.x){
        position.x += speed * deltaTime; //if close to touch point, position.x is now bigger than x
    }
    if (x < position.x){ //if we just moved right past the touch point, undo it
        position.x -= speed * deltaTime;
    }
    

    此外,此pan方法将仅在轮询手指位置并发现其已移动的帧上调用。手指位置不会像你的游戏可能正在运行一样每秒轮询60次,而且移动也不会总是发生

    相反,您应该使用平移来修改角色的目标位置。在update()方法中,你总是可以以一定的速度向目标位置移动。这取决于你是否应该在pan方法中使用xdeltaX来改变你的目标X。不同类型的游戏