0

In the assignment I was given for my Uni module titled problem solving & programming.

I was given a scenario with errors in it and from reading the assignment the code listed below is where the errors are.

So far I found that in the public void key section of my code I keep getting the class expected error however since I'm a total programming newbie I have no idea how to fix the problem.

I have tried to find a solution on the internet however I have no idea what to search for though my friends said that using stackoverflow is great if you have problems related to programming so I thought I would give it a try as I would appreciate some help.

public boolean canMove(int x, int y) {

    Actor sand;
    sand=getOneObjectAtOffset(x,y,sandroad.class);

    //the section below checks if there is a block you can move to
    // if there is it sets sand to a vlaue otherwise it says null
    // The errors are in this section
    boolean flag=true;
    if (sand !=null)
    {
        flag=false;
    }
    return flag;
}
public void key()
{
   //Note 1: Down the page increase the y value and going to the right increases the x value
   //Note 2: Each block is 60 pixels wide and high 
    int leftChange=//choose the appropriate left step size ; 
    int rightChange=//choose the appropriate right step size ; 
    int upChange=//choose the appropriate up step size ; 
    int downChange=//choose the appropriate down step size ; 
    if (Greenfoot.isKeyDown("left"))
    {
        if (canMove(leftChange, 0)==true)
        setLocation(getX()+leftChange, getY()) ;
    }
    if (Greenfoot.isKeyDown("right"))
    {
       if (canMove(rightChange, 0)==true)
        setLocation(getX()+rightChange, getY()) ; 
    }
    if (Greenfoot.isKeyDown("up"))
    {
        if (canMove(0, upChange)==true)
        setLocation(getX(), getY()+upChange) ;
    }
    if (Greenfoot.isKeyDown("down"))
    {
        if (canMove(0, downChange)==true)
        setLocation(getX(), getY()+downChange) ;
    }
}
  • Are those methods part of some class? – Eran Jan 18 '15 at 12:25
  • Actually having read all my code I don't think the methods under the public void key part have a class as all other sections such as public void win and public boolean canMove have a .class within them yet public key does not of course I'm not sure which methods you were talking about specifically. – KingYoshi HD Jan 18 '15 at 12:38
  • `canMove` and `key`. Any method in Java must be part of some class. – Eran Jan 18 '15 at 13:29

1 Answers1

0

Well the multiple int's in key() aren't set to anything. You can't leave them like that.

    int leftChange=//choose the appropriate left step size ; 
    int rightChange=//choose the appropriate right step size ; 
    int upChange=//choose the appropriate up step size ; 
    int downChange=//choose the appropriate down step size ; 

so should be something like

int leftChange=4;

for each one.

Austin
  • 4,801
  • 6
  • 34
  • 54