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

2018/2/7

Play charater idle animation if there aren't any keys pressed

1
2
3
4
5
6
LaurentiuRohan LaurentiuRohan

2018/2/15

#
danpost wrote...
First of all, no changes are meant to be done in the SWorld class (but, I guess if you only changed what was printed to the terminal, it should not mess things up any). Secondly, the SWorld class has a 'setMainActor(Actor actor)' method which you need in your Scrolling_World class (as well as the 'Actor mainActor' field, if not already there). Btw, the SWorld class, as is, was meant to be re-usable support class. That is, you can add it to any scenario and subclass it with a world that you want scrolling implemented in. However, it can be difficult to work with and may not be compatible with all types of scrolling. I, more recently, wrote aScroller class. which is a support class that is easy to work with and does all types of scrolling. It is well documented and has a tutorial to go along with it.
It looks like I've forgot 2 "))" and ; and I spent 2 hours asking why doesen't works after tried 3 differemt methods. Anyway, thanks a lot for your help. One more question if can you answer please. I did't understood how how that scrooling baclground realy works, could you explain a little bit please, the problem is that I have a 960 X 580 background image, but when the world load it's wider and higher too, so I tried to change the numbers on this 2 voids, but still doesen't works. How can I make it scrollable at the sime size?
LaurentiuRohan LaurentiuRohan

2018/2/15

#
public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide, int scrollHigh)
    {
        super(cellSize==1?wide:(wide/2)*2+1, cellSize==1?high:(high/2)*2+1, cellSize, false);
        scrollType=(scrollWide>wide?1:0)+(scrollHigh>high?2:0);
        scrollingWidth=scrollType%2==1?scrollWide:wide;
        scrollingHeight=scrollType/2==1?scrollHigh:high;
    }

public void setScrollingBackground(GreenfootImage scrollingBackground)    
    {
        if(mainActor==null)
        {
            System.out.println("'setMainActor' MUST be called prior to calling 'setScrollingBackground1.p'.");
            System.out.println("");
            return;
        }
        background = new GreenfootImage(scrollingBackground);
        background.scale(scrollingWidth*getCellSize(), scrollingHeight*getCellSize());
        scrollBackground();
    }
danpost danpost

2018/2/16

#
So you are still using the old SWorld codes. In the constructor, 'wide' and 'high' are the dimensions of the world viewport (what the "camera" sees) and 'scrollWide' and 'ScrollHigh' are the dimensions of your total scrolling area. The 'setScrollingBackground' method scales any image given it to the size of the total scrolling area. If you do not want the image scaled, make 'scrollWide' and 'scrollHigh' equal to the saved dimensions of the desired background image.
LaurentiuRohan LaurentiuRohan

2018/2/16

#
danpost wrote...
So you are still using the old SWorld codes. In the constructor, 'wide' and 'high' are the dimensions of the world viewport (what the "camera" sees) and 'scrollWide' and 'ScrollHigh' are the dimensions of your total scrolling area. The 'setScrollingBackground' method scales any image given it to the size of the total scrolling area. If you do not want the image scaled, make 'scrollWide' and 'scrollHigh' equal to the saved dimensions of the desired background image.
So I've tried in public Scroling_Worlds to change wide and high but doesn't works, how should i put my background dimensions?
public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide, int scrollHigh)
    {
        super(cellSize==1?wide:(960), cellSize==1?high:(580), cellSize, false);
        scrollType=(scrollWide>wide?1:0)+(scrollHigh>high?2:0);
        scrollingWidth=scrollType%2==1?scrollWide:wide;
        scrollingHeight=scrollType/2==1?scrollHigh:high;
        
    }
Super_Hippo Super_Hippo

2018/2/16

#
Show how you create your world.
LaurentiuRohan LaurentiuRohan

2018/2/16

