I’m trying to get the Image I have to appear on the box so I can try to scale it. When I call
.setUserData(Sprite);
It doesn’t work. Excuse any random crap, I tried to cut out any other code I was working on.
MyGdxGame Class
public class MyGdxGame extends Game {
@SuppressWarnings("unused")
private TestScreenA GameScreen;
@Override
public void create () {
//loads image
AssetLoaderTest.load("testImgs/pinkieflie.png");
AssetLoaderTest.loadTile("testImgs/Colors tile.png");
AssetLoaderTest.loadAnimation("testImgs/loadingsheet.png");
AssetLoaderTest.loadSound("testSounds/Peetiay - Overworld (Theme from Super Mario) (promodj.com).mp3");
AssetLoaderTest.loadFont("testFonts/ARHERMANN.TTF",80 ,Color.PINK);
GameScreen = new TestScreenA(this);
}
@Override
public void render () {
GameScreen.render(0);
}
}
Screen Class
public class TestScreenA implements Screen{
private MyGdxGame game;
//Camera
private OrthographicCamera camera;
//images etc.
private SpriteBatch batch;
int a = 100, b = 100;
Vector3 touch;
//Box2D
private World world;
private Box2DDebugRenderer b2dr;
private Body bd;
public TestScreenA(MyGdxGame game) {
this.game = game;
//Camera
camera = new OrthographicCamera();
camera.setToOrtho(true, 1920, 1080);
batch = new SpriteBatch();
//Box 2d
//CREATE WORLD
world = new World(new Vector2(0, 100f), true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef2 = new BodyDef();
bdef2.position.set(165 , 100 );
bdef2.type = BodyType.DynamicBody;
bd = world.createBody(bdef2);
PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(5, 5 );
FixtureDef fdef2 = new FixtureDef();
fdef2.shape = shape2;
fdef2.restitution = 1f;
bd.createFixture(fdef2);
// create platform
BodyDef bdef = new BodyDef();
bdef.position.set(160 , 120 );
bdef.type = BodyType.StaticBody;
Body body = world.createBody(bdef);
//WHERE I TRY TO GIVE THE BOX AN IMAGE
body.setUserData(AssetLoaderTest.s);
PolygonShape shape = new PolygonShape();
shape.setAsBox(50, 5 );
FixtureDef fdef = new FixtureDef();
fdef.shape = shape;
body.createFixture(fdef);
//platform 2
bdef.position.set(160 , 280 );
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 3
bdef.position.set(200 , 380 );
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(100 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 4
bdef.position.set(100 , 500);
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(80 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 4
bdef.position.set(0 , 0);
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(2 , 500);
fdef.shape = shape;
body.createFixture(fdef);
// create falling box
bdef.position.set(160 , 200 );
bdef.type = BodyType.DynamicBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
fdef.restitution = 3;
fdef.density = 40;
body.createFixture(fdef);
//falling box 2
bdef.position.set(180 , 200 );
bdef.type = BodyType.DynamicBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
fdef.restitution = 5;
fdef.density = 90;
body.createFixture(fdef);
}
public void render(float delta) {
//clears the screen with white
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
update(delta);
world.step(1.f/60.f, 6, 3);
batch.begin();
b2dr.render(world, camera.combined);
batch.end();
}
public void update(float delta) {
}
public void resize(int width, int height) {
}
//first thing that happens before game starts pretty much
public void show() {
}
public void hide() {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
AssetLoaderTest Class
public class AssetLoaderTest {
//Images etc.
public static Texture t;
public static Sprite s;
//Tile Sheet
static Texture sSheet;
static Sprite blueTile;
static Sprite yellowTile;
//Animation
static Texture sheet;
static TextureRegion[] sheetFrames;
static TextureRegion currentFrame;
static Animation sheetAnim;
//Sound
static Sound marioTheme;
//Fonts (you can get a font by C: > windows > fonts)
static BitmapFont font;
static void load(String assetPath) {
t = new Texture(Gdx.files.internal(assetPath));
//Automatically scales texture based off of window size
t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//a sprite is pretty much the body and the texture is the texture or image of it
s = new Sprite(t);
s.flip(false, true);
}
static void loadTile(String assetPath) {
sSheet = new Texture(assetPath);
blueTile = new Sprite(sSheet, 0, 0, 16, 16);
yellowTile = new Sprite(sSheet, 16, 0, 16, 16); //add 16 since they're 16 bit
//don't need to flip. Maybe cuz you can't tell if it's flipped
}
static void loadAnimation(String assetPath) {
sheet = new Texture(Gdx.files.internal(assetPath));//sprite sheet and sprite dimaensions down there
TextureRegion[][] temp = TextureRegion.split(sheet, 16, 16);
//images in our frame
sheetFrames = new TextureRegion[12];
int index = 0;
//3 is amout of rows 4 is cols
for (int a = 0; a < 3; a++) {
for (int b = 0; b < 4; b++) {
sheetFrames[index++] = temp[a][b];
}
}
//12 is number of images
for (int a = 0; a < 12; a++) {
sheetFrames[a].flip(false, true);
}
//0.2f is the time
sheetAnim = new Animation(0.1f, sheetFrames);
}
static void loadSound(String assetPath) {
marioTheme = Gdx.audio.newSound(Gdx.files.internal(assetPath));
}
@SuppressWarnings("deprecation")
static void loadFont(String assetPath, int fontSize,Color fontColor) {
FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(assetPath));
//@param size
font = gen.generateFont(fontSize);
font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
font.setColor(fontColor);
//@Param flips font
font.setScale(1, -1);
}
}
I can render them the images with batch.draw(); but I can’t draw it on the box.