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

2014/10/3

How to use abstract classes to make multiple maps

Kirito101 Kirito101

2014/10/3

#
this is my code.The first map works and makes the map.The second needs to make the characters on the map.I'm not really sure what i'm doing though.
import greenfoot.*;

public abstract class Level extends World
{
    String[] map,map2;

    public Level()
    {
        super(576, 576, 1);
        //setBackground(new GreenfootImage("StartScreen.png")); // Splash Screen
        setFields();
        for (int i=0; i<map.length; i++) for (int j=0; j<map[i].length(); j++)
            {
                int kind = "ABCDEFGHIJKLMNOPQRSTU".indexOf(""+map[i].charAt(j));
                if (kind < 0) continue;
                Actor actor = null;
                if (kind == 0) actor = new grass();     //A
                if (kind == 1) actor = new CastleA();   //B
                if (kind == 2) actor = new CastleB();   //C
                if (kind == 3) actor = new CastleC();   //D
                if (kind == 4) actor = new CastleD();   //E
                if (kind == 5) actor = new PathA();     //F
                if (kind == 6) actor = new PathB();     //G
                if (kind == 7) actor = new PathC();     //H
                if (kind == 8) actor = new RiverA();    //I
                if (kind == 9) actor = new RiverB();    //J
                if (kind == 10) actor = new RiverL();    //K
                if (kind == 11) actor = new MountA();    //L
                if (kind == 12) actor = new Bridge();    //M
                if (kind == 13) actor = new BridgeB();    //N
                if (kind == 14) actor = new PathD();     //O
                if (kind == 15) actor = new MountB();    //P
                if (kind == 16) actor = new MountC();    //Q
                if (kind == 17) actor = new PathL();    //R
                if (kind == 18) actor = new tree();    //S
                if (kind == 19) actor = new Anri();    //T
                if (kind == 20) actor = new Maxx();    //U
                addObject(actor, 24+j*48, 24+i*48);
        }
        //setFields();
        /*for (int i2=0; i2<map2.length; i2++) for (int j2=0; j2<map2[i2].length(); j2++)
            {
                int kind2 = "T".indexOf(""+map2[i2].charAt(j2));
                Actor actor2 = null;
                if (kind2 == 1) actor2 = new Anri();    //T
                addObject(actor2, 24+j2*48, 24+i2*48);
            }*/
    }

    public void setFields() {}

    public void nextLevel() {}
}
danpost danpost

2014/10/3

#
You should not have a 'map2' field in this class. Each subclass of Level (or level world) will have its own map set to the 'map' array (from within that world's 'setFields' method) for the Level constructor to use.
Kirito101 Kirito101

2014/10/3

#
So I need a separate class for the character map.Is this what you mean or?
davmac davmac

2014/10/3

#
When you post an image link, you must provide the URL of the image itself, not the page that contains the image. In this case the image URL is http://i.imgur.com/evXAUrZ.png
Kirito101 Kirito101

2014/10/3

#
Also the characters are moving strangely when interacting with the river when they are intended to stop in the character superclass the code is the following:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Used to control characters.
 * glitches regarding collision with River where characters will move
 * directions not intended-intended to move 1 square in direction of key pressed
 * 
 * @author Kirito101 
 * @version (a version number or a date)
 */
public class Characters extends Actor
{
    int Direction=1;int runtime=0;
    int switcher =0;boolean run,run1,run2,run3=false;
    /**
     * Act - do whatever the Characters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Characters()
    {
        World world;
       world=getWorld();
       
    }
    public void detect()
    {
        String key = Greenfoot.getKey();
        if ( Greenfoot.isKeyDown("q"))
                
                    switcher=1;
        if ( Greenfoot.isKeyDown("e"))
                    switcher=0;
    }
    public int Direction()
    {
        int oldLocate = getY();int newLocate;
        //RiverA collision
        Actor RiverR=getOneObjectAtOffset(24,0,RiverA.class);
        Actor RiverL=getOneObjectAtOffset(-24,0,RiverA.class);
        Actor RiverT=getOneObjectAtOffset(0,-24,RiverA.class);
        Actor RiverB=getOneObjectAtOffset(0,24,RiverA.class);
        //RiverB collision
        Actor RiverBR=getOneObjectAtOffset(24,0,RiverB.class);
        Actor RiverBL=getOneObjectAtOffset(-24,0,RiverB.class);
        Actor RiverBT=getOneObjectAtOffset(0,-24,RiverB.class);
        Actor RiverBB=getOneObjectAtOffset(0,24,RiverB.class);
        
        if(Greenfoot.isKeyDown("Up")||run==true)
        {
            Direction=3;
            if(RiverT == null&&RiverBT == null)
            {setLocation(getX(), getY()-2);runtime++;
                if(runtime<=24)
                run=true;
                else{
                runtime=0;run=false;}
            }
             else
            runtime=0;
        }
        else if(Greenfoot.isKeyDown("Right")||run1==true)
        {
            Direction=4;
            //loop to move 1 square
            if(RiverR == null&&RiverBR == null)
            { move(2);runtime++;
                if(runtime<=24)
                run1=true;
                else{
                runtime=0;run1=false;}
            }
             else
            runtime=0;
        }
        else if(Greenfoot.isKeyDown("Left")||run2==true)
        {
            Direction=2;
            if(RiverL == null&&RiverBL == null)
            {move(-2);runtime++;
                if(runtime<=24)
                run2=true;
                else{
                runtime=0;run2=false;}
            }
            else
            runtime=0;
        }
        else if(Greenfoot.isKeyDown("Down")||run3==true)
        {
            Direction=1;
            if(RiverB == null&&RiverBB == null)
            {setLocation(getX(), getY()+2);runtime++;
                if(runtime<=24)
                run3=true;
                else{
                runtime=0;run3=false;}
            }
             else
            runtime=0;
        }
        return Direction;
    }
}
danpost danpost

2014/10/3

#
Kirito101 wrote...
So I need a separate class for the character map.Is this what you mean or?
No. That is not what I meant. Use the scenario you got the Level class from as an example of how your 'map' array should be set for each level.
You need to login to post a reply.