Closed Thread Icon

Topic awaiting preservation: addActionListener and sprites (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=23758" title="Pages that link to Topic awaiting preservation: addActionListener and sprites (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: addActionListener and sprites <span class="small">(Page 1 of 1)</span>\

 
Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 10-22-2004 17:44

I'm working on a project (different one from the one with the CLI, which I have yet to get back to...), but I've already got our sprites so that I can script movements in a .txt file, and I even have sounds and narrations playing, and everything is synched up and beautiful (well, ok, I have a couple slight bugs to iron out, but nothing a few hours won't fix).

What I need now is to add an action listener to certain sprites. A mouse listener to be precise. I've kinda hacked a couple things together, but no luck so far. Both ways involved making my Sprite class extend JButton (I would suppose that JMenuItem or something else that also has the ability to add a listener would work as well, at least I think). Then adding the listener to the sprite, and displaying the sprite like I had been all along. The sprite displayed, but no listener. The other way was to add the sprite as a button to a panel and add that to the contentpane, which gave me a standard gray button taking up the top left corner of the screen, which had the listener but no sprite.

Is there something simple I missed? If possible, I'd prefer to do it similar to the first way I mentioned here. I found a way to do it the second way (I just have to figure out how to work it into my code), but I'd rather not have to worry about placing buttons around the screen (unless there's a layout manager that allows absolute positioning?)

Any ideas or comments are appreciated,
LF

[edit: never mind the comment about absolute positioning of buttons... setLocation() ::
So would that probably be the best way to do this? I'd just have to keep track of which sprites have action listeners and which don't since I'm using AffineTransform for everything right now, but setLocation for 'buttons' ... Maybe change everything over to setLocation later?

(Edited by Lord_Fukutoku on 10-22-2004 18:23)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-24-2004 05:06

If you are keeping track of the sprite location (which you must be doing), you can add a mouse motion listener to the frame and keep track of the mouse location clicks and such, with a small algorithm you will find out which sprites are being affected. In your sprite class you can also make it so you just supply the x and y locaitons and have it return a boolean based on if a collision occured or not.

Dan @ Code Town

Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 10-25-2004 19:28

The sprites are keeping track of their own position. I have an ArrayList of sprites that are in use, and my sprite class has an update method which updates its position (and whatever else needs updated), then they are drawn to the screen. I suppose I can do something similar to check for a collision; run through my list of sprites in use and get their position and size to check for a colision.

Thank you much WM

InI
Maniac (V) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 10-25-2004 19:46

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 10-25-2004 21:52

Works just fine now, thanks again WM

INI - Yep, that's what I ended up with. I haven't gone through and tweaked it yet to optimize the execution time, thanks for the reminder (there are several places I'll go through and touch up once I get a little more content together).

LF

« BackwardsOnwards »

Show Forum Drop Down Menu