Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [Java] Syntax errors?  (Read 3426 times)

0 Members and 1 Guest are viewing this topic.
[Java] Syntax errors?
« on: November 10, 2013, 09:26:44 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
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:
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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;
}
}

Logged
  • Super Fan Gamers!
Re: [Java] Syntax errors?
« Reply #1 on: November 10, 2013, 09:58:19 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Disregard.  I am a moron.  I placed those functions inside update().  >_>
Logged
  • Super Fan Gamers!
Re: [Java] Syntax errors?
« Reply #2 on: November 10, 2013, 04:42:07 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Just as a note, this isn't the best way to handle movement of an entity. Separating movement commands into functions such as moveRight() and moveLeft() is a start (as it allows you to abstract between AI and the player), but directly modifying the velocity of an object (which should honestly be stored as a vector value, but I understand that the tutorial is probably trying to keep things simple) is a pretty big no-no. In this case, what if you were to attempt to add some form of a speed boost to your character? You'd ultimately end up just setting your speed back to the base value, or worse -- writing special-case code for each modification you want to implement to speed.

The simplest way is to store boolean values for different types of input (many ways to do this, let me know if you want any suggestions) and instead modify those based on your move*() methods, jumps, etc and then react to them in some form of update() method.

Also, some of your variable names don't make any sense at all -- when choosing names for variables, try to select something that tells you what the purpose of that variable is. Also, it is best to keep them scoped as close to where they're needed as possible. An example for both of these is the variable second -- it is only ever used in your update() method, so there is no reason to declare it as a member variable, and the name "second" doesn't give any indication of what it's used for. Based on what the code is doing with it, might I suggest naming it "imageBuffer," "imageGraphics" or something thereabouts? I see that you're only capturing this when you construct the image being used as a buffer (I suggest doing this in some for of initialization method, by the way), but there's no reason not to grab it each frame instead. When doing this, you aren't actually constructing a new Graphics instance, you're just capturing a reference to one that already exists, so you aren't incurring any (unreasonable) overhead.
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: [Java] Syntax errors?
« Reply #3 on: November 11, 2013, 07:35:45 am »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Just as a note, this isn't the best way to handle movement of an entity. Separating movement commands into functions such as moveRight() and moveLeft() is a start (as it allows you to abstract between AI and the player), but directly modifying the velocity of an object (which should honestly be stored as a vector value, but I understand that the tutorial is probably trying to keep things simple) is a pretty big no-no. In this case, what if you were to attempt to add some form of a speed boost to your character? You'd ultimately end up just setting your speed back to the base value, or worse -- writing special-case code for each modification you want to implement to speed.

The simplest way is to store boolean values for different types of input (many ways to do this, let me know if you want any suggestions) and instead modify those based on your move*() methods, jumps, etc and then react to them in some form of update() method.

Also, some of your variable names don't make any sense at all -- when choosing names for variables, try to select something that tells you what the purpose of that variable is. Also, it is best to keep them scoped as close to where they're needed as possible. An example for both of these is the variable second -- it is only ever used in your update() method, so there is no reason to declare it as a member variable, and the name "second" doesn't give any indication of what it's used for. Based on what the code is doing with it, might I suggest naming it "imageBuffer," "imageGraphics" or something thereabouts? I see that you're only capturing this when you construct the image being used as a buffer (I suggest doing this in some for of initialization method, by the way), but there's no reason not to grab it each frame instead. When doing this, you aren't actually constructing a new Graphics instance, you're just capturing a reference to one that already exists, so you aren't incurring any (unreasonable) overhead.

You make good suggestions.  If I were coding my own game, I would probably have thought of better names and functions.  Java is still new to me so I was just following the tutorial to the letter as it rolls on.

If you are curious to the tutorial itself, kilobolt.com  I don't know if his method is the best to learn from, but it seems simple enough.  Most of the knowledge I gained from C# and GML seems to apply in Java which is nice.

My reasoning for learning Java game programming(more so Android) is to hopefully get a jRPG on the market.  I've had a 70 page notebook filled with algorithms for attacks, magic, level-ups and all sorts of monsters with their stats.  Zelda QoC is still being programmed - Java is just my "gotta do something else for an hour" thing.
Logged
  • Super Fan Gamers!
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.183 seconds with 45 queries.