【发布时间】:2016-01-12 16:37:13
【问题描述】:
我正在尝试用 libgdx 做一个游戏,比如“缓存缓存游戏”
我希望我的角色首先停留 2 秒,然后每 2 秒给一个新的随机位置,即数组 carte 的一个新元素。
首先,我已经生成了我的随机索引,并且字符保持在第一位。
我的问题是: 我应该如何让每个键盘输入使我的角色在数组中的随机位置移动?
这是我的代码:
大家好,我正在尝试用 libgdx 做一个游戏,比如“缓存缓存游戏” 我希望我的角色首先停留 2 秒,并给每个 2seconde 一个新的随机数组位置(“carte”) *首先我已经生成了我的随机索引,并且字符保持在第一位。但我的问题是:: 我应该如何让每个键盘输入我的角色在数组中的随机位置移动
任何想法! 这是我的代码:::
公共类 MainScreen 实现 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() {
}
}
【问题讨论】:
-
你想让你的角色在时间事件还是键盘事件中移动?
-
如果键盘事件在 2 秒之间发生,所以角色移动到另一个随机位置!!!
-
要明确:角色是否应该移动:(A)如果两个键盘输入在不到 2 秒内发送; (B) 如果有用户输入,则每隔 2s 除外; (C)如果在 2 秒的间隔内发送了一个键盘事件,该事件是由不是之前的键盘事件启动的?
-
要清楚::不能移动字符,它必须在 2 秒之间发送键盘事件
-
如果你有什么想法请帮忙