2014-10-21 4 views
0

MoveToAction을 사용하여 화면상의 액터를 업데이트하려고합니다. 그러나, 아무 것도하지 않는 것 같아 도움이 될만한 온라인 예제를 찾을 수 없습니다. (필자가 발견 한 것들은 내가 올바르게 설정하고 있음을 암시합니다. setPositionX 메서드를 통해 positionX를 업데이트하고 로깅을 통해 positionX가 업데이트되고 있는지 확인할 수있었습니다. 이 작품을 만들기 위해 추가해야 할 것이 있습니까?Libgdx MoveToAction - 작동하지 않습니다.

public MyActor(boolean playerIsEast, int positionX) { 
     setBounds(this.getX(), 140, 50, 200); 
     this.positionX = positionX; 
     this.setX(400); 
     currentImage = AssetLoader.losEast; 
     moveAction = new MoveToAction(); 
     moveAction.setDuration(1); 
     this.addAction(moveAction); 
    } 

    @Override 
    public void draw(Batch batch, float alpha) { 
     batch.draw(currentImage, this.getX(), 140, 50, 200); 
    } 

    @Override 
    public void act(float delta) { 
     moveAction.setPosition(positionX, 140); 
     for (Iterator<Action> iter = this.getActions().iterator(); iter 
       .hasNext();) { 
      iter.next().act(delta); 
     } 

    } 

답변

0

동작을 만들 때 act 메서드가 아닌 대상 위치를 설정합니다. 그리고 이미 액터 수퍼 클래스에 내장되어 있기 때문에 act 메소드가 조치를 처리 할 필요가 없습니다.

그래서 다음과 같아야합니다

public MyActor(boolean playerIsEast, int positionX) { 
    setBounds(this.getX(), 140, 50, 200); 
    this.positionX = positionX; 
    this.setX(400); 
    currentImage = AssetLoader.losEast; 
    moveAction = new MoveToAction(); 
    moveAction.setDuration(1); 
    moveAction.setPosition(positionX, 140); 
    this.addAction(moveAction); 
} 

@Override 
public void draw(Batch batch, float alpha) { 
    batch.draw(currentImage, this.getX(), 140, 50, 200); 
} 

@Override 
public void act(float delta) { 
    super.act(delta); //This handles actions for you and removes them when they finish 

    //You could do stuff other than handle actions here, such as 
    //changing a sprite for an animation. 
} 

그러나,이 GC를 트리거 피하기 위해 풀링 작업을 사용하는 것이 좋습니다. Actions 클래스는이를위한 편리한 메소드를 제공합니다. 따라서 이것을 더 단순화 할 수 있습니다 :

public MyActor(boolean playerIsEast, int positionX) { 
    setBounds(this.getX(), 140, 50, 200); 
    this.positionX = positionX; 
    this.setX(400); 
    currentImage = AssetLoader.losEast; 
    this.addAction(Actions.moveTo(positionX, 140, 1)); //last argument is duration 
} 
관련 문제