2016-01-10 4 views
1

Я пытаюсь создать игру с libgdx, но у меня есть проблема. Я искал очень хорошо, но я не могу найти решение.Продолжительность случайной позиции libgdx

Я пытаюсь сделать последнее позиционирование символа случайной позиции Array <Rectangle> каждому «рендерингу», необходимо, чтобы символ имел случайное положение массива.

Проблема заключается в том, что положение персонажа не длится, Я хочу, чтобы продолжительность каждой секунды была «рендеринга». Я попытался сделать анимацию, но не работал в случайном положении. by cons, если я определяю позицию (100, 100), например, он работает, каждое изображение символа остается 1 секундой.

Я не знаю, понимаете ли вы меня, но я хочу, чтобы символ оставался 1 секундой в каждой позиции массива (случайная позиция).

Это мой код:

public class MainScreen implements 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 ; 
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>(); 

} 


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); 


    this.mari = depMario(); 
    time += delta; 
    Vector2 position = new Vector2(this.mari.x, this.mari.y); 

     // i try te do a copy and scaling whith time but it doesn't work 

    position.cpy().scl(time); 

    batch.begin(); 
    carte(); 

    batch.draw(animation.getKeyFrame(time), position.x, position.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() { 

} 
} 

ответ

0

я прочитал вашу проблему несколько раз это то, что я понимаю

вы хотите для вашего Caractere, чтобы иметь rondom poistion для продолжительности одной секунды, вы могли бы сделать что, как это

counterTime += Gdx.graphic.getDeltaTime(); 
     time += delta; 
     if(counterTime > 1) {// each second ==> one call to position random 
      // putting the random position here 
      position = new Vector2(this.mari.x, this.mari.y); 
      counterTime = 0; // reset to 0; 
     } 

    // do not scall anything 


    batch.begin(); 
    carte(); 

если что-нибудь еще оставить комментарий

удачи

Edit:

но вам нужно просто инициализировать позицию в первый раз в создать() метод

+0

кулак спасибо за ваш ответ, но это не то, что я ищу I хочу, чтобы мой charactar пребывание в положении 1seconde whitout движется !! я не уверен, если вы понимаете мии – joe

+0

salamoalikoum Mr Netero السلام عليكم أستاد نيتيرو انا مغريبي و لاست جيدا بي الينجليزي كل ما اواد هوووا ان يابق ماريو في ماكان لي مودت 1 تانية توما يانتاكيل إلى ماكان أخر وا يابك لنافس ال مودا و أود أن أعتدر على هذا الخط – joe

+0

JE саис па си уоиз comprenez Le français. voila mon probléme ::: je veux que le personnage se deplace aléatoirment (random) sur le tableau et a chaque position il faut qu'il reste (le personnege) 1seconde et apres il se deplace a une autre position et qu'il reste aussi 1 seconde et ainsi de suite pour ca j fait une animation mais ca marche pas pour les position random !!! si vous voulez bien Mr netero je crois que vous etes arabe moi aussi j'en suis un si vous voullez bien m'aider j'en serait reconnaissant – joe