How to change x and y position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    How to change x and y position

    Hi,
    I am using Java, Libgdx, box2d


    I main class I have created a body and I set player x and y position to 50.


    Code:
    // main class
    public class main{
      ...
      private void createPlayer() {
    	BodyDef bdef = new BodyDef();
    	Body body;
    	FixtureDef fdef = new FixtureDef();
    	PolygonShape shape = new PolygonShape();
    
    	/*** Body - Player ***/
    	bdef.type = BodyType.DynamicBody;
    	bdef.position.set(50 / PPM, 50 / PPM);
    	bdef.linearVelocity.set(1.5f, 0);
    	body = world.createBody(bdef);
            .....
      }
    }


    How can I set player x and y position to 100.
    Code:
    //player class
    public class player{
               public player(Body body){
                     super(body);
               }
           ....
           public void update(){
                 //get player x position
                 currentX = this.getBody().getPosition().x;
    
                 //how to change player x and y position to 100?   
           }
    }
  • Jaxter
    New Member
    • Jul 2014
    • 1

    #2
    Hi.

    Create a method that updates the position of x and y. After doing so, call it in the player class.

    Hope I helped.

    Comment

    • game2d
      New Member
      • Apr 2013
      • 59

      #3
      here is what i tried. but didnt worked bc CreatePlayer() method is in Main class constructor.


      // main class
      Code:
      
          public class main{
            ...
            public main(){
                createPlayer();
            }
            
            private void createPlayer() {
              BodyDef bdef = new BodyDef();
              Body body;
              FixtureDef fdef = new FixtureDef();
              PolygonShape shape = new PolygonShape();
          
              /*** Body - Player ***/
              bdef.type = BodyType.DynamicBody;
              bdef.position.set(50 / PPM, 50 / PPM);
              bdef.linearVelocity.set(1.5f, 0);
              body = world.createBody(bdef);
                  .....
              player = new Player(bdef);
            }
            
            ....
            
            public void update(float dt) {
                playerObj.update(dt);
                ...
            }
          }





      //player class
      Code:
          public class player{
                 public player(Body body, BodyDef bdef){
                       super(body);
                       this.bdef = bdef;
                 }
          
                 ....
                 public void update(){
                       //get player x position
                       currentX = this.getBody().getPosition().x;
                      
                       //how to change player x and y position to 100? 
                       bdef.position.set(100/PPM, 100/PPM);
                 }
          }

      Comment

      Working...