[java] Why won't this work? Does anyone know?

Started by
5 comments, last by redarcher 20 years, 5 months ago
The code below is just declaring variables and if statements for keyboard input commands, and for some funny reason; my ide(compiler) can't reconize the variables. For example: I made some sprites display onto the screen, but it won't happen unless the variables are declared. That's exactly what I did but, it doesn't reconize that I did that. How can I make it work? import java.applet.Applet; import java.awt.Graphics; public class Sprite Display Program extends Applet { public boolean keyDown (Event e, int key) { if (key == Event.LEFT) { x_speed = -1; } else if (key == Event.RIGHT) { x_speed = 1; } else if (key == 32) { x_speed = 0; } } } [edited by - redarcher on November 13, 2003 9:47:52 PM]
Advertisement
Is that the entire class?

Class names can''t have spaces in them.
All variables in Java must be declared. Are you coming from something like python?
No No, I''m positive that this is java. Um.. yeh, that''s the whole class. I just don''t want to fit all that into one class file, it would run too slow. That''s why I put the rest onto a
seperate program.

The purpose of this one is: when you press an arrow key, it will activate a variable and set it to true(using booleans) and then locate the other class file(the sprite & sprite vector class) and run those programs. But for some reason it won''t work, I''m sure it''s suppose to.

Perhaps you could help me out here.
If you know, email me at: tumpynobusi@hotmail.com
Wesley Breden,

Java Network Programmer.
Where is x_speed declared? It has to be declared in SpriteDisplayProgram in this particular situation, but it's not.

I asked if you usually program in Python or somethign like it and are now trying Java. Because in Python you don't declare variables. I think you might need to hit the basics a little bit more.

Just from the code you gave, remove the spaces from "Sprite Display Program" just have it "SpriteDisplayProgram", then put "int x_speed;" right after the first bracket. That will fix this problem, but again i think you need to get stronger on the basics a bit.

[edited by - tortoise on November 13, 2003 10:48:18 PM]
Oh, yeh It made a big difference! Thanks, it''s a bit glitchy.
Yeh, I know. My basic java skills are a bit off, I forgot alot
of how to program in java. I guess I spend more time with c++,
it''s so simular to java tho. Oh well, I better practice then.
Thanks again!
quote:Original post by redarcher
The code below is just declaring variables and if statements for keyboard input commands, and for some funny reason; my ide(compiler) can''t reconize the variables. For example: I made some sprites display onto the screen, but it won''t happen unless the variables are declared. That''s exactly what I did but, it doesn''t reconize that I did that. How can I make it work?

import java.applet.Applet;
import java.awt.Graphics;

public class Sprite Display Program extends Applet


This is definitely wrong. Class names in any languages can''t have spaces in them.

quote:
{
public boolean keyDown (Event e, int key)
{
if (key == Event.LEFT)
{
x_speed = -1;


you should define x_speed, that might help

quote:
}

else if (key == Event.RIGHT)
{
x_speed = 1;
}

else if (key == 32)
{
x_speed = 0;
}
}
}

[edited by - redarcher on November 13, 2003 9:47:52 PM]


Anyways, see if that helps.

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com][ Administrator & WebMaster GuLSE]
[Cyberdrek | ]
quote:Original post by redarcher
Oh, yeh It made a big difference! Thanks, it''s a bit glitchy.
Yeh, I know. My basic java skills are a bit off, I forgot alot
of how to program in java. I guess I spend more time with c++,
it''s so simular to java tho. Oh well, I better practice then.
Thanks again!


You''re a bit confusing. In one post, you sign saying that your a Java Network Programmer and in the other you say that your C++ skills are better than your Java. Why don''t you simply state that you''re a newbie that''s trying to learn. It might help you.


[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com][ Administrator & WebMaster GuLSE]
[Cyberdrek | ]

This topic is closed to new replies.

Advertisement