#
Super_Hippo wrote...
Show how you create your world.
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Scrooling_Worlds here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scrooling_Worlds extends World
{
     
    /**
     * Constructor for objects of class Scrooling_Worlds.
     * 
     */
    private int scrollingWidth, scrollingHeight; 
    private int actorMinX, actorMaxX, actorMinY, actorMaxY; 
    private int scrolledX, scrolledY; 
    private int scrollType; 
    Actor mainActor = null; 
    private List<Actor>genActors=new ArrayList(); 
    private GreenfootImage background = null;
    static int ammunition = 0; 
    static int lives = 3; 
    private ScoreBoard scoreBoard;
    
     public int getAmmo()
    {
        return ammunition;
    }
    
    public void resetAmmo()
    {
        ammunition = 0;
    }
    
    public void increaseAmmunition()
    {
        ammunition = ammunition + 3;
    }
    
    public void decreaseAmmunition()
    {
        ammunition = ammunition - 1;
    }
    
    public void increaseLives()
    {
        lives = lives + 1;
    }
    
    public void decreaseLives()
    {
        lives = lives - 1;
    }
    
    public int getLives()
    {
        return lives;
    }
    
    public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide, int scrollHigh)
    {
        super(cellSize==1?wide:(960), cellSize==1?high:(580), cellSize, false);
        scrollType=(scrollWide>wide?1:0)+(scrollHigh>high?2:0);
        scrollingWidth=scrollType%2==1?scrollWide:wide;
        scrollingHeight=scrollType/2==1?scrollHigh:high;
        
    }
    
    public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide)
    {
        this(wide, high, cellSize, scrollWide, high);
    }
    
    public void setMainActor(Actor main, int xRange, int yRange)
    {
        if (main==null)
        {
            System.out.println("A main actor MUST be supplied.");
            System.out.println("");
            return;
        }
        super.addObject(main, getWidth()/2, getHeight()/2);
        mainActor = main;
        xRange=(int)Math.min(xRange, getWidth());
        yRange=(int)Math.min(yRange, getHeight());
        actorMinX=getWidth()/2-xRange/2;
        actorMaxX=getWidth()/2+xRange/2;
        actorMinY=getHeight()/2-yRange/2;
        actorMaxY=getHeight()/2+yRange/2;
    }
    
    public void setScrollingBackground(GreenfootImage scrollingBackground)    
    {
        if(mainActor==null)
        {
            System.out.println("'setMainActor' MUST be called prior to calling 'setScrollingBackground1.p'.");
            System.out.println("");
            return;
        }
        background = new GreenfootImage(scrollingBackground);
        background.scale(scrollingWidth*getCellSize(), scrollingHeight*getCellSize());
        scrollBackground();
    }
    
    
    
    public void addObject(Actor obj, int xLoc, int yLoc, boolean scroller)
    {
        super.addObject(obj, xLoc, yLoc);
        if(scroller) genActors.add(obj);
    }
    
    public void addObject(Actor obj, int xLoc, int yLoc)
    {
        addObject(obj, xLoc, yLoc, true);
    }
    
    public void removeObject(Actor obj)
    {
        if(obj==null)return;
        if(obj.equals(mainActor))mainActor=null;
        else genActors.remove(obj);
        super.removeObject(obj);
    }
    
    public void removeObjects(List<Actor>objs)
    {
        for(Actor obj:objs)removeObject(obj);
    }
    
    public void act()
    {
        scrollObjects();
        scrollBackground();
        
        
    }
    
    private void scrollBackground()
    {
        if (background==null) return;
        int w = getWidth(), h = getHeight(), c = getCellSize();
        int bw = background.getWidth(), bh = background.getHeight();
        getBackground().drawImage(background, (w*c-bw)/2-scrolledX*c, (h*c-bh)/2-scrolledY*c);
    }
    
    private void scrollObjects()
    {
        if (mainActor==null) return;
        // determine how far the main actor is outside its standard window limits
        int dx=0, dy=0;
        if(mainActor.getX()<actorMinX) dx=actorMinX-mainActor.getX();
        if(mainActor.getX()>actorMaxX) dx=actorMaxX-mainActor.getX();
        if(mainActor.getY()<actorMinY) dy=actorMinY-mainActor.getY();
        if(mainActor.getY()>actorMaxY) dy=actorMaxY-mainActor.getY();
        if(dx==0 && dy==0) return; // not outside window limits
        // ** outside standard window limits **
        int dxSum = dx, dySum = dy; // hold changes in scroll amount
        scrolledX-=dx; scrolledY-=dy;// track scroll amount
        // move main actor back within standard window limits
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
        // determine how far the background is inside the world limits
        dx=0; dy=0;
        if(scrolledX > scrollingWidth/2-getWidth()/2) dx=scrolledX-(scrollingWidth/2-getWidth()/2);
        if(scrolledX < getWidth()/2-scrollingWidth/2) dx=scrolledX-(getWidth()/2-scrollingWidth/2);
        if(scrolledY > scrollingHeight/2-getHeight()/2) dy=scrolledY-(scrollingHeight/2-getHeight()/2);
        if(scrolledY < getHeight()/2-scrollingHeight/2) dy=scrolledY-(getHeight()/2-scrollingHeight/2);
        // ** background does not completely cover world limits
        dxSum+=dx; dySum+=dy; // keep running sum of changes in scroll amount
        scrolledX-=dx; scrolledY-=dy; // adjust scroll amount
        // move all objects so background covers the world
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
        for(Object obj : genActors)
        {
            Actor actor=(Actor)obj;
            actor.setLocation(actor.getX()+dxSum, actor.getY()+dySum);
        }
        // determine how far main actor is outside universal limits
        dx=0; dy=0;
        if(mainActor.getX() < 0) dx=0-mainActor.getX();
        if(mainActor.getX() > getWidth()-1) dx=(getWidth()-1)-mainActor.getX();
        if(mainActor.getY() < 0) dy=0-mainActor.getY();
        if(mainActor.getY() > getHeight()-1) dy=(getHeight()-1)-mainActor.getY();
        if(dx==0 && dy==0) return;
        // ** outside universal limits
        // move main actor back within world limits
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
    }
    
    public int getScrolledX()
    {
        return scrolledX;
    }
    
    public int getScrolledY()
    {
        return scrolledY;
    }
    
    public int getScrollingWidth()
    {
        return scrollingWidth;
    }
    
    public int getScrollingHeight()
    {
        return scrollingHeight;
    }
 
    
}
danpost danpost

