【问题标题】:Move a character randomly with time随时间随机移动角色
【发布时间】:2016-01-12 16:37:13
【问题描述】:

我正在尝试用 libgdx 做一个游戏,比如“缓存缓存游戏”

我希望我的角色首先停留 2 秒,然后每 2 秒给一个新的随机位置,即数组 carte 的一个新元素。

首先,我已经生成了我的随机索引,并且字符保持在第一位。

我的问题是: 我应该如何让每个键盘输入使我的角色在数组中的随机位置移动?

这是我的代码:

大家好,我正在尝试用 libgdx 做一个游戏,比如“缓存缓存游戏” 我希望我的角色首先停留 2 秒,并给每个 2seconde 一个新的随机数组位置(“carte”) *首先我已经生成了我的随机索引,并且字符保持在第一位。但我的问题是:: 我应该如何让每个键盘输入我的角色在数组中的随机位置移动

任何想法! 这是我的代码:::

公共类 MainScreen 实现 Screen {

SpriteBatch batch;
private Texture carte;
private Texture mario;
private Array<Rectangle>foret;
private Animation animation;
private float time;
private Rectangle mari;
private Vector2 position;
private Rectangle mickey ;
private float counterTime; 
Game game;
public MainScreen(Game game) {
    this.game = game;
    batch = new SpriteBatch();
    carte = new Texture(Gdx.files.internal("foret.png"));

    animation = new Animation(3/3f , new TextureRegion(new Texture("mario1.png")) , new TextureRegion(new Texture("mario2.png")) , new TextureRegion(new Texture("mario3.png")));

    foret = new Array<Rectangle>();


      this.mari = depMarioRandom();
      Vector2 position = new Vector2();
      position.x = 0;
      position.y = 0;
}


public void carte(){
    foret = new Array<Rectangle>();
    for(int i =0 ; i<7 ; i++){
        for(int j =0 ; j<7 ; j++){
            Rectangle fore = new Rectangle();
            fore.x = (i*100)+100;
            fore.y = (j*50)+20 ;
            fore.width = 64;
            fore.height = 64;
            foret.add(fore);
            batch.draw(carte ,fore.x , fore.y , 64 , 64 );
        }
    }
}
public Rectangle depMarioRandom(){
    foret = new Array<Rectangle>();
    for(int i =0 ; i<7 ; i++){
        for(int j =0 ; j<7 ; j++){
            Rectangle fore = new Rectangle();
            fore.x = (i*100)+100;
            fore.y = (j*50)+20 ;
            fore.width = 64;
            fore.height = 64;
            foret.add(fore);
        }}
    int random = (int) ( Math.random() * foret.size );
    Rectangle randomX = foret.get(random);
     return randomX;

}

@Override
public void show() {


}
@Override
public void render(float delta) {

    Gdx.gl.glClearColor(1, 1, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    carte();
    counterTime += Gdx.graphics.getDeltaTime();
    time += delta;
    if(counterTime > 2f ) {// each second ==> one call to position random

         position = new Vector2(mari.x, mari.y);
         counterTime =0f;


   }else { // between 2 seconds
       if(Gdx.input.isKeyPressed(Keys.B)) { // if "B" of Keyboard is pressed
           counterTime =3f; // go directly to the next random position

}

} batch.draw(animation.getKeyFrame(time), mari.x, mari.y, 64, 64);

    animation.setPlayMode(Animation.PlayMode.LOOP);


    batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {

}

}

【问题讨论】:

  • 你想让你的角色在时间事件还是键盘事件中移动?
  • 如果键盘事件在 2 秒之间发生,所以角色移动到另一个随机位置!!!
  • 要明确:角色是否应该移动:(A)如果两个键盘输入在不到 2 秒内发送; (B) 如果有用户输入,则每隔 2s 除外; (C)如果在 2 秒的间隔内发送了一个键盘事件,该事件是由不是之前的键盘事件启动的?
  • 要清楚::不能移动字符,它必须在 2 秒之间发送键盘事件
  • 如果你有什么想法请帮忙

标签: java libgdx


【解决方案1】:

所以,你想保持位置随时间的随机变化,并通过键盘输入添加变化,对吧?!

试试这个:

counterTime += Gdx.graphics.getDeltaTime();
time += delta;
if(counterTime > 2f ) {// each second ==> one call to position random

    position = new Vector2(this.mari.x, this.mari.y);
    counterTime =0f;


}else { // between 2 seconds
    if(Gdx.input.isKeyPressed(Keys.B)) { // if "B" of Keyboard is pressed
        counterTime =3f; // go directly to the next random position

    }
}

【讨论】:

  • 首先感谢您花费您的时间并且没有工作 eclipse 给我发送了一个“位置 = new new Vector2(this.mari.x, this.mari.y); i已在 create 方法中初始化
  • 我忘记了窗户正在打开,但 2 秒后他给我发送了“例外”,我现在真的不知道该怎么办了!!
  • 如果我将 counterTime 重置为 0 mario 没有 2 秒的持续时间 .mario 在所有位置出现和消失
  • 你能发布空指针异常消息吗? ,只有当你点击“B”键时重置才有效
  • 如果我删除“this”前面的 mari 它的工作并且没有例外是generete 但角色没有2秒的持续时间!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
相关资源
最近更新 更多