随机位置持续时间 libgdx
Duration of random position libgdx
我正在尝试使用 libgdx 制作游戏,但遇到了问题。我已经很好地搜索了,但找不到解决方案。
我尝试使它最后将随机位置的字符定位到数组 <Rectangle>
到每个 "render" 字符必须具有数组的随机位置。
问题是人物的位置不持久,
我想要每个持续一秒钟的东西 "render"。
我试图制作一个动画,但它不适用于随机位置。
通过 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() {
}
}
我已经多次阅读你的问题,这是我的理解
你想让你的角色有一个持续一秒钟的随机位置,你可以这样做
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();
如有其他问题请留言
祝你好运
编辑:
但您只需要在 create() 方法
中第一次初始化位置
我正在尝试使用 libgdx 制作游戏,但遇到了问题。我已经很好地搜索了,但找不到解决方案。
我尝试使它最后将随机位置的字符定位到数组 <Rectangle>
到每个 "render" 字符必须具有数组的随机位置。
问题是人物的位置不持久, 我想要每个持续一秒钟的东西 "render"。 我试图制作一个动画,但它不适用于随机位置。 通过 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() {
}
}
我已经多次阅读你的问题,这是我的理解
你想让你的角色有一个持续一秒钟的随机位置,你可以这样做
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();
如有其他问题请留言
祝你好运
编辑:
但您只需要在 create() 方法
中第一次初始化位置