libgdx - 遇到问题建立我的对象走动? [英] libgdx - Having trouble establishing my objects to move around?

查看:123
本文介绍了libgdx - 遇到问题建立我的对象走动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为实践中Android项目和我有麻烦通过触摸四处移动拨片,就像他们滚动由左到右。我想过使用Gdx.input.isTouched但可悲的是似乎并没有在我自己的经验有任何建​​议或例子是很好地工作真的AP preciated?这是所有我希望这也许能澄清这个问题。

 私人无效updatePaddle2(浮点DT){
    如果(Gdx.input.isTouched(0)){
        paddle1.move(-350,0);        paddle1.Intergrate(DT);    }
私人无效更新(浮点DT){
    //桨更新
    updatePaddle1(DT);
    updatePaddle2(DT);}
私人无效resetRectangle(){
    paddle1.move((field.width * .5f),field.y +(field.height * .1F));
    paddle2.move((field.width * .5f),field.y +(field.height * .8f));
}

和类桨

 公共类桨扩展GameShare {私人ShapeRenderer paddleRenderer;
颜色paddleColor =新的色彩();
保护桨(){
    超级(100,32);
}公共无效paddleCreate(){
    paddleRenderer =新ShapeRenderer();}公共无效paddleRender(浮点DT){
    paddleRenderer.begin(ShapeType.Filled);
    paddleColorSwap(DT);
    drawPaddle(DT);
    paddleRenderer.end();
}私人无效drawPaddle(浮点DT){
    paddleRenderer.rect(this.getX(),this.getY(),this.getWidth(),this.getHeight());
    paddleRenderer.rect(this.getX(),this.getY(),this.getWidth(),this.getHeight());}
INT阈值= 7000;
长LastChanged = 0;
公共无效paddleColorSwap(浮点DT){
    如果(System.currentTimeMillis的() - LastChanged&所述阈)
        返回;
    INT RND =(INT)(的Math.random()* 6);
    开关(RND){
    情况下0:paddleColor.set(Color.GREEN);打破;
    案例1:paddleColor.set(Color.BLUE);打破;
    案例2:paddleColor.set(Color.RED);打破;
    案例3:paddleColor.set(Color.YELLOW);打破;
    案例4:paddleColor.set(Color.CYAN);打破;
    案例5:paddleColor.set(Color.ORANGE);打破;
    }
    LastChanged = System.currentTimeMillis的();
    paddleRenderer.setColor(paddleColor);}}

和扩展的类

 公共抽象类GameShare {私人Vector2位置=新Vector2();
私人Vector2速度=新Vector2();
私人矩形范围=新的Rectangle();
保护GameShare(INT宽度,高度INT){
    bounds.setWidth(宽);
    bounds.setHeight(高度);
}公共矩形的getBounds(){
    updateBounds();
    返回界限;
}公共无效的setBounds(矩形范围){
    this.bounds =界限;
}公共无效updateBounds(){
    bounds.set(position.x,position.y,bounds.width,bounds.height);
}公众持股量的getX(){
    返回position.x;
}公众持股量的getY(){
    返回position.y;
}公众持股量getVelocityX(){
    返回velocity.x;
}公众持股量getVelocityY(){
    返回velocity.y;
}
公共Vector2为getPosition(){
    返回的位置;
}公共无效setPosition两种(Vector2位置){
    this.position =位置;
}
公共Vector2 getVelocity(){
    返回的速度;
}公共无效setVelocity(Vector2速度){
    this.velocity =速度;
}公众持股量的底部(){
    返回bounds.y;
}公众持股量的getHeight(){
    返回bounds.height;
}公众持股量的getWidth(){
    返回bounds.width;
}公共无效G642.44各级(浮点DT){
    position.add((velocity.x * dt的),(velocity.y * dt的));
}公众持股量左(){
    返回bounds.x;
}公共无效移动(浮法X,浮法Y){
    position.set(X,Y​​);
}公众持股权(){
    返回bounds.x + bounds.width;
}公共无效setVelocity(浮法X,浮法Y){
    velocity.set(X,Y​​);
}公众持股量最高(){
    返回bounds.y + bounds.height;
}公共无效翻译(浮​​点X,浮Y){
    position.add(X,Y);
}


解决方案

updatePaddle2 的方法调用的moveTo 的桨,它在你的code直接设置的位置,所以它瞬间远距传物桨到新的位置-350单位了。然后调用 G642.44各级因为你还没有设置挡板的速度而没有做任何事情。由于 updatePaddle2 被称为每一帧,你的桨大概飞了屏幕在350单位每帧* 60帧= 21000台每秒速率。

根据什么好像你正在试图做的,你的方法应该是这个样子,虽然似乎有其他问题(如你为什么要更新 paddle1 updatePaddle2 的方法?)。

 私人无效updatePaddle2(浮点DT){
    如果(Gdx.input.isTouched(0)){
        paddle2.setVelocity(-350,0);
    }其他{
        paddle2.setVelocity(0,0);
    }    paddle2.Intergrate(DT);
}

您将需要更改您的GameShare类的 setVelocity 法取x和y作为参数,而不是一个新的Vector2。这是浪费的更换速度矢量,而不是仅仅修改它。

我也注意到,您没有更新的范围时你GameShare类中的位置更新。这是怎么回事,当你开始使用范围的东西,以后会导致问题。在$ C $(c)任何地方的位置被修改,界限'X和Y也应该更新以匹配。

I'm currently working on an android project for practice and I'm having trouble moving the paddles around by touch, like having them scroll from left to right. I thought about using Gdx.input.isTouched but sadly doesn't seem to work very well in my own experience any suggestions or examples would be really appreciated? this is all I have hopefully this may be able to clarify on the subject

private void updatePaddle2(float dt) {
    if(Gdx.input.isTouched(0)){
        paddle1.move(-350, 0);

        paddle1.Intergrate(dt);

    }


private void update(float dt) {


    //paddle update
    updatePaddle1(dt);
    updatePaddle2(dt);

}


private void resetRectangle(){
    paddle1.move((field.width * .5f), field.y + (field.height * .1f));
    paddle2.move((field.width * .5f), field.y + (field.height * .8f));
}

and the class for the paddles

public class Paddle extends GameShare{

private ShapeRenderer paddleRenderer;
Color paddleColor = new Color();


protected Paddle() {
    super(100, 32);
}

public void paddleCreate(){
    paddleRenderer = new ShapeRenderer();

}

public void paddleRender(float dt){
    paddleRenderer.begin(ShapeType.Filled);
    paddleColorSwap(dt);
    drawPaddle(dt);
    paddleRenderer.end();
}

private void drawPaddle(float dt) {
    paddleRenderer.rect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
    paddleRenderer.rect(this.getX(), this.getY(), this.getWidth(), this.getHeight());

}


int threshold = 7000;
long LastChanged = 0;
public void paddleColorSwap(float dt){
    if(System.currentTimeMillis() - LastChanged < threshold)
        return;
    int rnd = (int)(Math.random() * 6);
    switch(rnd){
    case 0: paddleColor.set(Color.GREEN);break;
    case 1: paddleColor.set(Color.BLUE);break;
    case 2: paddleColor.set(Color.RED);break;
    case 3: paddleColor.set(Color.YELLOW);break;
    case 4: paddleColor.set(Color.CYAN);break;
    case 5: paddleColor.set(Color.ORANGE);break;
    }
    LastChanged = System.currentTimeMillis();
    paddleRenderer.setColor(paddleColor);

}

}

and the extended class

public abstract class GameShare {

private Vector2 position = new Vector2(); 
private Vector2 velocity = new Vector2();
private Rectangle bounds = new Rectangle();


protected GameShare(int width, int height){
    bounds.setWidth(width);
    bounds.setHeight(height);
}

public Rectangle getBounds(){
    updateBounds();
    return bounds;
}

public void setBounds(Rectangle bounds){
    this.bounds = bounds;
}

public void  updateBounds(){
    bounds.set(position.x, position.y, bounds.width, bounds.height);
}

public float getX(){
    return position.x;
}

public float getY(){
    return position.y;
}

public float getVelocityX(){
    return velocity.x;
}

public float getVelocityY(){
    return velocity.y;
}


public Vector2 getPosition() {
    return position;
}



public void setPosition(Vector2 position) {
    this.position = position;
}


public Vector2 getVelocity() {
    return velocity;
}



public void setVelocity(Vector2 velocity) {
    this.velocity = velocity;
}

public float bottom(){
    return bounds.y;
}

public float getHeight(){
    return bounds.height;
}

public float getWidth(){
    return bounds.width;
}

public void Intergrate(float dt){
    position.add((velocity.x * dt), (velocity.y * dt));
}

public float left(){
    return bounds.x;
}

public void move(float x, float y){
    position.set(x, y);
}

public float right(){
    return bounds.x + bounds.width;
}

public void setVelocity(float x, float y){
    velocity.set(x, y);
}

public float top(){
    return bounds.y + bounds.height;
}

public void translate(float x, float y){
    position.add(x, y);
}

解决方案

Your updatePaddle2 method calls moveTo on the paddle, which in your code sets the position directly, so it instantly teleports the paddle to a new position -350 units away. Then you call Intergrate which doesn't do anything since you haven't set a velocity on the paddle. Since updatePaddle2 is called on every frame, your paddle is probably flying off the screen at a rate of 350 units per frame * 60 fps = 21,000 units per second.

Based on what it seems like you're trying to do, your method should look something like this, although there seem to be other issues (like why are you updating paddle1 in the updatePaddle2 method?).

private void updatePaddle2(float dt) {
    if (Gdx.input.isTouched(0)){
        paddle2.setVelocity(-350, 0);
    } else {
        paddle2.setVelocity(0, 0);
    }

    paddle2.Intergrate(dt);
}

You'll need to change your setVelocity method in your GameShare class to take x and y as arguments instead of a new Vector2. It's wasteful to replace the velocity vector instead of just modifying it.

I also noticed that you don't update the bounds when the position updates in your GameShare class. That's going to cause issues later when you start using the bounds for something. Any place in your code where the position is modified, the bounds' x and y should also be updated to match.

这篇关于libgdx - 遇到问题建立我的对象走动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