【问题标题】:Saving Sprite Position保存精灵位置
【发布时间】:2015-06-12 10:26:15
【问题描述】:

我有一个精灵,当到达某个位置时它会被移除:

 if(sprite.getY()>=700){
      enemyIterator.remove();
      Pools.free(sprite);
}

我不想在精灵被删除之前保存它的最后位置,我尝试了sprite.getX()sprite.getY(),但这些只有在精灵在游戏中时才可用。

【问题讨论】:

  • 您是否在删除精灵后保留对精灵的引用?你什么时候需要使用这些职位?是在移除之后还是之后的某个时间?
  • 你不能把它保存在enemyIterator.remove(); 行之前吗?
  • @plastique 我将在他们被移除后使用他们的位置。
  • @Tenofur04 我可以在它们被移除之前使用它们的位置,但是当精灵被移除时,它们的坐标也会被移除。

标签: java libgdx position sprite


【解决方案1】:

基于提供的附加信息。 Sprite 方法 getX()getY() 返回浮点值。因此,您可以将这些方法返回的值分配给float 类型的变量以供以后使用。

float lastX, lastY;

if(sprite.getY()>=700){
  lastX = sprite.getX();
  lastY = sprite.getY();
  enemyIterator.remove();
  Pools.free(sprite);
}

System.out.println("Removed sprite coordinates where: " + lastX + ", " + lastY);

【讨论】:

    【解决方案2】:

    也许你不能得到精灵的最后一个位置,但是你可以保存你在这一帧中绘制的最后一个位置,你总是需要在绘制每一帧时设置一个位置,如果发生了什么事,抓住例如最后一个位置。

    private Rectangle bordes;
    
    public Ball(float x, float y,int isMoved) {
        ..........
            texture = new Texture(Gdx.files.internal("bola.png"));
            bordes = new Rectangle(x, y, texture.getWidth(), texture.getHeight());
        ..........
        }
    
    public void draw(SpriteBatch batch) { 
        //where i draw my last texture or sprite  was bordes.x bordes.y in this frame
            batch.draw(texture, bordes.x, bordes.y, texture.getWidth(), texture.getHeight());
        }
    
    public vois getLastPosition(){
    
    lastX = bordes.x;
    lastY = bordes.y;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      相关资源
      最近更新 更多