在此示例中,非平滑 2d 精灵移动的原因是什么
what reason is there for non-smooth 2d sprite movement in this example
正在为 android 制作 space 入侵者风格的游戏。
我希望玩家能够在角色右侧或左侧(屏幕底部)的任何位置触摸屏幕以将他移动到那个方向。
代码编译没有错误,玩家确实移动了,但是
A) 他的动作比我预期的要慢得多
B) 运动是 'jittery',尽管我已经以几种不同的方式将运动速度乘以 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();
}
}
游戏管理器class:
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();
}
}
玩家class:
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() {
}
}
}
仔细看看你的逻辑。
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
方法中是否应该使用 x
或 deltaX
来更改目标 X 取决于您。不同类型的游戏。
正在为 android 制作 space 入侵者风格的游戏。 我希望玩家能够在角色右侧或左侧(屏幕底部)的任何位置触摸屏幕以将他移动到那个方向。 代码编译没有错误,玩家确实移动了,但是 A) 他的动作比我预期的要慢得多 B) 运动是 'jittery',尽管我已经以几种不同的方式将运动速度乘以 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();
}
}
游戏管理器class:
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();
}
}
玩家class:
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() {
}
}
}
仔细看看你的逻辑。
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
方法中是否应该使用 x
或 deltaX
来更改目标 X 取决于您。不同类型的游戏。