随时间随机移动角色 [英] Move a character randomly with time
问题描述
我正在尝试使用libgdx做游戏,例如缓存游戏"
I'm trying to do a game with libgdx like "Cache-cache game"
我希望我的角色首先停留 2 秒,并每 2 秒给一个新的随机位置,即数组 carte
的一个新元素.
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?
这是我的代码:
大家好,我正在尝试使用libgdx做游戏,例如缓存游戏"我想让我的角色在第一时间停留2秒,并为每2秒分配一个新的array("carte")随机位置*首先我已经生成了我的随机索引,并且角色停留在首位.但我的问题是::与每个键盘输入相比,我应该如何使字符在数组中的随机位置移动
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:::
公共类MainScreen实现Screen {
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);
} 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() {
}
}
推荐答案
所以,您想保留位置随时间变化的随机变化,还希望通过键盘输入添加变化,对吧?!
so, you want to keep the the random changes of the position by time and also add a change by keyboard inpput , right?!
尝试:
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
}
}
这篇关于随时间随机移动角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!