alaslipknot

Libgdx group issue

Jun 8th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.04 KB | None | 0 0
  1. //I tried to post all the important class and method to make it easier for you to track the issue    
  2.  
  3.  
  4.         SpriteBatch batch;
  5.     Texture circleImg;
  6.     Color redCol;
  7.  
  8.     private float rotSpeed = 10;
  9.  
  10.     public static Vector2 screenCenter;
  11.  
  12.     OrthographicCamera camera;
  13.     ShapeRenderer shape;
  14.  
  15.     private Stage stage;
  16.     private Group group;
  17.    
  18.     Paddle centerPad, redPad, bluePad; //this are the group elements
  19.  
  20. public GameScreen(final RgbX gam) {
  21.         this.game = gam;
  22.        
  23.         screenCenter = new Vector2(RgbX.WIDTH / 2, game.HEIGHT / 2);
  24.        
  25.  
  26.         circleImg = new Texture(Gdx.files.internal("circle2.PNG"));
  27.         //a method to define all the colors
  28.         setColors();
  29.         //this is where i set up my group
  30.         createThePaddle();
  31.        
  32.        
  33.         camera = new OrthographicCamera();
  34.         camera.setToOrtho(false, game.WIDTH, game.HEIGHT);
  35.         batch = new SpriteBatch();
  36.         shape = new ShapeRenderer();
  37.         Gdx.input.setInputProcessor(this);
  38.  
  39.     }
  40. @Override
  41.     public void render(float delta) {
  42.         // TODO Auto-generated method stub
  43.         Gdx.gl.glClearColor(0, 0.0f, 0.0f, 0);
  44.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  45.        
  46.         //in this method i control the group with inputs
  47.         groupUpdate();
  48.        
  49.         //drawing the group
  50.         stage.act(Gdx.graphics.getDeltaTime());
  51.         stage.draw();
  52.        
  53.  
  54.         //attempts to get the rectangle
  55.         for (Actor ac : group.getChildren()) {
  56.             Vector2 pos = ac.stageToLocalCoordinates(new Vector2(0, 0));
  57.  
  58.             System.out.println(pos);
  59.             //i have a method to return a rectangle but i can't use it
  60.             Rectangle rect = new Rectangle(pos.x, pos.y, ac.getWidth(),
  61.                     ac.getHeight());
  62.  
  63.             shape.setProjectionMatrix(stage.getCamera().combined);
  64.             shape.begin(ShapeType.Line);
  65.             shape.rect(rect.x, rect.y, rect.width, rect.height);
  66.             shape.end();
  67.  
  68.         }
  69.        
  70.  
  71.     }
  72. //the group setup method :
  73. private void createThePaddle() {
  74.         // Create a stage
  75.         stage = new Stage(new StretchViewport(game.WIDTH, game.HEIGHT));
  76.  
  77.         // Create a group and add it to the stage.
  78.         group = new Group();
  79.         stage.addActor(group);
  80.  
  81.         // Create images and add them to the group.
  82.         Sprite sprite = new Sprite(circleImg);
  83.         Image image = new Image(circleImg);
  84.         //this is what the paddle class take (i'll post it below)
  85.         redPad = new Paddle(redCol, "red", sprite, image, new Vector2(0, 0), 1f);
  86.         bluePad = new Paddle(blueCol, "blue", sprite, image, new Vector2(0, 0),
  87.                 1f);
  88.         //adding the actor (i don't know why i can't add the object itself)
  89.         group.addActor(redPad.getImage());
  90.         group.addActor(bluePad.getImage());
  91.         //setting the position relative to the stage
  92.         redPad.getImage().setPosition(10, 0);
  93.         bluePad.setPosition(new Vector2(20, 0));
  94.         //setting the group position in the middle of the screen
  95.         group.setPosition(RgbX.WIDTH / 2, game.HEIGHT / 2 );
  96.        
  97.         group.setScale(.5f);
  98.  
  99.     }
  100. //this method is to update the group
  101. private void groupUpdate() {
  102.         if (Gdx.input.isKeyPressed(Keys.LEFT)) {
  103.             group.rotateBy(rotSpeed);
  104.             // group.setX(group.getX() + 100 * Gdx.graphics.getDeltaTime());
  105.         }
  106.         if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
  107.             group.rotateBy(-rotSpeed);
  108.             // group.setX(group.getX() - 100 * Gdx.graphics.getDeltaTime());
  109.         }
  110.     }
  111.  
  112. /***** The Paddle Class  ****/
  113. package com.aladine.rgbx;
  114.  
  115. import com.badlogic.gdx.graphics.Color;
  116. import com.badlogic.gdx.graphics.g2d.Sprite;
  117. import com.badlogic.gdx.math.Rectangle;
  118. import com.badlogic.gdx.math.Vector2;
  119. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  120.  
  121. public class Paddle {
  122.  
  123.     public Color color;
  124.     public String type;
  125.     public Image image;
  126.     public Vector2 position;
  127.     public Sprite sprite;
  128.     public float scale;
  129.     public Rectangle rectangle;
  130.  
  131.     public Paddle(Color color, String type, Sprite sprite,Image image, Vector2 position,
  132.             float scale) {
  133.  
  134.         this.color = color;
  135.         this.type = type;
  136.         this.position = position;
  137.         this.sprite = sprite;
  138.         this.image = image ;
  139.         sprite.setColor(color);
  140.         image.setColor(color);
  141.         sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
  142.         sprite.setOriginCenter();
  143.         setRect();
  144.        
  145.     }
  146.  
  147.     public void setRect() {
  148.         Rectangle rect = new Rectangle();
  149.  
  150.         // change the rectangle size to match the sprite
  151.         rect.width = sprite.getWidth();
  152.         rect.height = sprite.getHeight();
  153.         rect.x = sprite.getX();
  154.         rect.y = sprite.getY();
  155.  
  156.         this.rectangle = rect;
  157.     }
  158.  
  159.     public void updateRect() {
  160.         this.rectangle.x = position.x;
  161.         this.rectangle.y = position.y;
  162.     }
  163.  
  164.     public Color getColor() {
  165.         return color;
  166.     }
  167.  
  168.     public void setColor(Color color) {
  169.         this.color = color;
  170.     }
  171.  
  172.     public String getType() {
  173.         return type;
  174.     }
  175.  
  176.     public void setType(String type) {
  177.         this.type = type;
  178.     }
  179.  
  180.     public Image getImage() {
  181.         Image img = new Image(sprite.getTexture());
  182.         img.setColor(sprite.getColor());
  183.         return img;
  184.     }
  185.  
  186.     public void setImage(Image image) {
  187.         this.image = image;
  188.     }
  189.  
  190.     public Vector2 getPosition() {
  191.         return position;
  192.     }
  193.  
  194.     public void setPosition(Vector2 position) {
  195.         this.position = position;
  196.     }
  197.  
  198.     public Sprite getSprite() {
  199.         return sprite;
  200.     }
  201.  
  202.     public void setSprite(Sprite sprite) {
  203.         this.sprite = sprite;
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment