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
World:
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; } } }
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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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,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 \\ \\ } } }