2016-08-10 12 views
1

私はこのコードを使用していますが、何かが完璧ではありません。それは働いているが滑らかではない。最初にtouchXに行き、touchYになります。しかし、私はこれを同時に望みます。私は別のコードを試みたが、うまくいかなかった。どうすれば修正できますか?どうしたの?このセクションではJava LibGDX And​​roidのタッチ位置に移動

package com.anil.game1; 
import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.math.Rectangle; 
import com.badlogic.gdx.math.Vector3; 
public class Game1 extends ApplicationAdapter { 
    private SpriteBatch batch; 
    private OrthographicCamera camera; 
    private Texture player1Texture; 
    private Rectangle player1Rectangle; 
    private Vector3 touchPos; 
    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     camera = new OrthographicCamera(480, 800); 
     player1Texture = new Texture("Player1.png"); 
     player1Rectangle = new Rectangle(); 
     player1Rectangle.set(240, 0, 64, 64); 
     touchPos = new Vector3(); 
    } 
    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.setProjectionMatrix(camera.combined); 
     batch.begin(); 
     if (Gdx.input.isTouched()) { 
      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
      camera.unproject(touchPos); 
      //player1Rectangle.setPosition(new Vector2(touchPos.x, touchPos.y)); 
     } 
     if (touchPos.x > player1Rectangle.x) 
      player1Rectangle.x += 5; 
     else 
      player1Rectangle.x -= 5; 
     if (touchPos.y > player1Rectangle.y) 
      player1Rectangle.y += 5; 
     else 
      player1Rectangle.y -= 5; 
     batch.draw(player1Texture, player1Rectangle.x - 32, player1Rectangle.y - 32); 
     batch.end(); 
    } 
    @Override 
    public void dispose() { 
     batch.dispose(); 
     player1Texture.dispose(); 
    } 
} 
+0

、チェックマークを使用して、以下の答えをしてください受け入れます。それがここで解決された投稿をマークする方法です。 –

答えて

1

if (touchPos.x > player1Rectangle.x) 
     player1Rectangle.x += 5; 
    else 
     player1Rectangle.x -= 5; 
    if (touchPos.y > player1Rectangle.y) 
     player1Rectangle.y += 5; 
    else 
     player1Rectangle.y -= 5; 

あなたは常に設定量によってXとY方向に移動する長方形を強制されています。まだ座ったり、ゆっくりと座ったりすることは許されません。また、おそらく、XとYを別々に扱うことは望ましくありません。なぜなら、その速度は基本方位では遅く、対角線方向ではより速く、これは奇妙に見えるからです。最後に、安定した移動速度を保つために、スピードと時間で作業する必要があります。

まず、オブジェクトの速度を定義します。また、計算に便利なVector2が必要になります。あなたはタッチ位置を計算した後

private static final float SPEED = 300f; //world units per second 
private final Vector2 tmp = new Vector2(); 

は今、あなたは内に移動すると、どこまでその方向に沿って移動するのか方向を把握したいです。あなたisTouchedブロックの後にそう右:

//how far the player can move this frame (distance = speed * time): 
float maxDistance = SPEED * Gdx.graphics.getDeltaTime(); 

//a vector from the player to the touch point: 
tmp.set(touchPos.x, touchPos.y).sub(player1Rectangle.x, player1Rectangle.y); 

if (tmp.len() <= maxDistance) {// close enough to just set the player at the target 
    player1Rectangle.x = touchPos.x; 
    player1Rectangle.y = touchPos.y; 
} else { // need to move along the vector toward the target 
    tmp.nor().scl(maxDistance); //reduce vector length to the distance traveled this frame 
    player1Rectangle.x += tmp.x; //move rectangle by the vector length 
    player1Rectangle.y += tmp.y; 
} 
+0

このコードをマルチタッチするにはどうすればよいですか? –

+0

InputListenerを実装し、そこでコードを移動します。あなたは提供されているポインタパラメータで指を区別することができます。 – Tenfour04

+0

マルチタッチを行うためにforループを使用しましたが、2つのプレイヤーの速度は同じではありません。 Player1はplayer2よりも速いです。 InputListenerについて少し説明できますか?私が何を意味するのか知っていれば幸いですありがとう...このコードにInputProcessorを使用するにはどうすればよいですか? –

関連する問題