2016-01-12 150 views
0

我试图做一个游戏像“缓存缓存游戏”随机移动人物随着时间的推移

我要为我的性格入住两秒钟的首位,并给每个2秒libgdx一新的随机地点,即数组的新元素carte

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

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

这是我的代码:

大家好我试图做一个游戏像“缓存缓存游戏” libgdx 我要为我的角色在首位入住2二,给每个2seconde一个新的数组随机地点(“carte”) *首先我已经生成了我的随机索引,并且字符保持在第一位。但我的问题是:: 我应该怎么做比每个keybord输入我的角色在阵列中的随机位置移动

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

公共类MainScreen实现屏幕{

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(时间),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() { 

} 

}

+0

你想你的角色在时间事件或键盘事件移动? – jhamon

+0

如果键盘事件在2秒内变形,所以角色移动到另一个随机位置! – joe

+0

要清楚:如果两个键盘输入在2秒内发送,字符是否移动:(** A **); (** B **)如果有用户输入,每2秒除外; (** C **)如果键盘事件发送的时间间隔是2s,而不是以前的键盘事件, – jhamon

回答

0

因此,要通过时间来保持位置的随机变化,并通过添加键盘inpput的改变,对不对?

试试这个:

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 

    } 
} 
+0

首先感谢花时间和不工作日食给我一个“nullpinterexeption”的“位置=新的新的Vector2(this.mari.x,this.mari.y)的豁免”;我已经初始化在创建方法 – joe

+0

我忘记了窗口正在打开,但2秒后,他给我的“exeption”我真的不现在该怎么做!! – joe

+0

,如果我重置counterTime为0马里奥不具有持续2秒.mario apear和取消所有 – joe