Slick Forums

Discuss the Slick 2D Library
It is currently Thu Aug 22, 2019 6:45 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: camera questions
PostPosted: Sun Nov 25, 2012 10:14 pm 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
(sorry i make so many topics just have a lot of questions and this is my best place to find answers)

so i just restarted making my game again, and i want to upgrade the way the game is viewed, how would i go about having the screens view always centered on a moving sprite, befroe i just kept the sprite in the same place and moved the backround, but thats ridiculus.


EDIT: also how do you get keyboard input from a class outside a state useing the if(input.isKeyDown(Input.KEY_WHATEVERKEY)) method, because you dont have agame container variable, should i make a new one? how does that work?


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Mon Nov 26, 2012 6:59 pm 
Offline
Regular
User avatar

Joined: Sun Feb 06, 2011 11:59 pm
Posts: 107
Location: USA
Quote:
EDIT: also how do you get keyboard input from a class outside a state useing the if(input.isKeyDown(Input.KEY_WHATEVERKEY)) method, because you dont have agame container variable, should i make a new one? how does that work?


No, do not create a new one.

Instead, slick will send you the game container in update. You can in turn send that same game container object to any method tha tneeds its.

Code:
// this is written off the top of my head. look for the point not the literal syntax.

void update(GameContainer gc) {

    myObject.update(gc);
}

public class MyObject {


    public void update(GameContainer gc) {
          gc.getInput(); // or whatever. this is the exact same GC as in the above method. There is only one instance and you are passing it around.
    }
}

_________________
"When you break the big laws, you do not get freedom; you do not even get anarchy. You get the small laws."
-GK Chesterton


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Tue Nov 27, 2012 1:49 am 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
ok, thanks for replying
i actually ended up solving it by just using lwjgls method of input detection, much cleaner i think


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Nov 28, 2012 12:32 am 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
still got the camera problem though, and thats kinda the bigger one
i dont know how to shift the screen


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Nov 28, 2012 11:24 am 
Offline
Regular
User avatar

Joined: Mon Feb 07, 2011 8:48 pm
Posts: 182
Location: United Kingdom
I think you may find this example to be particularly useful:

https://bitbucket.org/kevglass/slick/sr ... at=default

You can find the resources necessary to run this example here:

https://bitbucket.org/kevglass/slick/sr ... at=default

_________________
Mirage Realms - A free, retro, 2D MMORPG being developed using Slick2D and TWL


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Nov 28, 2012 3:51 pm 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
hmmm, ok ill look at that

EDIT:ok i see now, im guessing that the scrolling is done by Graphics.Translate? how do i do that in another class without making a new graphics object? everytime i tyr it tells me i cant make a non static reference to a static object, but its not static.


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Nov 28, 2012 4:38 pm 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
if you use Graphics.Translate() directly rather than the instance of Graphics that you get from slicks render method, you are calling it statically.
instead pass it to your own object via a parameter, just like TrentonZero explained you should do with the GameContainer object.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Nov 28, 2012 4:41 pm 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
hmmm, ok, i think i might get it, kinda confused still. ill try, sorry im so new to this and dont get things.




EDIT:i see the problem, renders graphics variable is being created in the render method, and so i cant use it outside of it, right?, maybe not


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Tue Dec 04, 2012 5:44 am 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
ok, this is kinda getting agravating, ive been lopking and looking and looking. heres my code:


Code:
package zelda;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import java.awt.Rectangle;
import org.newdawn.slick.Graphics;




public class TestLevel extends BasicGameState{

   Image image;
   boolean quit = false;
   Player link;
   Graphics s;

   
   public TestLevel(int state) throws SlickException {
   
   }
   
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
      image = new Image("res/playNow.png");
      link = new Player();
   }
   
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      
      g.drawString(" xpos = "+ link.Xpos + " Ypos = " + link.Ypos, link.Xpos, link.Ypos + 20);
      g.toString();
      g.drawString(g.toString(),50,50);
   }
   
   
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
   link.Start();
   Input input = gc.getInput();
      
      
      
      //start exit menu
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }
         if(quit==true){
           
            if(input.isKeyDown(Input.KEY_R)){
               quit = false;

            }
            if(input.isKeyDown(Input.KEY_M)){
               sbg.enterState(0);
               try{
                  Thread.sleep(250);
               }catch(InterruptedException e){
                  e.printStackTrace();
               }
            }
            if(input.isKeyDown(Input.KEY_Q))
               System.exit(0);
            }
      
        
   }
   
   
   
   public int getID(){
      return 1;
   }
}



assume all the referenced classes exist
all i want to do i have the graphics object created for a parameter of the
render method, to be a refenerce instead of being created right there,
WHY cant i just do that, and if i can, why is it that nowhere i look tells me how?


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Mon Dec 10, 2012 9:52 pm 
Offline

Joined: Tue Nov 27, 2012 10:51 am
Posts: 29
Code:
   Image image;

Make a RessourceManager class, that handles access to the ressources, like animations, graphics, sounds.... for you.

Code:
      g.toString();

You are calling .toString on your Graphics ref without doing anything with it.

Code:
     g.drawString(" xpos = "+ link.Xpos + " Ypos = " + link.Ypos, link.Xpos, link.Ypos + 20);

rather call link.render(GameContainer gc, Graphics g) and have the Player class implement its own render() method, where you have it draw itself.

Code:
  link.Start();

Whats in here? :D

Code:
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }
         if(quit==true){
                           }


Your keys are only polled, while holding down ESC. Not to mention, its not really _that_ usefull to assign a value to a variable, which only purpose is, to be evaluated in the next line.


Top
 Profile  
 
 Post subject: Re: camera questions
PostPosted: Wed Dec 12, 2012 5:20 pm 
Offline

Joined: Mon Oct 22, 2012 1:31 am
Posts: 25
thanks for replying, as for all your comments:

i will make a resource manager, thanks i didnt know about that

the unused g.toString was just a lne i forgot to delete

link.start is the method that checks all the collisions for the character and things like that. i later changed it it link.Update

also the esc thing is just mt atempt at making a pause menu, which failed and also later got deleted


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group