Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //I tried to post all the important class and method to make it easier for you to track the issue
- SpriteBatch batch;
- Texture circleImg;
- Color redCol;
- private float rotSpeed = 10;
- public static Vector2 screenCenter;
- OrthographicCamera camera;
- ShapeRenderer shape;
- private Stage stage;
- private Group group;
- Paddle centerPad, redPad, bluePad; //this are the group elements
- public GameScreen(final RgbX gam) {
- this.game = gam;
- screenCenter = new Vector2(RgbX.WIDTH / 2, game.HEIGHT / 2);
- circleImg = new Texture(Gdx.files.internal("circle2.PNG"));
- //a method to define all the colors
- setColors();
- //this is where i set up my group
- createThePaddle();
- camera = new OrthographicCamera();
- camera.setToOrtho(false, game.WIDTH, game.HEIGHT);
- batch = new SpriteBatch();
- shape = new ShapeRenderer();
- Gdx.input.setInputProcessor(this);
- }
- @Override
- public void render(float delta) {
- // TODO Auto-generated method stub
- Gdx.gl.glClearColor(0, 0.0f, 0.0f, 0);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- //in this method i control the group with inputs
- groupUpdate();
- //drawing the group
- stage.act(Gdx.graphics.getDeltaTime());
- stage.draw();
- //attempts to get the rectangle
- for (Actor ac : group.getChildren()) {
- Vector2 pos = ac.stageToLocalCoordinates(new Vector2(0, 0));
- System.out.println(pos);
- //i have a method to return a rectangle but i can't use it
- Rectangle rect = new Rectangle(pos.x, pos.y, ac.getWidth(),
- ac.getHeight());
- shape.setProjectionMatrix(stage.getCamera().combined);
- shape.begin(ShapeType.Line);
- shape.rect(rect.x, rect.y, rect.width, rect.height);
- shape.end();
- }
- }
- //the group setup method :
- private void createThePaddle() {
- // Create a stage
- stage = new Stage(new StretchViewport(game.WIDTH, game.HEIGHT));
- // Create a group and add it to the stage.
- group = new Group();
- stage.addActor(group);
- // Create images and add them to the group.
- Sprite sprite = new Sprite(circleImg);
- Image image = new Image(circleImg);
- //this is what the paddle class take (i'll post it below)
- redPad = new Paddle(redCol, "red", sprite, image, new Vector2(0, 0), 1f);
- bluePad = new Paddle(blueCol, "blue", sprite, image, new Vector2(0, 0),
- 1f);
- //adding the actor (i don't know why i can't add the object itself)
- group.addActor(redPad.getImage());
- group.addActor(bluePad.getImage());
- //setting the position relative to the stage
- redPad.getImage().setPosition(10, 0);
- bluePad.setPosition(new Vector2(20, 0));
- //setting the group position in the middle of the screen
- group.setPosition(RgbX.WIDTH / 2, game.HEIGHT / 2 );
- group.setScale(.5f);
- }
- //this method is to update the group
- private void groupUpdate() {
- if (Gdx.input.isKeyPressed(Keys.LEFT)) {
- group.rotateBy(rotSpeed);
- // group.setX(group.getX() + 100 * Gdx.graphics.getDeltaTime());
- }
- if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
- group.rotateBy(-rotSpeed);
- // group.setX(group.getX() - 100 * Gdx.graphics.getDeltaTime());
- }
- }
- /***** The Paddle Class ****/
- package com.aladine.rgbx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.scenes.scene2d.ui.Image;
- public class Paddle {
- public Color color;
- public String type;
- public Image image;
- public Vector2 position;
- public Sprite sprite;
- public float scale;
- public Rectangle rectangle;
- public Paddle(Color color, String type, Sprite sprite,Image image, Vector2 position,
- float scale) {
- this.color = color;
- this.type = type;
- this.position = position;
- this.sprite = sprite;
- this.image = image ;
- sprite.setColor(color);
- image.setColor(color);
- sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
- sprite.setOriginCenter();
- setRect();
- }
- public void setRect() {
- Rectangle rect = new Rectangle();
- // change the rectangle size to match the sprite
- rect.width = sprite.getWidth();
- rect.height = sprite.getHeight();
- rect.x = sprite.getX();
- rect.y = sprite.getY();
- this.rectangle = rect;
- }
- public void updateRect() {
- this.rectangle.x = position.x;
- this.rectangle.y = position.y;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public Image getImage() {
- Image img = new Image(sprite.getTexture());
- img.setColor(sprite.getColor());
- return img;
- }
- public void setImage(Image image) {
- this.image = image;
- }
- public Vector2 getPosition() {
- return position;
- }
- public void setPosition(Vector2 position) {
- this.position = position;
- }
- public Sprite getSprite() {
- return sprite;
- }
- public void setSprite(Sprite sprite) {
- this.sprite = sprite;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment