I been following a great game tutorial on Java(before you hiss, it gets ported to Android in the end). However , I hit a part where my IDE (Eclipse just as the writer uses) is giving me syntax errors.
Here's the code:
public void moveRight()
{
speedX = 6;
}
public void moveLeft()
{
speedX = -6;
}
public void stop()
{
speedX = 0;
}
public void jump()
{
if(jumped == false)
{
speedY = -15;
jumped = true;
}
}
And there are errors on moveLeft moveRight and stop:
Multiple markers at this line
- Syntax error on token "void", @
expected
- Syntax error on token(s), misplaced
construct(s)
And on jump
Multiple markers at this line
- Syntax error, insert "EnumBody" to complete BlockStatement
- Syntax error on token "void", @ expected
- Syntax error, insert "enum Identifier" to complete
EnumHeaderName
All my code looks like the tutorial code, so I don't get why I am getting errors. My IDE is slightly different as it is Eclipse that is bundled with ADT but it's still Eclipse just with the Android Tools built in instead of having to manually add them.
Anyway, was wondering if anyone here might know the issue? If you need the full code:
StartingClass.java
package kiloboltgame;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
public class StartingClass extends Applet implements Runnable, KeyListener
{
private Robot robot;
private Image image,character;
private Graphics second;
private URL base;
@Override
public void init()
{
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame)this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
try
{
base = getDocumentBase();
}
catch(Exception e)
{
//handle exception
}
}
@Override
public void start()
{
robot = new Robot();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop()
{
}
@Override
public void destroy()
{
}
@Override
public void run()
{
while(true)
{
repaint();
try
{
Thread.sleep(17);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
// TODO Auto-generated method stub
}
@Override
public void update(Graphics g)
{
if(image == null)
{
image = createImage(this.getWidth(),this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image,0,0,this);
}
@Override
public void paint(Graphics g)
{
g.drawImage(character, robot.getCenterX()-61, robot.getCenterY()-63,this);
}
@Override
public void keyPressed(KeyEvent e)
{
// TODO Auto-generated method stub
switch(e.getKeyCode())
{
case KeyEvent.VK_UP:
System.out.println("Move Up");
break;
case KeyEvent.VK_DOWN:
System.out.println("Move Down");
break;
case KeyEvent.VK_LEFT:
{
System.out.println("Move Left");
robot.moveLeft();
break;
}
case KeyEvent.VK_RIGHT:
{
System.out.println("Move Right");
robot.moveRight();
break;
}
case KeyEvent.VK_SPACE:
{
System.out.println("Jump");
robot.jump();
break;
}
}
}
@Override
public void keyReleased(KeyEvent e)
{
// TODO Auto-generated method stub
switch(e.getKeyCode())
{
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_SPACE:
break;
}
}
@Override
public void keyTyped(KeyEvent e)
{
// TODO Auto-generated method stub
}
}
Robot.java
package kiloboltgame;
import java.awt.Graphics;
public class Robot
{
private int centerX = 100;
private int centerY = 382;
private boolean jumped = false;
private int speedX = 0;
private int speedY = 1;
public void update()
{
//moves the character
if(speedX < 0)
{
centerX += speedX;
}
else if (speedX == 0)
{
System.out.println("do not scroll");
}
else
{
if(centerX <= 150)
{
centerX += speedX;
}
else
{
System.out.println("scrolling");
}
}
//Y position
if(centerY +speedY >= 382)
{
centerY = 382;
}
else
{
centerY += speedY;
}
//Jumping
if(jumped == true)
{
speedY+=1;
if(centerY + speedY >= 382)
{
centerY = 382;
speedY = 0;
jumped = false;
}
}
//x prevention
if(centerX + speedX <= 60)
{
centerX = 61;
}
public void moveRight()
{
speedX = 6;
}
public void moveLeft()
{
speedX = -6;
}
public void stop()
{
speedX = 0;
}
public void jump()
{
if(jumped == false)
{
speedY = -15;
jumped = true;
}
}
}
public int getCenterX() {
return centerX;
}
public int getCenterY() {
return centerY;
}
public boolean isJumped() {
return jumped;
}
public int getSpeedX() {
return speedX;
}
public int getSpeedY() {
return speedY;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public void setJumped(boolean jumped) {
this.jumped = jumped;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
}