Reputation: 1870
I'm creating a card game and using slick2d for graphics. When I call my method to draw a card from a deck in public void init it runs twice. Below method draws a card from the deck constructor and returns the index number.
public Card PlayerCardDraw()
{
Card index = cards.remove(1);
System.out.println("Cards left: " + cards.size());
return index;
}
Deck constructor:
Deck()
{
cards = new ArrayList<Card>();
for (int a = 0; a < 52; a++)
{
cards.add(new Card(a));
}
}
public class Play extends BasicGameState
{
Deck deck = new Deck ();
Image PlayScreen;
Image PauseButton;
Image FaceDown1, FaceDown2;
Image card[] = new Image [52];
public String mouse = "no input";
Card C;
public Play (int state)
{
}
public void init (GameContainer gc, StateBasedGame sbg) throws SlickException
{
PlayScreen = new Image ("res/Play/PlayScreen.png");
PauseButton = new Image ("res/Play/PauseButton.png");
FaceDown1 = new Image("res/CardBacks/redback1.png");
FaceDown2 = new Image("res/CardBacks/redback2.png");
String fileLocation = new String ();
for (int i = 0 ; i < 52 ; i++)
{
fileLocation = "res/cards/" + (i+1) + ".png";
card [i] = new Image (fileLocation);
}
//should only run once, right?
C = deck.PlayerCardDraw();
}
the output i get when ran is Cards left: 51 Cards left: 50
This means the PlayerCardDraw() was called twice. I added in a simple system out statement saying "hi" and it ran twice as well.
does anybody know why this is happening and how to fix it? By the way, this stuff is running before the program even enters the play state. It displays cards left and hi in the title state which doesn't include any of this.
Upvotes: 1
Views: 629
Reputation: 11
I realize that this is a very old question, but I had the exact same problem and I just figured it out. For me, it was that I was initializing my screens in the constructor of my StateBasedGame
class and also in the initStatesList()
function.
This was my code:
public TheGame(String name)
{
super(name);
this.addState(new TitleScreen(TheGame.MENUSTATEID));
this.addState(new HelpScreen(TheGame.HELPSTATEID));
this.addState(new GameScreen(TheGame.PLAYSTATEID, GAMEWIDTH, GAMEHEIGHT));
this.addState(new PauseScreen(TheGame.PAUSESTATEID));
this.addState(new GameOverScreen(TheGame.GAMEOVERSTATEID));
this.addState(new HighScoresScreen(TheGame.GAMERESULTSSTATEID));
}
public void initStatesList(GameContainer arg0) throws SlickException
{
this.getState(TheGame.MENUSTATEID).init(arg0, this);
this.getState(TheGame.HELPSTATEID).init(arg0, this);
this.getState(TheGame.PLAYSTATEID).init(arg0, this);
this.getState(TheGame.PAUSESTATEID).init(arg0, this);
this.getState(TheGame.GAMEOVERSTATEID).init(arg0, this);
this.getState(TheGame.GAMERESULTSSTATEID).init(arg0, this);
this.enterState(TheGame.MENUSTATEID);
}
This is the way my Computer Science teacher taught my class to do it but he's new to Slick 2D and so I think he just made a mistake. I fixed it by removing all the getState()
lines in initStatesList()
.
I hope this was helpful.
Upvotes: 1