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

2014/5/6

Help with lag!

Daringa Daringa

2014/5/6

#
I am doing a maze game with 2 different levels but the game lags any idea on how to help with this? Moveable object
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Write a description of class Object here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Object extends Actor
{
    /**
     * Act - do whatever the Object wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage image = getWorld().getBackground();
        if(Greenfoot.isKeyDown("right") && canMove("right",image)) {
           
           move(1);
        } else if(Greenfoot.isKeyDown("left") && canMove("left",image)) {
            setRotation(180);
            move(1);
            setRotation(0);
        } else if(Greenfoot.isKeyDown("up") && canMove("up",image)) {
            setRotation(270);
            move(1);
            setRotation(0);
            
        } else if(Greenfoot.isKeyDown("down") && canMove("down",image)) {
            setRotation(90);
            move(1);
            setRotation(0);
        }
    nextLevel();
     nextLevel2();
            
    }    
     public boolean canMove(String direction, GreenfootImage background) { 
        try {
            if(direction == "up") {
                if(background.getColorAt(getX(),getY() - 2).equals(Color.BLACK) || 
                    background.getColorAt(getX() -1,getY() - 2).equals(Color.BLACK)
                    || background.getColorAt(getX() + 1,getY() - 2).equals(Color.BLACK)) {
                    return false;
                }
            } else if(direction == "down") {
                    if(background.getColorAt(getX(),getY() + 2).equals(Color.BLACK)
                    || background.getColorAt(getX() - 1,getY() + 2).equals(Color.BLACK)
                    || background.getColorAt(getX() + 1,getY() + 2).equals(Color.BLACK)) {
                        return false;
                    }
            } else if(direction == "left") {
                if(background.getColorAt(getX() - 2,getY()).equals(Color.BLACK)
                || background.getColorAt(getX() - 2,getY() + 1).equals(Color.BLACK)
                || background.getColorAt(getX() - 2,getY() - 1).equals(Color.BLACK)) {
                    return false;
                }
            } else if(direction == "right") {
                if(background.getColorAt(getX() + 2,getY()).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() - 1).equals(Color.BLACK)
                || background.getColorAt(getX() + 2,getY() + 1).equals(Color.BLACK)) {
                    return false;
                }
            }
        } catch(Exception e) {
            return false;
        }
return true;
    }

public void nextLevel()  
{  
    if (getOneObjectAtOffset(0, 0, winner.class) != null)  
    {  
        World world2 = new level2(); 
        Greenfoot.setWorld(world2);  
    } 
}
    public void nextLevel2()  
{  
    if (getOneObjectAtOffset(0, 0, winner2.class) != null)  
    {  
        World world3 = new HardStage(); 
        Greenfoot.setWorld(world3);  
    }  
}  
}
lordhershey lordhershey

2014/5/6

#
Something really strange here - why would you create a class called Object? Are you finding that only world3 is present? It looks like your Object class here may be switching your worlds at every act call.
danpost danpost

2014/5/6

#
lordhershey wrote...
It looks like your Object class here may be switching your worlds at every act call.
I do not see that as the 'nextLevel' and 'nextLevel2' place conditions on changing worlds within them. You are correct in mentioning the name of the class. Object should probably never be used as a class name, as all classes are already subclasses of the Object class (except the Object class itself). I do not get grabbing the background image every act cycle. This may be the main cause of the lag.
You need to login to post a reply.