Results 1 to 14 of 14
  1. #1
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default getting the current map I'm on

    I need some help trying to get the current map number out of the code of a class in another class. the class responsible for getting and switching map images is called Earth. The class I want to get the map number is called Creature. I cant edit the Earth class, only the Creature class. The code for the Earth
    Java Code:
    import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
    
    import java.awt.Color;
    
    /**
     * This is Earth. Or at least some remote, uninhabited part of Earth. Here, Greeps can
     * land and look for piles of tomatoes...
     * 
     * @author Michael Kolling
     * @version 1.0
     */
    public class Earth extends World
    {
        public static final int RESOLUTION = 1;
        public static final int WIDTH = 800;
        public static final int HEIGHT = 600;
    
        public static final int SCORE_DISPLAY_TIME = 240;
    
        private GreenfootImage map;
        private Ship ship;
        private int currentMap;
        
        // Data defining map layout. For each map, the first 3-tuple defines the space ship
        // location: { target-y, start-x, start-y } The ship moves from start-y to target-y.
        //
        // then follow an arbitrary number of additional 3-tuples, one for each tomato pile,
        // in the format: { number-tomatos-in-pile, x, y }
        //
        private int[][][] mapData = {
            { {480, 100, 0}, {40, 721, 532}, {12, 400, 560}, {40, 615, 400},    // map 1
              {40, 642, 192}, {16, 128, 113}, {30, 400, 40} },
     
            { {496, 709, 0}, {10, 322, 422}, {40, 700, 241}, {40, 681, 49},     // map 2
              {10, 317, 54}, {50, 90, 174}, {40, 60, 339} },
              
            { {272, 394, 0}, {10, 39, 30}, {30, 71, 476}, {50, 398, 520},       // map 3
              {40, 655, 492} },          
        };
    
        private int[] scores;
        
        /**
         * Create a new world. 
         */
        public Earth()
        {
            super(WIDTH / RESOLUTION, HEIGHT / RESOLUTION, RESOLUTION);
            currentMap = 0;
            scores = new int[mapData.length];    // one score for each map
            showMap(currentMap);
        }
        
        /**
         * Return true, if the specified coordinate shows water.
         * (Water is defined as a predominantly blueish color.)
         */
        public boolean isWater(int x, int y)
        {
            Color col = map.getColorAt(x, y);
            return col.getBlue() > (col.getRed() * 2);
        }
        
        /**
         * Jump to the given map number (1..n).
         */
        public void jumpToMap(int map)
        {
            clearWorld();
            currentMap = map-1;
            showMap(currentMap);
        }
        
        /**
         * Set up the start scene.
         */
        private void showMap(int mapNo)
        {
            map = new GreenfootImage("map" + mapNo + ".jpg");
            setBackground(map);
            Counter mapTitle = new Counter(Greep.getAuthorName() + " - Map ", mapNo+1);
            addObject(mapTitle, 200, 20);
            int[][] thisMap = mapData[mapNo];
            for(int i = 1; i < thisMap.length; i++) {
                int[] data = thisMap[i];
                addObject(new TomatoPile(data[0]), data[1], data[2]);
            }
            int[] shipData = thisMap[0];
            ship = new Ship(shipData[0]);
            addObject(ship, shipData[1], shipData[2]);
        }
        
        /**
         * Game is over. Stop running, display score.
         */
        public void mapFinished(int time)
        {
            displayScore(time);
            Greenfoot.delay(SCORE_DISPLAY_TIME);
            clearWorld();
            currentMap++;
            if(currentMap < mapData.length) {
                showMap(currentMap);
            }
            else {
                displayFinalScore();
                Greenfoot.stop();
            }
        }
    
        private void displayScore(int time)
        {
            int points = ship.getTomatoCount() + time;
            scores[currentMap] = points;
            ScoreBoard board = new ScoreBoard(Greep.getAuthorName(), "Map " + (currentMap+1), "Score: ", currentMap, scores);
            addObject(board, getWidth() / 2, getHeight() / 2);
        }
        
        private void displayFinalScore()
        {
            clearWorld();
            ScoreBoard board = new ScoreBoard(Greep.getAuthorName(), "Final score", "Total: ", scores);
            addObject(board, getWidth() / 2, getHeight() / 2);
        }
        
        private void clearWorld()
        {
            removeObjects(getObjects(null));
        }
    }
    .
    I dont think the code for the Creature class matters but if you need I can post it. I'm using java.lang and the code for the api can be found at
    Index (Greenfoot API)
    so how can I get the current map number I'm on by editing the Creature class only.
    p.s Counter is another class which mostly displays text on the screen and keeps track of a timer.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    trying to get the current map number out of the code of a class in another class.
    Is the map number an int value held in a variable in a class? What class is it in?
    Is there a method in that class that returns the value?
    Can you add a method to the class with the value that will return the value to the caller?

    Do you have a reference to the class that contains the value so you can call the method that will return the value you want?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    yes its an int value, it is in the class I gave you, no there is no method that returns a value, it would have much easier if it did. and even easier if I could edit that class but I'm not allowed to make any changes to the Earth class. I can call the current world my creature is in, so I have a reference but there is no method that returns the value of the map

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    What is the name of the int variable you want the value of and where is it defined? If there are no methods to return its value and you can not add a method, then you will not be able to get the value unless the variable is defined as public.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    ahh ok. the variable isnt public and the code is confusing. well I kind of found a way around that while I was waiting for a reply so never mind

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    Did you ever give the name of the variable that you want the value of?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    thats the thing. the code is kind of confusing. the field declared at the top that i think i related to the map is
    private int currentMap;
    but I get lost when I try to track that field in the code. even if I could get the String for the map's name like "map0.png" it would be ok, but I dont even know how to do that.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    You'll have to explain what you are trying to do and show the code that is trying to do it. It may not be possible if you can not change the class where the currentMap variable is defined.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    The code and the link to the api were posted in my first post. Ill see what I can do since I'm kind of confused by the code
    at the top the currentMap field is defined. then in the constructor at line 51 it says showMap(currentMap). at line 70 and 71
    currentMap = map-1;
    showMap(currentMap);
    at line 77 we find the shoMap method. at line 83 it says
    int[][] thisMap = mapData[mapNo];
    I think thats all the code related to the map number. in all of this, I need just something to tell me what map I'm on whether it be the array for the ship which is declared at line 31 and used in line 38 or the currentMap variable. unfortunately both are private so I dont know how to get information out of them. at line 79 and 80 the background image is set using a string. that string will also work. I just need some way tell my creature that if we are on this map run this set of code.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    What code calls the: jumpToMap(int map) method? That is a place to control what map is shown.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    I have no idea. the jumpToMap(int map) method sets the value for currentMap and then I think the constructor uses hat value in showMap(int mapNo). in the constructor currentMap = mapNo

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    If you call jumpToMap() you will know what map is used.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    wahhaj is offline Member
    Join Date
    Jun 2012
    Posts
    8
    Rep Power
    0

    Default Re: getting the current map I'm on

    ok so I call jumpToMap() then what. I dont think simply calling it will do. I can do something like
    World world = getWorld().jumpToMap
    if (world == 0)
    {
    do this }
    but in this statement how do I find out what world is.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,391
    Rep Power
    25

    Default Re: getting the current map I'm on

    how do I find out what world is.
    jumpToMap sets the map. If you set it to map1 then it should be map1
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: 04-21-2011, 01:47 PM
  2. set current dir to project/bin
    By doomsword2001 in forum New To Java
    Replies: 3
    Last Post: 11-08-2010, 01:40 AM
  3. Current month
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:40 AM
  4. How can i get current directory?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •