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

2018/3/5

Lagging after removing objects

ferN ferN

2018/3/5

#
Hello everyone, Im creating a pacman copy and everything seemed to be just fine. However, after I added the feature of pacman removing dots, the game starts to lag after pacman destroys roughly a half of the dots and when all dots are removed the lag is insane. By 'lag' I mean the loss of frames between movement (the movement isnt smooth anymore). The particular lines I added to pacman's class act(), after which lagg occured:
        if (isTouching(Dot.class)) {
            removeTouching(Dot.class);
            world.dec_dots_left();
        }
Whole pacman, MyWorld and Dot codes respectively:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class pacman here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class pacman extends Mover
{
    private MyWorld world;
    private int img_timer;
    private Direction next_direction;

    public pacman(MyWorld world) {
        this.world = world;
        curr_direction = Direction.left;
        setImage(MyWorld.pac_animations[3]);
    }

    public void act()
    {
        if (isTouching(Dot.class)) {
            removeTouching(Dot.class);
            world.dec_dots_left();
        }
        Dot dot = (Dot) getOneIntersectingObject(Dot.class);
        if (dot != null) {
            //world.dec_dots_left();
            getWorld().removeObject(dot);
        }
        Direction dir = checkInput();
        int prev_x = getX(), prev_y = getY();
        // jeigu nuspaudziam rodykle, tai issaugom, kuria kryptim norim judet
        if (dir != null && dir != curr_direction)
            next_direction = dir;
        // jei galim, tai judam
        if (next_direction != null && can_move(next_direction))
            curr_direction = next_direction;
        if (can_move(curr_direction)) {
            move(curr_direction);
            UpdateAnim(getX() - prev_x, getY() - prev_y);
        }
        if (MyWorld.map[getY()/8][getX()/8] == 4)                               // PORTAL
            setLocation(getX() == 7 ? 27*8 : 8, getY());
    }

    private Direction checkInput() {
        if (Greenfoot.isKeyDown("up"))
            return Direction.up;
        else if (Greenfoot.isKeyDown("right"))
            return Direction.right;
        else if (Greenfoot.isKeyDown("down"))
            return Direction.down;
        else if (Greenfoot.isKeyDown("left"))
            return Direction.left;
        return null;
    }

    private void UpdateAnim(int dx, int dy) {
        turnTowards(getX()+dx, getY()+dy);
        img_timer = (img_timer + 1) % 9;
        // imam modulo is 9, nes viso animacija keisim 9 kartus (ratu)
        // viso animaciju 5, tai keisim tokiu principu: 0 1 2 3 4 3 2 1 0
        setImage(MyWorld.pac_animations[4-Math.abs(4-img_timer)]);
        //setImage(MyWorld.pac_animations[Math.abs(img_timer - 2*(img_timer / 5 + img_timer % 5))]);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * @author 
 * @version
 */
public class MyWorld extends World
{
    /* FIELDS */

        /* drawings */
        public static final GreenfootImage ghost_anim[][][] = new GreenfootImage[4][4][2];      // ghost, direction, animation
        public static final GreenfootImage pac_animations[] = new GreenfootImage[5];
        public static final GreenfootImage dot_img[] = new GreenfootImage[2];
        private Color c = new Color(255,185,175);                                       // dot color
        /* movers */
        private pacman pacmanas;
        private Blinky blinky;
        private Pinky pinky;
        private Inky inky;
        private Clyde clyde;


        private int dots_left, curr_maze;
        public static final int[][] map = {
                // -1 -> base entrance, 0 -> over the map, 1 -> wall, 2-> good, 3 -> silver dots, 4 -> portal
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        { 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
        { 1, 2,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, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1},
        { 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1},
        { 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1},
        { 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1},
        { 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1},
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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}
    };
        /* METHODS */

    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        // 28*8, 36*8
        super(224, 288, 1, false);
        Greenfoot.start();
        build_maze();                                               // set background image
        draw_dots();
        add_dots();                                                 // add dots to the world
        draw_movers();                                              // fulfill ghost_anim and pac_animations arrays
        SpawnPacman();                                              // spawns pacman to the world
        SpawnGhosts();                                              // spawns ghost to the world
    }
    
    
    private void add_dots()                                        // adds dots
    {
        for (int y=0; y<map.length; y++) {
            for (int x=0; x<map[0].length; x++) {
                int m = map[y][x];
                Actor new_dot = null;
                if (m == 2)                                             // dot
                    new_dot = new Dot(false);
                else if (m == 3)
                    new_dot = new Dot(true);                        // silver dot
                if (new_dot != null) {
                    addObject(new_dot, 4 + x * 8, 4 + y * 8);
                    dots_left++;
                }
            }
        }
    }
    
    private void SpawnPacman() {
        pacmanas = new pacman(this);
        addObject(pacmanas, 14*8, 4+26*8);        // add pacman to the world
    }

    private void SpawnGhosts() {
        /*
        addObject(geist[0], 14*8, 4+14*8);
        addObject(geist[1], 14*8, 4+17*8);
        addObject(geist[2], 12*8, 4+17*8);
        addObject(geist[3], 16*8, 4+17*8); */
        draw_ghosts();
        blinky = new Blinky();
        addObject(blinky, 14*8, 4+14*8);
        pinky = new Pinky();
        addObject(pinky, 14*8, 18*8);
        inky = new Inky();
        addObject(inky, 12*8, 18*8);
        clyde = new Clyde();
        addObject(clyde, 16*8, 18*8);
    }

    private void draw_ghosts() {
        Color c = new Color(255, 255, 0);
        int k = 0;
        for (int h=0; h<ghost_anim[0][0].length; h++) for (int i=0; i<ghost_anim[0].length; i++) for (int j=0; j<ghost_anim.length; j++)
        {
            k = 0;
            ghost_anim[j][i][h] = new GreenfootImage(14,14);

            switch(j)
            {
                case 0: c = new Color(255, 0, 0); break;
                case 1: c = new Color(255, 184, 255); break;
                case 2: c = new Color(0, 255, 255); break;
                case 3: c = new Color(255, 184, 81); break;
            }
            ghost_anim[j][i][h].setColor(c);


            ghost_anim[j][i][h].drawLine(5,k,8,k); k++;
            ghost_anim[j][i][h].drawLine(3,k,10,k); k++;
            ghost_anim[j][i][h].drawLine(2,k,11,k); k++;
            ghost_anim[j][i][h].drawLine(1,k,12,k); k++;
            ghost_anim[j][i][h].drawLine(1,k,12,k); k++;
            ghost_anim[j][i][h].drawLine(1,k,12,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;
            ghost_anim[j][i][h].drawLine(0,k,13,k); k++;

            switch (h) //unten verschieden
            {
                case 0:
                    ghost_anim[j][i][h].drawLine(0,k,1,k);
                    ghost_anim[j][i][h].drawLine(3,k,5,k);
                    ghost_anim[j][i][h].drawLine(8,k,10,k);
                    ghost_anim[j][i][h].drawLine(12,k,13,k);
                    k++;
                    ghost_anim[j][i][h].drawLine(0,k,0,k);
                    ghost_anim[j][i][h].drawLine(4,k,5,k);
                    ghost_anim[j][i][h].drawLine(8,k,9,k);
                    ghost_anim[j][i][h].drawLine(13,k,13,k);
                    break;

                case 1:
                    ghost_anim[j][i][h].drawLine(0,k,3,k);
                    ghost_anim[j][i][h].drawLine(5,k,8,k);
                    ghost_anim[j][i][h].drawLine(10,k,13,k);
                    k++;
                    ghost_anim[j][i][h].drawLine(1,k,2,k);
                    ghost_anim[j][i][h].drawLine(6,k,7,k);
                    ghost_anim[j][i][h].drawLine(11,k,12,k);
                    break;
            }

            ghost_anim[j][i][h].setColor(new Color(222, 222, 255)); //weiß
            switch (i)
            {
                case 1:
                    k = 3;
                    ghost_anim[j][i][h].drawLine(4,k,5,k); ghost_anim[j][i][h].drawLine(10,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,6,k); ghost_anim[j][i][h].drawLine(9,k,12,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,6,k); ghost_anim[j][i][h].drawLine(9,k,12,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,6,k); ghost_anim[j][i][h].drawLine(9,k,12,k); k++;
                    ghost_anim[j][i][h].drawLine(4,k,5,k); ghost_anim[j][i][h].drawLine(10,k,11,k);
                    ghost_anim[j][i][h].setColor(new Color(33, 33, 255)); //blau
                    k=5;
                    ghost_anim[j][i][h].drawLine(5,k,6,k); ghost_anim[j][i][h].drawLine(11,k,12,k); k++;
                    ghost_anim[j][i][h].drawLine(5,k,6,k); ghost_anim[j][i][h].drawLine(11,k,12,k);
                    break;


                case 2:
                    k = 4;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k);
                    ghost_anim[j][i][h].setColor(new Color(33, 33, 255)); //blau
                    k = 7;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k);
                    break;


                case 3:
                    k = 3;
                    ghost_anim[j][i][h].drawLine(2,k,3,k); ghost_anim[j][i][h].drawLine(8,k,9,k); k++;
                    ghost_anim[j][i][h].drawLine(1,k,4,k); ghost_anim[j][i][h].drawLine(7,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(1,k,4,k); ghost_anim[j][i][h].drawLine(7,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(1,k,4,k); ghost_anim[j][i][h].drawLine(7,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,3,k); ghost_anim[j][i][h].drawLine(8,k,9,k); k++;
                    ghost_anim[j][i][h].setColor(new Color(33, 33, 255)); //blau
                    k = 5;
                    ghost_anim[j][i][h].drawLine(1,k,2,k); ghost_anim[j][i][h].drawLine(7,k,8,k); k++;
                    ghost_anim[j][i][h].drawLine(1,k,2,k); ghost_anim[j][i][h].drawLine(7,k,8,k);
                    break;


                case 0:
                    k = 1;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(2,k,5,k); ghost_anim[j][i][h].drawLine(8,k,11,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k);
                    ghost_anim[j][i][h].setColor(new Color(33, 33, 255)); //blau
                    k = 1;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k); k++;
                    ghost_anim[j][i][h].drawLine(3,k,4,k); ghost_anim[j][i][h].drawLine(9,k,10,k);
                    break;
            }
        }
    }

    private void draw_pacman()                                                               // fulfill pac_animations[]
    {
        Color c = new Color(255, 255, 0);
        int k=0;

        for (int i=0; i<pac_animations.length; i++)
        {
            pac_animations[i] = new GreenfootImage(13,13);
            pac_animations[i].setColor(c);

            pac_animations[i].drawLine(5,0,7,0);
            pac_animations[i].drawLine(3,1,9,1);
            pac_animations[i].drawLine(3,11,9,11);
            pac_animations[i].drawLine(5,12,7,12);


            switch (i)
            {
                case 0:
                    pac_animations[i].drawLine(0,5,0,7);
                    pac_animations[i].drawLine(1,3,1,9);
                    pac_animations[i].drawLine(11,3,11,9);
                    pac_animations[i].drawLine(12,5,12,7);
                    pac_animations[i].fillRect(2,2,9,9);
                    break;

                case 1:
                    k=2;
                    pac_animations[i].drawLine(2,k,10,k); k++;
                    pac_animations[i].drawLine(1,k,11,k); k++;
                    pac_animations[i].drawLine(1,k,11,k); k++;
                    pac_animations[i].drawLine(0,k,9,k); k++;
                    pac_animations[i].drawLine(0,k,7,k); k++;
                    pac_animations[i].drawLine(0,k,9,k); k++;
                    pac_animations[i].drawLine(1,k,11,k); k++;
                    pac_animations[i].drawLine(1,k,11,k); k++;
                    pac_animations[i].drawLine(2,k,10,k);
                    break;

                case 2:
                    k=2;
                    pac_animations[i].drawLine(2,k,10,k); k++;
                    pac_animations[i].drawLine(1,k,10,k); k++;
                    pac_animations[i].drawLine(1,k,9,k); k++;
                    pac_animations[i].drawLine(0,k,7,k); k++;
                    pac_animations[i].drawLine(0,k,6,k); k++;
                    pac_animations[i].drawLine(0,k,7,k); k++;
                    pac_animations[i].drawLine(1,k,9,k); k++;
                    pac_animations[i].drawLine(1,k,10,k); k++;
                    pac_animations[i].drawLine(2,k,10,k);
                    break;

                case 3:
                    k=2;
                    pac_animations[i].drawLine(2,k,10,k); k++;
                    pac_animations[i].drawLine(1,k,9,k); k++;
                    pac_animations[i].drawLine(1,k,7,k); k++;
                    pac_animations[i].drawLine(0,k,5,k); k++;
                    pac_animations[i].drawLine(0,k,4,k); k++;
                    pac_animations[i].drawLine(0,k,5,k); k++;
                    pac_animations[i].drawLine(1,k,7,k); k++;
                    pac_animations[i].drawLine(1,k,9,k); k++;
                    pac_animations[i].drawLine(2,k,10,k);
                    break;

                case 4:
                    k=2;
                    pac_animations[i].drawLine(2,k,8,k); k++;
                    pac_animations[i].drawLine(1,k,7,k); k++;
                    pac_animations[i].drawLine(1,k,6,k); k++;
                    pac_animations[i].drawLine(0,k,5,k); k++;
                    pac_animations[i].drawLine(0,k,4,k); k++;
                    pac_animations[i].drawLine(0,k,5,k); k++;
                    pac_animations[i].drawLine(1,k,6,k); k++;
                    pac_animations[i].drawLine(1,k,7,k); k++;
                    pac_animations[i].drawLine(2,k,8,k);
                    break;
            }
        }
    }

    private void draw_movers() {
        draw_pacman();
        draw_ghosts();
    }
    
    private void draw_dots() {
        int z = 0;
        // casual dot
        dot_img[0] = new GreenfootImage(2, 2);
        dot_img[0].setColor(c);
        dot_img[0].fill();
        // silver dot
        dot_img[1] = new GreenfootImage(8, 8);
            f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); z++;
            f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); z++;
            f(dot_img[1],0,z); f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); f(dot_img[1],7,z); z++;
            f(dot_img[1],0,z); f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); f(dot_img[1],7,z); z++;
            f(dot_img[1],0,z); f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); f(dot_img[1],7,z); z++;
            f(dot_img[1],0,z); f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); f(dot_img[1],7,z); z++;
            f(dot_img[1],1,z); f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); f(dot_img[1],6,z); z++;
            f(dot_img[1],2,z); f(dot_img[1],3,z); f(dot_img[1],4,z); f(dot_img[1],5,z); z++;
    }

    private void f(GreenfootImage img, int x, int y) {img.setColorAt(x,y,c);}

    private void build_maze() {
        setBackground(Integer.toString(curr_maze) + ".png");
        curr_maze++;
    }


    public void dec_dots_left() {                                   // decrement dots_left, because a dot has been eaten
        if (--dots_left == 0)
            level_finished();
    }

    private void level_finished() {
        return;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Dot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Dot extends Actor
{
        private boolean silver = false;
        
        public Dot(boolean silver) {
            this.silver = silver;
            setImage(MyWorld.dot_img[silver ? 1 : 0]);
    }
}
ferN ferN

2018/3/5

#
In fact, I just wrote a quick function, that removes ALL dots immediately if pacman enters the portal:
for (Dot d : getObjects(Dot.class))
removeObject(d);
and as soon as pacman enters the portal, all dots disappear and there's a HUGE lag
danpost danpost

2018/3/5

#
I do not see the need for the 'dots_left' field. You can check with:
int dots_left = getObjects(Dot.class).size();
This means you can remove the 'dec_dots_left' method from the world. Also, you are trying to remove dots twice in the 'act' method of Pacman -- the segment from 23 to 26 and the segment from 27 to 31. The following should be sufficient to replace lines 23 through 31:
if (isTouching(Dot.class))
{
    removeTouching(Dot.class);
    MyWorld mw = (MyWorld)getWorld();
    if (mw.getObjects(Dot.class).isEmpty())
    {
        mw.level_finished();
        return;
    }
}
I am not saying this will fix your lag issue. Just anything you can eliminate as a possibility for the lag will narrow down the search. I think providing the Mover class might help since Pacman is dependent on it also. It might be best for you to share your project here in the gallery (with source provided) so that we can actually test it to (hopefully) determine why it is lagging.
ferN ferN

2018/3/5

#
Thanks for the answer, I'll look deeper into it, here's the mover code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mover extends Actor
{
    protected enum Direction {up, right, down, left};
    protected Direction curr_direction;                     // initialization is left to sub classes
    private int dx, dy;

    public void move(Direction dir) {
        if (dir == null)
            return;
        curr_direction = dir;
        update_coord_fields(dir);
        setLocation(getX()+dx, getY()+dy);
    }

    public boolean can_move(Direction dir) {
        int tmp, next_x, next_y, pixels_wall;           // pixels until a wall
        update_coord_fields(dir);
        pixels_wall = minusas(dir) ? 5 : 4;
        next_y = (dy * pixels_wall + getY()) / 8;             // 4 pixeliai iki sienos pvz. siena 32, tai 36 jau stabdys, jei judam i virsu(y mazeja)
        next_x = (dx * pixels_wall + getX()) / 8;
        next_y %= MyWorld.map.length;
        next_x %= MyWorld.map[0].length;
        tmp = MyWorld.map[next_y][next_x];
        if (tmp == 1 || tmp == -1)
            return false;
        if (dir != curr_direction && Math.abs(dir.ordinal() - curr_direction.ordinal()) != 2  && !can_turn())
            return false;
        return true;
    }

    private boolean can_turn() {
        return ((getX() + 4) % 8 == 0) && ((getY() + 4) % 8 == 0);
    }

    private void update_coord_fields(Direction dir) {
        dx = 0;
        dy = 0;
        if (dir == Direction.up)
            dy--;
        if (dir == Direction.right)
            dx++;
        if(dir == Direction.down)
            dy++;
        if (dir == Direction.left)
            dx--;
    }

    private Boolean minusas(Direction dir) {                    // whether we decrement dx or dy
        return dir == Direction.up || dir == Direction.left;
    }
}
ferN ferN

2018/3/5

#
http://www.greenfoot.org/scenarios/20955 here we go edit: that's kinda awkward, since it doesn't lag on the web, only on my arch linux
danpost danpost

2018/3/5

#
In line 56 of the Mover class, change 'Boolean' to 'boolean' (all lower case). A primitive is always preferred to an Object type when it is not needed to be an object for some reason. The 'MyWorld world' field in the Pacman class (declared at line 11) does not seem necessary. You can use:
MyWorld world = (MyWorld)getWorld();
at any time it is needed. So, you can remove lines 11 and 16 and remove "MyWorld world" from line 15; and in MyWorld, remove "this" from line 100. I do not see why you have all the images in your world class. It would seem logical to the images in their respective classes.
danpost danpost

2018/3/5

#
ferN wrote...
edit: that's kinda awkward, since it doesn't lag on the web, only on my arch linux
Yes -- awkward. I ran in Windows-7. I ate all dots with no decrease in speed (no lagging).
ferN ferN

2018/3/5

#
Thank you for taking your time, I'll take your suggestions into account and update the scenario as I finish it.
You need to login to post a reply.