问题
I'm trying to do a game with libgdx like "Cache-cache game"
I want for my character to stay 2 seconds in the first place and to give each 2 seconds a new Random place, i.e a new element of the array carte
.
First, I have generate my random index and the character stay in the first place.
My problem is : How should I make that each keyboard input makes my character move in a random place in the array?
This is my code:
hi everybody i'm trying to do a game with libgdx like "Cache-cache game" i want for my character staying 2 second in the first place and to give each 2seconde a new Random place of the array("carte") *first i have generate my random index and the character stay in the first place . but my problem is :: how i should making for than each keybord input my character moving in a random place in the array
any idea!!! this is my code:::
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 ;
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() {
}
}
回答1:
so, you want to keep the the random changes of the position by time and also add a change by keyboard inpput , right?!
try this :
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
}
}
来源:https://stackoverflow.com/questions/34749056/move-a-character-randomly-with-time