0

i am new to computer programming, and i am following a Android Game Development. I'm on Tutorial Android Game Development in Java - Part 3: Processing Input, and in the first five minutes he has you put a png file into the assets/data folder in the "Tutorial-android" directory. I did so, and followed him. He successfully loaded the picture of mario, i cant. i copied his exact code,

package com.me.tutorial;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Tutorial implements ApplicationListener {
;
SpriteBatch batch;
Texture mario;

Vector2 position;
@Override
public void create() {      
batch = new SpriteBatch();
mario = new Texture(Gdx.files.internal("mario.png"));


position = new Vector2(50,50);
}

@Override
public void dispose() {
}

@Override
public void render() {      
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


batch.begin();
batch.draw(mario, position.x, position.y);
batch.end();
}

i got these errors

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException:           Couldn't load file: mario.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at    com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:142)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:104)
at com.me.tutorial.Tutorial.create(Tutorial.java:19)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:115)    

Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: mario.png (Internal) at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134) at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218) at com.badlogic.gdx.graphics.Pixmap. (Pixmap.java:137) ... 8 more

i looked and i cannot find a way to solve this. please help me, Thank you.

Aseem Goyal
  • 2,683
  • 3
  • 31
  • 48

3 Answers3

2

If it's in the asset/data folder you need to change the loading line to:

mario=new Texture(Gdx.files.internal("data/mario.png");

The internal looks into the asset folder not into the asset/data folder.

bemeyer
  • 6,154
  • 4
  • 36
  • 86
0

The Gdx.files.internal("path") looks into the sourcefolders in your jar. On Android this sourcefolder is, like @BennX said the asset folder and as much as i know it has to be that folder. On Desktop you can have your own folder, but you need to mark it as source folder in Eclipse. So if your Projectstructure is like: asset/textures/mario.png you have to call Gdx.files.internal("textures/mario.png");.

Robert P
  • 9,398
  • 10
  • 58
  • 100
  • When i add@Override public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); if(Gdx.input.isKeyPressed(Keys.W));{ position.y += 1f; } if(Gdx.input.isKeyPressed(Keys.A));{ position.x -= 1f; } if(Gdx.input.isKeyPressed(Keys.S));{ position.y -= 1f; } if(Gdx.input.isKeyPressed(Keys.D));{ position.x += 1f; } batch.begin(); batch.draw(mario, position.x, position.y); batch.end(); } ` it loads the picture. but it does not move the image when i press W,A,S,D. – Noob2Coding Feb 20 '14 at 04:34
  • I would implement the InputProcessor interface and then set your class as the inputprocessor: Gdx.input.setInputProcessor(this). Then you should override the keyDown and the keyUp method (as it is an interface you have to implement all but you need to wride this two). Store an `int dir` which is: -1 if you don't press key, 1 for "W", 2 for "W+A"... In keyDown you add the pressed key to your int, in keyUp you remove it. In the render() you say if (dir == 1) pos.x+= speed*delta; else if (dir == 2) { pos.x += speed/2*delta; pos.y+=speed/2*delta;}. This should work. But this is only pseudocode. – Robert P Feb 20 '14 at 07:02
0

Thank you for the answers. i did the Gdx.files.internal("data/mario") and i ran it and the picture of mario came up like it did before, but when i add

@Override
  public void render() {        
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    if(Gdx.input.isKeyPressed(Keys.W));{
        position.y += 1f;
    }
    if(Gdx.input.isKeyPressed(Keys.A));{
        position.x -= 1f;

    }
    if(Gdx.input.isKeyPressed(Keys.S));{
        position.y -= 1f;

    }
    if(Gdx.input.isKeyPressed(Keys.D));{
        position.x += 1f;
    }


batch.begin();
batch.draw(mario, position.x, position.y);
batch.end();
}

` the "input" doesn't work when i loaded it. it loads the picture. but it does not move the image when i press W,A,S,D. just like theLazyTryhard did in his video.