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

2013/10/25

Worldswap with shift

1
2
danpost danpost

2013/10/25

#
OK, change 'SWorld sworld;' to 'SWorld sworld = null;'.
Baenefin Baenefin

2013/10/25

#
Ok, it complies now, except shoft does nothing now.
public void act() 
    {
        checkKeys();   
        if (getWorld() == null) return;
        checkShiftDown();
}

private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("AliceLeft.png");
            moveLeft();

        }
        if (Greenfoot.isKeyDown("right")  )
        {
            setImage("AliceRight.png");
            moveRight();

        }
        if (Greenfoot.isKeyDown("up")&& onGround )
        {
            if (onGround()) 
                jump();
        }
        if (Greenfoot.isKeyDown("space") )
        {
            //setImage("AliceShoot.png");
            //Greenfoot.playSound(".wav");
            getWorld().addObject(new Pumpkin(), getX(), getY());
        }
    }

    public void checkShiftDown()  
    {  
        if (shiftDown && !Greenfoot.isKeyDown("shift")) shiftDown = false;  
        if (shiftDown) return;  
        if (Greenfoot.isKeyDown("shift"))  
        {  
            shiftDown = true;  
            int x = ((SWorld)getWorld()).getUnivX(getX());  
            int y = ((SWorld)getWorld()).getUnivY(getY());  
            int xOffset = getX()-x, yOffset = getY()-y;  
            SWorld sworld = null;  
            if (getWorld() instanceof Level1) sworld = new Level0(this);  
            if (getWorld() instanceof Level0) sworld = new Level1(this);  
            sworld.mainActor.setLocation(sworld.mainActor.getX()+xOffset, sworld.mainActor.getY()+yOffset);  
        }  
    }  
davmac davmac

2013/10/25

#
That code does not appear to use Greenfoot.setWorld(...) at all. So it creates a new world (at line 46 or 47) but never makes it active.
danpost danpost

2013/10/25

#
Yeah, insert after line 48:
Greenfoot.setWorld(sworld);
Baenefin Baenefin

2013/10/28

#
If i change to my level 0, there are no levelblocks where they should be.
public class Level0 extends SWorld
{

    public Level0()
    {    
        this (new Alice());

    }

    public Level0(Alice alice)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 400, 1, 2400); 
        //Player
        setMainActor(new Alice(),10,340);
        mainActor.setLocation(200, 340);

        //Background
        GreenfootImage bg = new GreenfootImage("Forest.jpg");
        setScrollingBackground(bg);

        //Counter
        addObject(new Score(), 30, 10, false);
        addObject(new ShroomScore(), 130, 10, false);

        //Level blocks
        for(int i=1;i<80;i++)
        {
            addObject(new Block(), i*30-15, 384,true);
        }

        //Detector
        addObject(new LLevelDetect(), -5, 400,true);
    }
}
danpost danpost

2013/10/28

#
You appear to be placing your objects in the world with respect to the top-left corner of the window instead of with respect to the top-left corner of the scrolling area. You will need to move your main actor to the far left (and up, if you were scrolling vertically also) using 'mainActor.setLocation(mainActor.getX()-2400, mainActor.getY());' followed by 'super.act();' to set the top-left corner of the window to correspond with the top-left corner of the scolling area; then, add the other objects. Afterwords, move the main actor back to where you want it and call 'super.act();' again to reset the scrolling position to the previous state. I am inclined to update my SWorld class to have 'addObject' and 'setLocation' use scrolling coordinates instead of world coordinates. I think that might be a great improvement.
You need to login to post a reply.
1
2