2018/2/16

#
Again, the SWorld class was not meant to be modified. Also, you need to show the class that extends the one given.
LaurentiuRohan LaurentiuRohan

2018/2/16

#
danpost wrote...
Again, the SWorld class was not meant to be modified. Also, you need to show the class that extends the one given.
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mace_Level here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mace_Level extends Scrooling_Worlds
{
    private ScoreBoard scoreBoard;
    static GreenfootSound theme = new GreenfootSound("Intro Theme Menu.mp3");
    static GreenfootSound start = new GreenfootSound("Spawn Sound Effect.mp3");
    /**
     * Constructor for objects of class Mace_Level.
     * 
     */
    public Mace_Level()
    {
        super(960, 580, 1, 9600);
        setMainActor(new Puppy_Player(), 100, 342); // the int parameters are centered window x and y ranges
        // to start the main actor elsewhere
        mainActor.setLocation(100, 342);
        GreenfootImage bg = new GreenfootImage("Background2.png");
        setScrollingBackground(bg); // set the scolling background image

        lives = 3;
        ammunition = 0;
        if (!theme.isPlaying())
        {
            theme.playLoop();
        }
        buildWorld();
        if(!start.isPlaying())
        {
            start.play();
        }
        
        addObject(new Grass_Normal_Center(), 0, 554);
    }
    


    public void rebuildWorld()
    {
        if (lives < 0)
        {
            stopMusic();
            Greenfoot.setWorld(new GameOver_World());
        }
        ammunition = 0;
        List objects = getObjects(null);
        removeObjects(objects);
        buildWorld();
    }
    
    public void stopMusic()
    {
        theme.stop();
    }
    
    public void updateScoreboard()
    {
        scoreBoard.updateScore(lives, ammunition);
    }
    
    private void addScoreboard()
    {
        scoreBoard = new ScoreBoard();
        addObject(scoreBoard, 30, 15, false);
        scoreBoard.updateScore(lives, ammunition);
    }
    
    private void buildWorld()
    {
        
        StartingArea();
        Area1();
        Area2();
        Area3();
        Area4();
        Area5();
        Area6();
        Area7();
        Area8();
        Area9();
        Area10();
        
        
    }
    
    private void StartingArea()
    {
        addObject(new Grass_Normal_Center(), 49, 554);
        addObject(new Grass_Normal_Center(), 329, 554);
    }
    
    private void Area1()
    {
    }
    
    private void Area2()
    {
    }
    
    private void Area3()
    {
    }
    
    private void Area4()
    {
    }
    private void Area5()
    {
    }
    private void Area6()
    {
    }
    private void Area7()
    {
    }
    private void Area8()
    {
    }
    private void Area9()
    {
    }
    private void Area10()
    {
    }
    
    
    
}
danpost danpost

