我非常沮丧,因为我不能像Flappy Bird那样画出地面...... 我尝试使用这种方法:
private void drawGround(){
for(Rectangle mRectangleGroundHelper : mArrayGround){
if(spawnGround & mRectangleGroundHelper.x<0){ // spawn Ground if the actual ground.x + ground.width() is smaller then the display width.
mArrayGround.add(mRectangleGroundHelper);
spawnGround = false;
}
}
for(Rectangle mRectangleGroundHelper : mArrayGround){
if(mRectangleGroundHelper.x < -mTextureGround.getWidth()){ // set boolean to true, if the actual ground.x completely hide from display, so a new ground can be spawn
spawnGround = true;
}
}
for(Rectangle mRectangleGroundHelper : mArrayGround){ // move the ground in negative x position and draw him...
mRectangleGroundHelper.x-=2;
mStage.getSpriteBatch().draw(mTextureGround, mRectangleGroundHelper.x, mRectangleGroundHelper.y);
}
}
一次结果是,当地面x从显示器撞击左侧时,地面在负x处移动得更快。那么我的方法中的错误是什么?
答案 0 :(得分:1)
mRectangleGroundHelper.x-=2;
这是一段可怕的代码。一般来说,你可能根本不应该动起来,因为这基本上需要移动整个世界。
相反,创建一个“视口”位置,这实际上只是一个X变量。随着游戏向前移动,您向前移动视口(实际上只是X ++),并相对于此绘制所有对象。
然后你根本不需要“生成”任何地面。你要么只画画,要么不画画。
以下是基于 lot 对您的数据进行假设的粗略示例......
private void drawGround(){
viewport.x += 2;
for(Rectangle r : mArrayGround) {
if(r.x + r.width > viewport.x && r.x < viewport.x + viewport.width) {
mStage.getSpriteBatch().draw(mTextureGround, viewport.x - r.x, r.y);
}
}
}