Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Why isn't drawing a Sprite working?  (Read 296 times)
0 Members and 1 Guest are viewing this topic.
Offline Dragon

Junior Newbie


Exp: 2 years



« Posted 2013-08-19 19:48:28 »

Hi everyone. I'm having an issue, and it's really frustrating because of how simple it is Tongue . Drawing a Sprite isn't working at all. I'm using LibGDX.

For example, with this code, when I draw the sprite, it is flashing really fast.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
@Override
   public void show() {
      batch = new SpriteBatch();

      texture = new Texture("ui/mainmenu.png");
      mainMenu = new Sprite(texture);
      mainMenu.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
     
      batch.begin();
      mainMenu.draw(batch);
      batch.end();
   }

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
   }


but with this code, my sprite doesn't show up at all.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   @Override
   public void show() {
      batch = new SpriteBatch();

      texture = new Texture("ui/mainmenu.png");
      mainMenu = new Sprite(texture);
      mainMenu.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
   }

   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(0, 0, 0, 1);
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
       
       batch.begin();
       mainMenu.draw(batch);
       batch.end();
   }


Thanks in advance Smiley
Offline davedes
« Reply #1 - Posted 2013-08-19 20:08:42 »

Your first bit of code doesn't render the sprite in your render() loop. You are incorrectly trying to draw in your show() method.

Also, you should probably not create new textures and sprite batches in a method called "show()" unless you are absolute sure it will only be getting called once. Expensive things like texture creation should be done at startup, not more than necessary.

The second bit of code looks like it would work. You might need to use a ortho projection matrix, like this…

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
create
    cam = new OrthographicCamera();

resize…
    cam.setToOrtho(false, width, height);

render

    batch.setProjectionMatrix(cam.combined);
    ///now render your batch

Offline Longarmx
« Reply #2 - Posted 2013-08-19 20:28:45 »

Try changing it to this:

1  
new Texture(Gdx.files.local("ui/mainmenu.png"));

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dragon

Junior Newbie


Exp: 2 years



« Reply #3 - Posted 2013-08-19 20:33:52 »

I played around with it for a little bit, and I found the issue, but didn't solve it.

Pretty much, the main game class sets the screen to MainMenu. MainMenu then runs its show method once then never runs its render method, and switches back to the main game class render method. Why is it doing this?

 I have no idea what to do here because I've tried copying and pasting code from another screen class I made for a different project, pasting it in, then changing all the names and everything, and it still doesn't work.

EDIT: I figured out the issue. I had to add super.render(); to the main game class. Ugh, I can't believe I forgot that, and was trying to figure out the problem for a couple hours now DX. I guess that's programming though Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (11 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!