2018/2/16

#
Okay. So you are using the side-scrolling constructor. The height of the scrolling background image will be scaled to fit the height of the world view port and the width of the scrolling background image will be scaled to the width of the scrolling area.
LaurentiuRohan LaurentiuRohan

2018/2/16

#
danpost wrote...
Okay. So you are using the side-scrolling constructor. The height of the scrolling background image will be scaled to fit the height of the world view port and the width of the scrolling background image will be scaled to the width of the scrolling area.
So I have to make a 9600px background image, right?
danpost danpost

2018/2/16

#
LaurentiuRohan wrote...
So I have to make a 9600px background image, right?
... if that is the size at which you want your scrolling width. That would be a huge image and probably cause problems (heap space/memory/lagging). I suggest you use my Scroller class, instead of the SWorld class, so that the image can be wrapped for you.
LaurentiuRohan LaurentiuRohan

2018/2/16

#
danpost wrote...
LaurentiuRohan wrote...
So I have to make a 9600px background image, right?
... if that is the size at which you want your scrolling width. That would be a huge image and probably cause problems (heap space/memory/lagging). I suggest you use my Scroller class, instead of the SWorld class, so that the image can be wrapped for you.
Yes I will use your class , thanks in advance. Now I have one more problem. When I add an object into the Mace_Level world, the x coordonates aren't respected. For example I've added that Grass_Normal_Center object at X:49 and Y:554, the object goes to Y position but the X position is 334. Why is that happening?
danpost danpost

2018/2/16

#
LaurentiuRohan wrote...
When I add an object into the Mace_Level world, the x coordonates aren't respected. For example I've added that Grass_Normal_Center object at X:49 and Y:554, the object goes to Y position but the X position is 334. Why is that happening?
It probably has to do with the world scrolling. Like I said, the SWorld class is difficult to work with.
LaurentiuRohan LaurentiuRohan

2018/2/18

