This site requires JavaScript, please enable it in your browser!
Greenfoot back
Sparkeroo
Sparkeroo wrote ...

2015/5/1

Basic tower defense game, moving down is an issue

Sparkeroo Sparkeroo

2015/5/1

#
In my tower defence game the monsters are supposed to travel along different pieces of track to get to the end, as it stands now the actor can move right, up and left but as soon as i introduce down it has a compile error
public void move()
    {
        Actor righttrack = getOneIntersectingObject(trackmoveright.class);
        Actor uptrack = getOneIntersectingObject(trackmoveup.class);
        Actor downtrack = getOneIntersectingObject(trackmovedown.class);
        Actor lefttrack = getOneIntersectingObject(trackmoveleft.class);
        
        /**
        if (righttrack!=null)
        {
            setLocation(getX() + 1, getY());
           return;
            
        }
        
        if (uptrack != null)
        {
            setLocation(getX(), getY() -1);
            return;
        }

      /**
       if (downtrack != null);
        {
            setLocation(getX(), getY() +1);
            return;
        }
         
        
        if (lefttrack != null);
        {
            setLocation(getX() -1, getY());
            return;

        }
        */

    }
Can anyone see what im doing wrong here
Super_Hippo Super_Hippo

2015/5/1

#
What is the error message?
azazeel azazeel

2015/5/2

#
if (downtrack != null); if (lefttrack != null); should be if (downtrack != null) if (lefttrack != null) without semicolon
Sparkeroo Sparkeroo

2015/5/4

#
I did what azazeel said and removed the semicolons and i still get the error message "unreachable statement" on
if (uptrack != null)
Super_Hippo Super_Hippo

2015/5/4

#
Show the current code of the whole method.
Sparkeroo Sparkeroo

2015/5/8

#
public void move()
    {
        Actor righttrack = getOneIntersectingObject(trackmoveright.class);
        Actor uptrack = getOneIntersectingObject(trackmoveup.class);
        Actor downtrack = getOneIntersectingObject(trackmovedown.class);
        Actor lefttrack = getOneIntersectingObject(trackmoveleft.class);
       
        if (righttrack!=null)
        {
            setLocation(getX() + 1, getY());
            
            return;
        }
        if (lefttrack != null)
        {
            setLocation(getX() -1, getY());
            
            return;
        
        if (uptrack != null)
        {
            setLocation(getX(), getY() -1);
            
            //return;
        }
        
        
       /**
        if (downtrack != null)
        {
            setLocation(getX(), getY() +1);
            
            //return;
            
        }
        */
        
    }
Super_Hippo Super_Hippo

2015/5/8

#
Add a } in line 19.
Sparkeroo Sparkeroo

2015/5/8

#
I can't believe i missed that, thank you very much
You need to login to post a reply.