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

2018/6/5

World array issue

Ticker175 Ticker175

2018/6/5

#
Hey guys i am remaking Pac-man in greenfoot and I'm using an array to create the actors in the world when the game starts but I'm having an issue where the actors are moving correctly but they can go half out of the screen =/ I have made all the images (for the actors) exactly 30x30 pixels and the world is 840x930 (its this size because i want it to be 28x31 tiles and i want each tile to be 30x30) anyone know how i can stop my actors going half off the screen?? This is the code I'm using: Player
    private int timer;
    public int life = 2;
    GreenfootSound Respawn = new GreenfootSound("Respawn.wav");
    GreenfootSound Fail = new GreenfootSound("Fail Sound.mp3");
    
    public void act() 
    {
        Pacman_Hitbox();
        movement();
    }
    
    public void movement()
    {
        String key = Greenfoot.getKey();
        if (key != null && key.equals("up")) jump();
        if (key != null && key.equals("down")) jumpdown();
        if (key != null && key.equals("right")) jumpright();
        if (key != null && key.equals("left")) jumpleft();
    }
    public void jump()
    {
        setLocation(getX(), getY()-30);
        setImage("PacMan.png");
    }
    public void jumpdown()
    {
        setLocation(getX(), getY()+30);
        setImage("PacMan.png");
    }
    public void jumpright()
    {
        setLocation(getX()+30, getY());
        setImage("PacMan.png");
    }
    public void jumpleft()
    {
        setLocation(getX()-30, getY());
        setImage("PacMan.png");
    }
    
    
    public void Pacman_Hitbox()
    {
        if (isTouching(Wall.class))
        {
            if (life == 0)
            {
                Fail.play();
                Greenfoot.setWorld(new Frog_World());
            }
            else
            {
                setLocation(505,805);
                Respawn.play();
                life = life - 1;
            }
        }
        else if (isTouching(Clyde.class))
        {
            if (life == 0)
            {
                Fail.play();
                Greenfoot.setWorld(new Frog_World());
            }
            else
            {
                setLocation(505,805);
                Respawn.play();
                life = life - 1;
            }
        }
    }
World:
private int[][] mazeMap = {
                              {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                              {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                              };
                              
    public Frog_World()
    {
        super(840, 930, 1);
        CreateWorld();
        setbackground();
    }
    
    public void setbackground()
    {
        setBackground(new GreenfootImage("River.png"));
    }
    
    
    public void CreateWorld()
    {
        for (int i = 0; i<mazeMap.length; i++)
        {
            for (int j = 0; j<mazeMap[i].length; j++)
            {
                if (mazeMap[i][j]==0)
                {
                    
                }
                
                if (mazeMap[i][j]==1)
                {
                    Pacman Player = new Pacman();
                    addObject(Player, j*30 + 15, i*30 + 15);
                }
                
                if (mazeMap[i][j]==2)
                {
                    Wall wall = new Wall();
                    addObject(wall, j*30, i*30);
                }
                
                
                // //Ghosts \\ \\
                if (mazeMap[i][j]==10)
                {
                    Pinky Ghost = new Pinky();
                    addObject(Ghost, j*30, i*30);
                }
                
                if (mazeMap[i][j]==11)
                {
                    Inky Ghost = new Inky();
                    addObject(Ghost, j*30, i*30);
                }
                
                if (mazeMap[i][j]==12)
                {
                    Blinky Ghost = new Blinky();
                    addObject(Ghost, j*30, i*30);
                }
                
                if (mazeMap[i][j]==13)
                {
                    Clyde Ghost = new Clyde();
                    addObject(Ghost, j*30, i*30);
                }
                // //Ghosts \\ \\
                
                
            }
        }
    }
Yehuda Yehuda

2018/6/5

#
In Greenfoot when using a bounded World (constructor with only three parameters - fourth is false), Actors are stopped from leaving the World. The position of the actor is determined by the center of the Actor's image, not the edges of it, so when the center is at the edge of the window the Actor stops. To check when an actor is at the right edge of the window you can use the following code:
if (getX() >= getWorld().getWidth() - getImage().getWidth() / 2) {
    // when the x position of the center of the Actor is greater than or equal to
    // half the width of the Actor's image subtracted from the total width of the World
}
To check when an Actor is at the top edge of the window you can use the following code:
if(getY() <= getImage().getHeight() / 2) {
    // when the y position of the center of the Actor is distanced from the top edge (0) by at most half the amount of pixels in the Actor's image
}
danpost danpost

2018/6/5

#
In each of your four jump methods, add a condition to setting a new location such that a move is made only if the resultant changed coordinate is within world bounds. For example, the jump method would be as follows:
public void jump()
{
    if (getY()-30 > 0) setLocation(getX(), getY()-30);
}
Yehuda Yehuda

2018/6/7

#
That would stick the actor to the top of the World window, is that what's wanted (I didn't go through all the code)?
danpost danpost

2018/6/7

#
Yehuda wrote...
That would stick the actor to the top of the World window, is that what's wanted (I didn't go through all the code)?
How? The code basically says "If target location is in bounds, then move there".
Yehuda Yehuda