#
danpost wrote...
LaurentiuRohan wrote...
When I add an object into the Mace_Level world, the x coordonates aren't respected. For example I've added that Grass_Normal_Center object at X:49 and Y:554, the object goes to Y position but the X position is 334. Why is that happening?
It probably has to do with the world scrolling. Like I said, the SWorld class is difficult to work with.
So I solved the problem with scrooling objects and background I found a solution helping with your Scroller class, but now another problem appeared. When every world start's it has to spawn the Puppy_Player (main actor) he is spawning, but the problem is that he goes behind every object being on the world. Even when on the ground, I maded Puppy to stay on ground if he is with -2 more to the y axis, not at the very top of the ground , so when he is on ground you can't see a little bit of his body because he is behind the ground, same happening to the other objects. If he have the same position with other object wich are bigger than puppy he is behind them. How could I solve this, could you please analyze my code? If it's okay I could show the code for player and both worlds. First is the code for Scooling_Worlds, second for the first level that extends Scrooling_Worlds and third for the player. I've tried too to add "setPaintOrder(Puppy_Player.class);" in both worlds after super call, but doesen't works..:
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Scrooling_Worlds here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scrooling_Worlds extends World
{
     
    /**
     * Constructor for objects of class Scrooling_Worlds.
     * 
     */
    private int scrollingWidth, scrollingHeight; 
    private int actorMinX, actorMaxX, actorMinY, actorMaxY; 
    private int scrolledX, scrolledY; 
    private int scrollType; 
    Actor mainActor = null; 
    private List<Actor>genActors=new ArrayList(); 
    private GreenfootImage background = null;
    static int ammunition = 0; 
    static int lives = 3; 
    private ScoreBoard scoreBoard;
    
     public int getAmmo()
    {
        return ammunition;
    }
    
    public void resetAmmo()
    {
        ammunition = 0;
    }
    
    public void increaseAmmunition()
    {
        ammunition = ammunition + 3;
    }
    
    public void decreaseAmmunition()
    {
        ammunition = ammunition - 1;
    }
    
    public void increaseLives()
    {
        lives = lives + 1;
    }
    
    public void decreaseLives()
    {
        lives = lives - 1;
    }
    
    public int getLives()
    {
        return lives;
    }
    
    public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide, int scrollHigh)
    {
        super(cellSize==1?wide:(wide/2)*2+1, cellSize==1?high:(high/2)*2+1, cellSize, false);
        scrollType=(scrollWide>wide?1:0)+(scrollHigh>high?2:0);
        scrollingWidth=scrollType%2==1?scrollWide:wide;
        scrollingHeight=scrollType/2==1?scrollHigh:high;
    }
    
    public Scrooling_Worlds(int wide, int high, int cellSize, int scrollWide)
    {
        this(wide, high, cellSize, scrollWide, high);
    }
    
    public void setMainActor(Actor main, int xRange, int yRange)
    {
        if (main==null)
        {
            System.out.println("A main actor MUST be supplied.");
            System.out.println("");
            return;
        }
        super.addObject(main, getWidth()/2, getHeight()/2);
        mainActor = main;
        xRange=(int)Math.min(xRange, getWidth());
        yRange=(int)Math.min(yRange, getHeight());
        actorMinX=getWidth()/2-xRange/2;
        actorMaxX=getWidth()/2+xRange/2;
        actorMinY=getHeight()/2-yRange/2;
        actorMaxY=getHeight()/2+yRange/2;
    }
    
    public void setScrollingBackground(GreenfootImage scrollingBackground)    
    {
        if(mainActor==null)
        {
            System.out.println("'setMainActor' MUST be called prior to calling 'setScrollingBackground1.p'.");
            System.out.println("");
            return;
        }
        background = new GreenfootImage(scrollingBackground);
        background.scale(scrollingWidth*getCellSize(), scrollingHeight*getCellSize());
        scrollBackground();
    }
    
    
    
    public void addObject(Actor obj, int xLoc, int yLoc, boolean scroller)
    {
        super.addObject(obj, xLoc, yLoc);
        if(scroller) genActors.add(obj);
    }
    
    public void addObject(Actor obj, int xLoc, int yLoc)
    {
        addObject(obj, xLoc, yLoc, true);
    }
    
    public void removeObject(Actor obj)
    {
        if(obj==null)return;
        if(obj.equals(mainActor))mainActor=null;
        else genActors.remove(obj);
        super.removeObject(obj);
    }
    
    public void removeObjects(List<Actor>objs)
    {
        for(Actor obj:objs)removeObject(obj);
    }
    
    public void act()
    {
        scrollObjects();
        scrollBackground();
    }
    
    private void scrollBackground()
    {
        if (background==null) return;
        int w = getWidth(), h = getHeight(), c = getCellSize();
        int bw = background.getWidth(), bh = background.getHeight();
        getBackground().drawImage(background, (w*c-bw)/2-scrolledX*c, (h*c-bh)/2-scrolledY*c);
    }
    
    private void scrollObjects()
    {
        if (mainActor==null) 
        {
            return;
        }
        int dx=0, dy=0;
        if(mainActor.getX()<actorMinX) 
        {
            dx=actorMinX-mainActor.getX();
        }
        if(mainActor.getX()>actorMaxX) 
        {
            dx=actorMaxX-mainActor.getX();
        }
        if(mainActor.getY()<actorMinY) 
        {
            dy=actorMinY-mainActor.getY();
        }
        if(mainActor.getY()>actorMaxY) 
        {
            dy=actorMaxY-mainActor.getY();
        }
        if(dx==0 && dy==0) 
        {
            return;
        }
        int dxSum = dx, dySum = dy;
        scrolledX-=dx; scrolledY-=dy;
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
        dx=0; dy=0;
        if(scrolledX > scrollingWidth-getWidth())
        {
            dx=scrolledX-(scrollingWidth-getWidth());
        }
        if(scrolledX < 0) 
        {
            dx=scrolledX;
        }
        if(scrolledY > scrollingHeight-getHeight()) 
        {
            dy=scrolledY-(scrollingHeight-getHeight());
        }
        if(scrolledY < 0) 
        {
            dy=scrolledY;
        }
        dxSum+=dx; dySum+=dy;
        scrolledX-=dx; scrolledY-=dy;
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
        for(Object obj : genActors)
        {
            Actor actor=(Actor)obj;
            actor.setLocation(actor.getX()+dxSum, actor.getY()+dySum);
        }
        dx=0; dy=0;
        if(mainActor.getX() < 0) 
        {
            dx=0-mainActor.getX();
        }
        if(mainActor.getX() > getWidth()-1) 
        {
            dx=(getWidth()-1)-mainActor.getX();
        }
        if(mainActor.getY() < 0) 
        {
            dy=0-mainActor.getY();
        }
        if(mainActor.getY() > getHeight()-1) 
        {
            dy=(getHeight()-1)-mainActor.getY();
        }
        if(dx==0 && dy==0) 
        {
            return;
        }
        mainActor.setLocation(mainActor.getX()+dx, mainActor.getY()+dy);
    }
    
    public int getScrolledX()
    {
        return scrolledX;
    }
    
    public int getScrolledY()
    {
        return scrolledY;
    }
    
    public int getScrollingWidth()
    {
        return scrollingWidth;
    }
    
    public int getScrollingHeight()
    {
        return scrollingHeight;
    }
}
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mace_Level here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mace_Level extends Scrooling_Worlds
{
    private ScoreBoard scoreBoard;
    static GreenfootSound theme = new GreenfootSound("Intro Theme Menu.mp3");
    static GreenfootSound start = new GreenfootSound("Spawn Sound Effect.mp3");
    /**
     * Constructor for objects of class Mace_Level.
     * 
     */
    public Mace_Level()
    {
        super(960, 580, 1, 9600);
setPaintOrder(Puppy_Player.class);
        setMainActor(new Puppy_Player(), 100, 342); // the int parameters are centered window x and y ranges
        // to start the main actor elsewhere
        mainActor.setLocation(100, 342);
        GreenfootImage bg = new GreenfootImage("Background1.png");
        setScrollingBackground(bg); // set the scolling background image

        lives = 3;
        ammunition = 0;
        if (!theme.isPlaying())
        {
            theme.playLoop();
        }
        buildWorld();
        if(!start.isPlaying())
        {
            start.play();
        }
        
        
    }
    


    public void rebuildWorld()
    {
        if (lives < 0)
        {
            stopMusic();
            Greenfoot.setWorld(new GameOver_World());
        }
        ammunition = 0;
        List objects = getObjects(null);
        removeObjects(objects);
        buildWorld();
    }
    
    public void stopMusic()
    {
        theme.stop();
    }
    
    public void updateScoreboard()
    {
        scoreBoard.updateScore(lives, ammunition);
    }
    
    private void addScoreboard()
    {
        scoreBoard = new ScoreBoard();
        addObject(scoreBoard, 30, 15, false);
        scoreBoard.updateScore(lives, ammunition);
    }
    
    private void buildWorld()
    {
        addObject(new Grass_Normal_Left(), 29, 545, true);
        addObject(new Grass_Normal_Center(), 89, 545, true);
        addObject(new Grass_Normal_Center(), 269, 545, true);
        addObject(new Grass_Normal_Center(), 380, 545, true);
        addObject(new Grass_Normal_Center(), 200, 390);
   
    }

}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Puppy_Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Puppy_Player extends Actor
{
     private int vSpeed = 7;
    private int acceleration = 2;
    private int jumpStrenght = 20;
    public boolean jumping;
    private int speed = 4;
    private boolean onGrounds; 
    private int frame = 1;
    
    /**
     * Act - do whatever the Doors_Level_Select wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
       checkKey();
       checkFall();
   
       GreenfootImage[] inAnimation = currentAnimation;
       int atFrame = frame;
       int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (dx  == 0) // idle
         {
        if (idleLeftAnimation != currentAnimation && idleRightAnimation != currentAnimation)
            {
          if (runningRightAnimation == currentAnimation || jumpingRightAnimation == currentAnimation)
             currentAnimation = idleRightAnimation;
          else
             currentAnimation = idleLeftAnimation;
             frame = 0;
            }
        }
        else if (dx == 1 && currentAnimation != runningRightAnimation)
         {
            currentAnimation = runningRightAnimation;
            frame = 0;
         }
        else if (dx == -1 && currentAnimation != runningLeftAnimation)
         {
            currentAnimation = runningLeftAnimation;
            frame = 0;
         }
         
         // run animation
         setImage(currentAnimation[frame]);
         frame = (frame+1)%currentAnimation.length;
        
    }   

  public void checkFall()
  {
    if(onGround()) 
     {
        vSpeed = 0;
     } 
    else 
     {
        fall();
     } 
    }
    
  public boolean onGround()
   {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight() /2  ,Grounds.class);
       return under != null;
   }
   
  public void fall()
   {
       setLocation(getX(), getY() + vSpeed);
       vSpeed = vSpeed + acceleration;
   }
   
   public void jump()
   {
       vSpeed = -jumpStrenght;
       speed = 6;
       fall();
   }    
   
   public void moveRight()
   {
       setLocation(getX()+speed, getY());
   }   
   
  public void moveLeft()
   {
       setLocation(getX()-speed, getY());
   } 
   
  public void checkKey()
   {
       if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
       if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
       if (Greenfoot.isKeyDown("up") && onGround())
        {
                jump();
        }
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Saw.class)))
       {
               Greenfoot.setWorld(new Saw_Level());
       }
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Mace.class)))
       {
               Greenfoot.setWorld(new Mace_Level());
       } 
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Orc.class)))
       {
               Greenfoot.setWorld(new Orc_Level());
       }
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Level1.class)))
       {
               Greenfoot.setWorld(new Level1());
       } 
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Level2.class)))
       {
               Greenfoot.setWorld(new Level3());
       } 
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_Level3.class)))
       {
               Greenfoot.setWorld(new Level3());
       } 
       if (Greenfoot.isKeyDown("down") && (isTouching(Door_BossLevel.class)))
       {
               Greenfoot.setWorld(new Boss_Level());
       } 
   }
        
        
        
        /** Running Right Images */
private final GreenfootImage[] runningRightAnimation =
{
    new GreenfootImage("Puupy_RR1.png" ),
    new GreenfootImage("Puupy_RR2.png"),
    new GreenfootImage("Puupy_RR3.png"),
    new GreenfootImage("Puupy_RR4.png"),
    new GreenfootImage("Puupy_RR5.png"),
    new GreenfootImage("Puupy_RR6.png"),
    new GreenfootImage("Puupy_RR7.png"),
    new GreenfootImage("Puupy_RR8.png"),
    new GreenfootImage("Puupy_RR9.png"),
    new GreenfootImage("Puupy_RR10.png"),
    new GreenfootImage("Puupy_RR11.png"),
    new GreenfootImage("Puupy_RR12.png"),
    new GreenfootImage("Puupy_RR13.png"),
    new GreenfootImage("Puupy_RR14.png"),
    new GreenfootImage("Puupy_RR15.png"),
    new GreenfootImage("Puupy_RR16.png"),
    new GreenfootImage("Puupy_RR17.png"),
    new GreenfootImage("Puupy_RR18.png")
};
 
/** Running Left Images */
private final GreenfootImage[] runningLeftAnimation =
{
    new GreenfootImage("Puupy_RL1.png"),
    new GreenfootImage("Puupy_RL2.png"),
    new GreenfootImage("Puupy_RL3.png"),
    new GreenfootImage("Puupy_RL4.png"),
    new GreenfootImage("Puupy_RL5.png"),
    new GreenfootImage("Puupy_RL6.png"),
    new GreenfootImage("Puupy_RL7.png"),
    new GreenfootImage("Puupy_RL8.png"),
    new GreenfootImage("Puupy_RL9.png"),
    new GreenfootImage("Puupy_RL10.png"),
    new GreenfootImage("Puupy_RL11.png"),
    new GreenfootImage("Puupy_RL12.png"),
    new GreenfootImage("Puupy_RL13.png"),
    new GreenfootImage("Puupy_RL14.png"),
    new GreenfootImage("Puupy_RL15.png"),
    new GreenfootImage("Puupy_RL16.png"),
    new GreenfootImage("Puupy_RL17.png"),
    new GreenfootImage("Puupy_RL18.png")
};
 
/** Jumping Right Images */
private final GreenfootImage[] jumpingRightAnimation =
{
    new GreenfootImage("Puppy_JumpRight1.png"),
    new GreenfootImage("Puppy_JumpRight2.png")
};
 
/** Jumping Left Images */
private final GreenfootImage[] jumpingLeftAnimation =
{
    new GreenfootImage("Puppy_JumpLeft1.png"),
    new GreenfootImage("Puppy_JumpLeft2.png")
};
 
/** Idlie Right Images */
private final GreenfootImage[] idleRightAnimation =
{
    new GreenfootImage("Puupy_Inactive_Right1.png"),
    new GreenfootImage("Puupy_Inactive_Right2.png"),
    new GreenfootImage("Puupy_Inactive_Right3.png"),
    new GreenfootImage("Puupy_Inactive_Right4.png"),
    new GreenfootImage("Puupy_Inactive_Right5.png"),
    new GreenfootImage("Puupy_Inactive_Right6.png"),
    new GreenfootImage("Puupy_Inactive_Right7.png"),
    new GreenfootImage("Puupy_Inactive_Right8.png"),
    new GreenfootImage("Puupy_Inactive_Right9.png"),
    new GreenfootImage("Puupy_Inactive_Right10.png"),
    new GreenfootImage("Puupy_Inactive_Right11.png")
};
 
/** Idle Left Images */
private final GreenfootImage[] idleLeftAnimation =
{
    new GreenfootImage("Puupy_Inactive_Left1.png"),
    new GreenfootImage("Puupy_Inactive_Left2.png"),
    new GreenfootImage("Puupy_Inactive_Left3.png"),
    new GreenfootImage("Puupy_Inactive_Left4.png"),
    new GreenfootImage("Puupy_Inactive_Left5.png"),
    new GreenfootImage("Puupy_Inactive_Left6.png"),
    new GreenfootImage("Puupy_Inactive_Left7.png"),
    new GreenfootImage("Puupy_Inactive_Left8.png"),
    new GreenfootImage("Puupy_Inactive_Left9.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png")
};
 
/** Current Animation */
private GreenfootImage[] currentAnimation = idleRightAnimation;
    }
danpost danpost

2018/2/18

#
Are all ground objects going to be at the same level (y-coordinates are all the same) in each world? or, will any world have any two ground objects within it at different heights?
There are more replies on the next page.
1
2
3
4
5
6