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

2018/8/2

Keeping a jump code whilst scolling

mole2003 mole2003

2018/8/2

#
Hi all I am coding the offline trex game from google chrome. After getting the world to scroll the trex's jumping code is failing to work any ideas? Trex code
import greenfoot.*;
 
public class Trex extends Actor
{
    private int ySpeed;
 
    public Trex()
    {
    }
    public void setLocation(int x, int y)
    {
    
    }
    public void act()
    {
        int groundLevel = 500 - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) 
        {
            ySpeed++; 
            setLocation(getX(), getY()+ySpeed); 
            if (getY()>=groundLevel) 
            {
                setLocation(getX(), groundLevel); 
                Greenfoot.getKey(); 
           }
        }
        else 
        {
            if ("up".equals(Greenfoot.getKey())) 
            {
                ySpeed = -16; 
                setLocation(getX(), getY()+ySpeed); 
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setImage("duck.png");
        }
        else
        {
            setImage("Trex.png");
        }
    }
}
world code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    public static final int WIDE = 1185;
    public static final int HIGH = 432;
    private Scroller scroller;
    public Background()
    {
        super(WIDE,HIGH,1,false);
        GreenfootImage image = new GreenfootImage("background.png");
        scroller = new Scroller(this,image);
        prepare();
    }


    public void act()
    { 
        scroll();
        prepare();
    }
    

    
    private void prepare()
    {

        Trex trex = new Trex();
        addObject(trex,75,316);
        trex.setLocation(84,302);
    }


    private void scroll()
    {
        int speed = 2;
        scroller.scroll(speed, 0);
    }
        
}
scroller code:
import greenfoot.*;

public class Scroller
{
    private World world; 
    private GreenfootImage scrollImage; 
    private boolean limited; 
    private int scrolledX, scrolledY; 
    private int wide, high; 
   
    
    public Scroller(World viewWorld, GreenfootImage image)
    {
        world = viewWorld;
        scrollImage = image;
        if (image != null)
        {
            wide = image.getWidth();
            high = image.getHeight();
        }
        scroll(0, 0); 
    }
   
   
    public Scroller(World viewWorld, GreenfootImage image, int wide, int high)
    {
        this.wide = wide;
        this.high = high;
        limited = true;
        world = viewWorld;
        if (image != null)
        {
            
            scrollImage = new GreenfootImage(wide*world.getCellSize(), high*world.getCellSize());
            for (int x=0; x<wide*world.getCellSize(); x+= image.getWidth())
                for (int y=0; y<high*world.getCellSize(); y+=image.getHeight())
                    scrollImage.drawImage(image, x, y);
            
            scroll(0, 0);
        }
    }
   
    
    public void scroll(int dsx, int dsy)
    {
        
        if (limited)
        {
            
            int maxX = wide-world.getWidth();
            int maxY = high-world.getHeight();
            
            if (scrolledX+dsx < 0) dsx = -scrolledX;
            if (scrolledX+dsx >= maxX) dsx = maxX-scrolledX;
            if (scrolledY+dsy < 0) dsy = -scrolledY;
            if (scrolledY+dsy >= maxY) dsy = maxY-scrolledY;
            
            scrolledX += dsx;
            scrolledY += dsy;
            
            if (scrollImage != null)
            {
                world.getBackground().drawImage
                (   
                    scrollImage,
                    -scrolledX*world.getCellSize(),
                    -scrolledY*world.getCellSize()
                );
            }
        }
        else 
        {
            
            scrolledX += dsx;
            scrolledY += dsy;
            
            if (scrollImage != null)
            {
                
                int imageX = scrolledX*world.getCellSize();
                int imageY = scrolledY*world.getCellSize();
                
                imageX = imageX%wide;
                imageY = imageY%high;
                
                if (imageX < 0) imageX += wide;
                if (imageY < 0) imageY += high;
                
                GreenfootImage hold = new GreenfootImage(scrollImage);
                hold.drawImage(scrollImage, -imageX, -imageY);
                if (imageX > 0) hold.drawImage(scrollImage, wide-imageX, -imageY);
                if (imageY > 0) hold.drawImage(scrollImage, -imageX, high-imageY);
                if (imageX > 0 && imageY > 0)
                    hold.drawImage(scrollImage, wide-imageX, high-imageY);
                
                world.setBackground(hold);
            }
        }
        
        for (Object obj : world.getObjects(null))
        {
            Actor actor = (Actor) obj;
            actor.setLocation(actor.getX()-dsx, actor.getY()-dsy);
        }
    }
    public int getScrolledX()
    {
        return scrolledX;
    }
   