2018/6/8

#
Your code says:
public void jump() {
    if (getY() - 30 /** if 30 pixels above the center of this Actor's image */ > 0 /** is greater than 0 meaning not on the top of the frame */)
        setLocation(getX(), getY() - 30); // set the position of the Actor to have the center of the image be 30 pixels off the top of the frame
}
I don't know what you were trying to do to show how it would be different.
danpost danpost

2018/6/8

#
Yehuda wrote...
I don't know what you were trying to do to show how it would be different.
If you think there is something wrong with the code, then you are completely mistaken.
public void jump() {
    if (getY()-30 > 0) // if 30 pixels above the center of this actor is in bounds
        setLocation(getX(), getY()-30); // move to that in bounds location
}
The top of the frame is not mentioned in the setting of the actor's location, but the actor's current y-coordinate is.
Yehuda Yehuda

2018/6/8

#
The top of the frame does not have to be mentioned by name since it's always 0.
danpost wrote...
Yehuda wrote...
I don't know what you were trying to do to show how it would be different.
Still true. I don't know what you mean by "in bounds".
danpost danpost

2018/6/8

#
Yehuda wrote...
I don't know what you mean by "in bounds".
In bounds: a valid location within the world. In this case, a valid position within the world along the corresponding horizontal or vertical axis.
Yehuda Yehuda

2018/6/11

#
So you're trying to give code to say "if the Actor is in the World (not above it) then move to a location that is 30 pixels off the top of the World".
Yehuda wrote...
That would stick the actor to the top of the World window, is that what's wanted (I didn't go through all the code)?
danpost danpost

2018/6/12

#
Yehuda wrote...
So you're trying to give code to say "if the Actor is in the World (not above it) then move to a location that is 30 pixels off the top of the World".
NO. That is not what it says. It says "If there is a cell with a positive y-coordinate 30 potential cells above the actor's current location, then move there (move 30 cells up)." Or, more literally, "if, by subtracting 30 from the actor's current y-coordinate, a positive value is maintained, then move the actor 30 cells up."
danpost danpost

2018/6/12

#
Yehuda wrote...
That would stick the actor to the top of the World window, is that what's wanted (I didn't go through all the code)?
To be fair, yes -- but, only after it gets there. It is what limits the movement of the actor to keep it within the world window. Other code allows the actor to move down, left and right (similarly).
Yehuda Yehuda

2018/6/12

#
It makes sense now but I'm not sure why it didn't before. For some reason I misinterpreted the "getY() - 30" part.
Ticker175 Ticker175

2018/6/12

#
i managed to fix it by changing the addObject parameters to +15 pixels for both the y and x cords
    public void CreateWorld()
    {
        for (int i = 0; i<mazeMap.length; i++)
        {
            for (int j = 0; j<mazeMap[i].length; j++)
            {
                if (mazeMap[i][j]==0)
                {
                    
                }
                
                if (mazeMap[i][j]==1)
                {
                    Pacman Player = new Pacman();
                    addObject(Player, j*30 + 15, i*30 + 15);
                }
                
                if (mazeMap[i][j]==2)
                {
                    Wall wall = new Wall();
                    addObject(wall, j*30 + 15, i*30 + 15);
                }
                
                if (mazeMap[i][j]==3)
                {
                    Intercection wall = new Intercection();
                    addObject(wall, j*30 + 15, i*30 + 15);
                }
You need to login to post a reply.