Computer Science Canada Sprite Movement when key is pressed |
Author: | huskiesgoaler34 [ Thu Jun 02, 2011 12:40 pm ] | ||
Post subject: | Sprite Movement when key is pressed | ||
Hey, I am trying to create a similar game to PacMan called PacMaze. I was able to put all the Sprites i want onto the screen but I am having trouble getting my sprite, the yellow PacMan to move. I put in code that I thought would move the sprite when each arrow key was pressed but it isn't working. However, there are no errors. The program works fine but the sprite doesn't move. (Note: I know that it flickers (I'll fix that later) and I don't want the ghosts to move yet, just the PacMan.) Can someone help me out. I inserted the if statements but the image isn't moving. Thanks in advance.
|
Author: | huskiesgoaler34 [ Thu Jun 02, 2011 5:27 pm ] |
Post subject: | Re: Sprite Movement when key is pressed |
I tried searching the site as well as the internet and I really haven't been able to find an answer to my problems.... |
Author: | Zren [ Thu Jun 02, 2011 7:07 pm ] | ||
Post subject: | RE:Sprite Movement when key is pressed | ||
Pretty sure you forgot this line of some sort: typingArea.addKeyListener(this); http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Since your object is both the target area and the listener, I'm guessing you'd do something like: this.addKeyListener(this); |
Author: | huskiesgoaler34 [ Thu Jun 02, 2011 8:42 pm ] |
Post subject: | Re: Sprite Movement when key is pressed |
Yeah, your right that I used the addKeyListener (this) statement. I added the section of code that you provided. It complied but it still didn't make my sprite move. I took a look at the link you gave me and by understanding, the typing.area commands, from my understanding, allow the result of whether or not a key was pressed to be displayed (in a text frame). I have already included code (if statements, variable declarations, etc.) but it doesn't move the sprite. I added the key listener, I have the Key Pressed, Released, etc. methods. What am I missing? |
Author: | Zren [ Thu Jun 02, 2011 8:46 pm ] |
Post subject: | RE:Sprite Movement when key is pressed |
It looks like the coordinates that you're drawing the pacman image to screen is hardcoded... g.drawImage (img,345,255,this); |
Author: | huskiesgoaler34 [ Thu Jun 02, 2011 8:49 pm ] |
Post subject: | Re: Sprite Movement when key is pressed |
Does that mean i will have trouble getting the sprite to move? By the way, what does hard coded mean? |
Author: | huskiesgoaler34 [ Thu Jun 02, 2011 8:56 pm ] |
Post subject: | Re: Sprite Movement when key is pressed |
Hard coding (also, hard-coding or hardcoding) refers to the software development practice of embedding what may, perhaps only in retrospect, be regarded as input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input. Ok, now I now what hardcoding means but what is the problem when it comes to making the image move? |
Author: | Zren [ Thu Jun 02, 2011 9:35 pm ] |
Post subject: | RE:Sprite Movement when key is pressed |
The numbers are constant. They don't change. They aren't variable. You have x and y variables but you aren't using them. Actually you are, but you're using them for the location of a label. Which you never pack into anything. In other words, it's down on paper, but you never make it and put it on something. http://download.oracle.com/javase/tutorial/uiswing/examples/components/LabelDemoProject/src/components/LabelDemo.java Since you're drawing the image, and are locally storing the coordinates, why are you using a label instead of: g.drawImage (img, x, y, this); |
Author: | huskiesgoaler34 [ Fri Jun 03, 2011 12:57 pm ] |
Post subject: | Re: Sprite Movement when key is pressed |
Thanks Zren. I finally found my problem. I am finally able to make the PacMan move. Thanks again. |