    public int getScrolledY()
    {
        return scrolledY;
    }
}
all scrolling code comes from Danpost's scrolling tutorial
danpost danpost

2018/8/2

#
The scrolling code is completely independent of the jumping code. It is the jumping code, in itself, that is "failing to work". Remove line 25 in the Trex class -- the line only ensures that getKey will return null on line 30. The getKey method is not suited well for player control. It is more suited for text input and general game control (when used properly). Better would be to use isKeyDown on line 30.
danpost danpost

2018/8/2

#
Also, remove line 27 from the Background class. You only want one Trex (player-controlled) object in your world. You do not want to add a new one into your world every act cycle.
mole2003 mole2003

2018/8/2

#
The jumping code worked before iplementing scrolling
danpost danpost

2018/8/2

#
mole2003 wrote...
The jumping code worked before iplementing scrolling
I was mistaken when I said this:
line 25 in the Trex class -- the line only ensures that getKey will return null on line 30
The lines is not needed, however does not force a null return on line 30. This, albeit, does not change the fact that scrolling is independent of jumping. You must have changed something if it does not work now. I will take a closer look at what you have.
danpost danpost

2018/8/2

#
Remove lines 10 through 13 to allow the Trex object to move. Those line should only be put in classes that create objects you do not want to have move. The Trex object is meant to "appear" to move -- it is not a non-moving object.
mole2003 mole2003

2018/8/3

#
However I want it to stay put on the X co-ordinate but can change the y coordinate
mole2003 mole2003

2018/8/3

#
Also removing lines 10-13 allowed the Trex to move backwards
mole2003 mole2003

2018/8/3

#
I have managed to get the trex to jump but he is still scrolls away with the rest of the world new codes:
import greenfoot.*;
 
public class Trex extends Actor
{
    private int ySpeed;
 
    public Trex()
    {
    }
    public void setLocation(int x)
    {
        
    }
    public void act()
    {
        int groundLevel = 340 - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            ySpeed++; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("up".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setImage("duck.png");
        }
        else
        {
            setImage("Trex.png");
        }
    }
}
and world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{

    public static final int WIDE = 1185;
    public static final int HIGH = 432;
    private Scroller scroller;
    public Background()
    {
        super(WIDE,HIGH,1,false);
        GreenfootImage image = new GreenfootImage("background.png");
        scroller = new Scroller(this,image);

        prepare();
    }

    public void act()
    { 
        scroll();

    }

    

    private void scroll()
    {
        int speed = 2;
        scroller.scroll(speed, 0);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Trex trex = new Trex();
        addObject(trex,165,292);
        trex.setLocation(140,302);
    }
}
danpost danpost

2018/8/3

#
mole2003 wrote...
I have managed to get the trex to jump but he is still scrolls away with the rest of the world new codes: << Code Omitted >>
The tutorial shows ways to restrict main actor movement as well. See the Actor follow scrolling code page (in scroll method). You will only need to restrict movement along the horizontal, however.
mole2003 mole2003

2018/8/6

#
Which bits do I need from the code and where shall I put them
danpost danpost

2018/8/6

#
mole2003 wrote...
Which bits do I need from the code and where shall I put them
Actually, all you probably need is:
move(2);
in the Trex class act method.
You need to login to post a reply.