SentientStone
Member
I am currently making some sort of controllable side scroller, and I currently am using AbstractActions to move the character left and right.
The thing is that I start off with an idle animation and then the character changes to a walking animation which is initiated inside the AbstractAction code. The problem I have is that after I release the left or right keys, the character does not go back to its idle animation.
I have tried making a noAction AbstracAction thing but I am not sure if I can (just screwing around). Anyway, it might be simple (or not) but I have no idea how to do it right now.
My current code for right movement:
// handle right key
AbstractAction rightAction = new AbstractAction() {
private static final long serialVersionUID = 9124806384729591969L;
public void actionPerformed(ActionEvent ae) {
Background background = graphicPanel.getBackgroundObj();
background.moveRight();
f.repaint();
graphicPanel.foregroundImage = Toolkit.getDefaultToolkit().getImage("turtlewalk.gif");
}
};
((JPanel) f.getContentPane()).getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
((JPanel) f.getContentPane()).getActionMap().put("right", rightAction);
This is within my main class.
In my GraphicsPanel class I first initiated the idle animation via,
public GraphicPanel() {
setPreferredSize(new Dimension(3000, 500));
Image image = Toolkit.getDefaultToolkit().getImage("turtlebg.jpg");
background = new Background(image, this);
Border border = BorderFactory.createLineBorder(Color.black);
this.setBorder(border);
foregroundImage = Toolkit.getDefaultToolkit().getImage("turtleidle.gif");
}
I am not sure if anyone knows what I am talking about (I am not so certain myself... and I am berry sleepy having not slept the whole night trying to figure this out :lol), hopefully someone understands.
So what is the best and easiest way to revert back to the initial idle animation in this case?
PS: Also any ideas on how to implement an infinitely looped background?
The thing is that I start off with an idle animation and then the character changes to a walking animation which is initiated inside the AbstractAction code. The problem I have is that after I release the left or right keys, the character does not go back to its idle animation.
I have tried making a noAction AbstracAction thing but I am not sure if I can (just screwing around). Anyway, it might be simple (or not) but I have no idea how to do it right now.
My current code for right movement:
// handle right key
AbstractAction rightAction = new AbstractAction() {
private static final long serialVersionUID = 9124806384729591969L;
public void actionPerformed(ActionEvent ae) {
Background background = graphicPanel.getBackgroundObj();
background.moveRight();
f.repaint();
graphicPanel.foregroundImage = Toolkit.getDefaultToolkit().getImage("turtlewalk.gif");
}
};
((JPanel) f.getContentPane()).getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
((JPanel) f.getContentPane()).getActionMap().put("right", rightAction);
This is within my main class.
In my GraphicsPanel class I first initiated the idle animation via,
public GraphicPanel() {
setPreferredSize(new Dimension(3000, 500));
Image image = Toolkit.getDefaultToolkit().getImage("turtlebg.jpg");
background = new Background(image, this);
Border border = BorderFactory.createLineBorder(Color.black);
this.setBorder(border);
foregroundImage = Toolkit.getDefaultToolkit().getImage("turtleidle.gif");
}
I am not sure if anyone knows what I am talking about (I am not so certain myself... and I am berry sleepy having not slept the whole night trying to figure this out :lol), hopefully someone understands.
So what is the best and easiest way to revert back to the initial idle animation in this case?
PS: Also any ideas on how to implement an infinitely looped background?