• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Java help!? (AbstractAction)

Status
Not open for further replies.
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?
 

iapetus

Scary Euro Man
Easiest way taking this sort of approach would be to set up another action for key releases, and reset the animation to idle in that (and to make the moving animation action only trigger on key presses).

I have a feeling that would work pretty horribly, but it would at least work. :p Watch out for situations where you have multiple keys pressed.
 

joedan

Member
does anyone here have experience using GWT Google Web ToolKit in an Eclipse environment? How to you set up a CSS style sheet?
 

mashoutposse

Ante Up
Going with your noAction idea, couldn't you try something like this?

AbstractAction noAction = new AbstractAction() {
private static final long serialVersionUID = 9124806384729591969L;

public void actionPerformed(ActionEvent ae) {
graphicPanel.foregroundImage = Toolkit.getDefaultToolkit().getImage("turtleidle.gif");
}
};
((JPanel) f.getContentPane()).getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "rightRELEASED");
((JPanel) f.getContentPane()).getActionMap().put("rightRELEASED", noAction);
 
Status
Not open for further replies.
Top Bottom