I'm stuck at slick graphics -
i'm trying make game, using slick2d, , lwjgl. don't why code doesn't work
firststage.java
package net.charlesdickenson; import org.newdawn.slick.gamecontainer; import org.newdawn.slick.graphics; import org.newdawn.slick.image; import org.newdawn.slick.slickexception; import org.newdawn.slick.state.basicgamestate; import org.newdawn.slick.state.statebasedgame; public class firststage extends basicgamestate { public bossvar bosschecker() { if(isbeforemiddleboss) return bossvar.beforeboss; if(ismiddleboss) return bossvar.middle; if(isbeforeboss) return bossvar.beforeboss; if(isboss) return bossvar.boss; return null; } @suppresswarnings("static-access") public firststage(int state) { this.state = state; } @override public void init(gamecontainer _arg0, statebasedgame _arg1) throws slickexception { scoreboard = new image("res/scoreboard.png"); backs = new image("res/1stageback.gif"); isbeforemiddleboss = true; ismiddleboss = false; isbeforeboss = false; isboss = false; _arg0.setshowfps(false); } @override public void render(gamecontainer arg0, statebasedgame _arg1, graphics _g) throws slickexception { this._g = _g; new mob().getgraphics(_g);//i passed graphics new char().getgraphics(_g);//i passed graphics new bullet().getgraphics(_g);//i passed graphics _g.drawimage(scoreboard, 550, 5); _g.drawimage(backs, 10, 10); _g.drawstring(fps, 580, 570); _g.drawstring("high score-> not avaiable", 560, 60); _g.drawstring("score-> " + currscore, 595, 80); } @override public void update(gamecontainer _arg0, statebasedgame _arg1, int arg2) throws slickexception { fps = "frame per second-> " + _arg0.getfps(); bossvar b = bosschecker(); switch(b) { case beforemiddle : break; case boss : break; default: break; } } @suppresswarnings("static-access") @override public int getid() { return this.state; } private static int state; private static int currscore = 0; private static final int originx = 270; private static final int originy = 490; public static int x = originx; public static int y = originy; private static string fps; private image scoreboard; private image backs; private graphics _g; public boolean isbeforemiddleboss; public boolean ismiddleboss; public boolean isbeforeboss; public boolean isboss; }
char.java
package net.charlesdickenson; import org.lwjgl.input.keyboard; import org.newdawn.slick.graphics; import org.newdawn.slick.image; import org.newdawn.slick.slickexception; public class char extends bullet implements entity { @override public void getgraphics(graphics _g) { this._g = _g;//so got graphics, if(!isinit) return; _g.drawimage(char, getcharx(), getchary());//this codes doesn't works. } @override public int getcharx() { switch(vartracker.stage) { case 1: return firststage.x; } return 0; } @override public int getchary() { switch(vartracker.stage) { case 1: return firststage.y; } return 0; } public void setcharx(int i) { system.out.println("asdgagsd"); switch(vartracker.stage) { case 1: firststage.x += i; } } public void setchary(int i) { system.out.println("asdgagsd"); switch(vartracker.stage) { case 1: firststage.y += i; } } @override public void update() { if(!isinit) return; _g.drawimage(char, getcharx(), getchary());//this code doesn't work, too. = keyboard.iskeydown(keyboard.key_up); down = keyboard.iskeydown(keyboard.key_down); left = keyboard.iskeydown(keyboard.key_left); right = keyboard.iskeydown(keyboard.key_right); shift = keyboard.iskeydown(keyboard.key_lshift); z = keyboard.iskeydown(keyboard.key_z); if(up && !shift) { setchary(6); } if(down && !shift) { setchary(-6); } if(left && !shift) { setcharx(-6); } if(right && !shift) { setcharx(6); } if(up && shift) { setchary(2); } if(down && shift) { setchary(-2); } if(left && shift) { setcharx(-2); } if(right && shift) { setcharx(2); } if(z) { new bullet().isfiring = true; } if(!z) { new bullet().isfiring = false; } } @override public void init() { system.out.println("<char> initializing..."); isinit = false; try { char = new image("res/char.png"); } catch (slickexception e) { e.printstacktrace(); } isinit = true; system.out.println("<char> done init()"); } private boolean up; private boolean down; private boolean left; private boolean right; private boolean shift; private boolean z; private boolean isinit; private image char; private graphics _g; }
i passed graphics other class using getgraphics method, put image, doesn't work. @ render method, worked, can't put image in other class.
the reason doesn't work using graphics
incorrectly. when slick2d
draws something, uses render
method. method passed instance of graphics
, can draw stuff. when call ends graphics
object no longer useful anything. there no reason pass doesn't use immediately.
what want create render
method in mob, char , bullet classes. make instances of said classes outside of render
method, instance in init
, store them in data structure, instance list
. in render
method, simple traverse list
, call render
or draw
on each element. quick example:
// give entity interface 2 methods if don't exist already: public interface entity { void render(graphics g); void update(int delta); } // in firststage.java list<entity> list; // in init() method public void init(gamecontainer container, statebasedgame game) throws slickexception { ... list = new arraylist<entity>(); list.add(new mob()); list.add(new char()); list.add(new bullet()); } // in render method public void render(gamecontainer container, statebasedgame game, graphics g) throws slickexception { ... (entity e : list) { e.draw(g); } } // in update method public void update(gamecontainer container, statebasedgame game, int delta) throws slickexception { ... (entity e : list) { e.update(delta); } }
tl;dr version: graphics object exists drawn in single render call. render called many times second, object creation in method not recommended. object oriented programming @ modeling objects. games tend model lot of objects. make use of it.
Comments
Post a Comment