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

2021/11/30

FROG JUMP

1
2
ronald ronald

2021/11/30

#
public void act() {        
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-10, getY());
            
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+10, getY());
            
        }
        if (Greenfoot.isKeyDown("up") && (onBlock32() == true)) {
            vSpeed = jumpHeight;
            fall();
        }
        
        checkFalling();
        animate();
    }
Hello I am creating a frog game, I explain to you, my frog moves with setLocation, I would like to know how to stop the frog when it is facing a wall or a platform of blocks made up of an array of blocks currently my frog is crossing the wall of blocks I give you part of the code, it would be easier to understand Thank you for your help
Spock47 Spock47

2021/12/1

#
Assuming that the wall's size is greater than the movement distance (here: 10), you can do the following: 1. Do the movement:
setLocation(getX()-10, getY()); // move left
2. Check whether the actor now is in a collision with a wall and reset its position if there actually is a wall (collision):
if (isTouching(Wall.class)) {
    setLocation(getX()+10, getY()); // undo move to the left
}
This way, the movement is immediately undone, if it would result in collision/touching of a wall. Live long and prosper, Spock47
ronald ronald

2021/12/1

#
ok thank you, i knew but I can't understand why the frog doesn't move anymore, once this code is compiled, it jumps but doesn't move anymore I want to tell you that it is a 2D array but I do not think that it comes from there, I think more of the booleanne onBlock32 thank you for your explanations
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Frog here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Frog extends Actor {    
    /**
     * Act - do whatever the Frog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int vSpeed = 0;
    private int acceleration = 1;
    private int jumpHeight = -20;
    
    private GreenfootImage images = getImage();
    private String[] imageNames = {"frog0.png", "frog1.png", "frog2.png", "frog3.png", "frog4.png", "frog5.png", "frog6.png", "frog7.png"};
    private int currentImage = 0;
    int xPos;
    int yPos;
    
    public Frog() {
    }
    
    public void addedToWorld(World world) {
        xPos = getX();
        yPos = getY();
    }
    
    public void act() {        
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX()-10, getY());
            if (isTouching(Block32.class)) {
                setLocation(getX()+10, getY());
            }
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX()+10, getY());
            if (isTouching(Block32.class)) {
                setLocation(getX()-10, getY());
            }
        }
        if (Greenfoot.isKeyDown("up") && (onBlock32() == true)) {
            vSpeed = jumpHeight;
            fall();
        }
        
        checkFalling();
        animate();
    }
    
    public void fall() {
        setLocation(getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
    
    private boolean onBlock32() {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Block32.class);
        return under != null;
    }

    public void checkFalling() {
        if ((onBlock32() == false)) {
            fall();
        }
        if ((onBlock32() == true)) {
            vSpeed = 0;
        }
    }
    
    private void animate() {
        if (getX() != xPos || getY() != yPos) {
            currentImage++;
            if (currentImage >= imageNames.length) 
                currentImage = 0;
                
                GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
                
                if (getX() < xPos) 
                    newImage.mirrorHorizontally();
                                
                setImage(newImage);
                images = getImage();
            
        }
        xPos = getX();
        yPos = getY();
    }
}

danpost danpost

2021/12/1

#
You probably fall "into" the block and therefore prevent moving left or right because the actor is now touching the block. When landing (or falling and finding that a block is touched), raise the actor up off the block (use y-coordinate of block minus half the height of both actors' images (plus, maybe, minus 1 more pixel) for the new y of the falling actor.
ronald ronald

2021/12/1

#
ok I understand, the frog falls in the block and does not move, I tested your explanation, it moves more or less to the right or the left but falls squarely in the block I think I will review the fall method
Spock47 Spock47

2021/12/1

#
The problem could be that the falling will continue until there is a collision, i.e. the frog's lowest pixel will be in the block, because if the frog is not one pixel in the block, the checkFalling method will make the frog fall again. You could change the onBlock32 method accordingly that it checks whether the frog is right above the block (just adding "+ 1" to the y value), i.e.
    private boolean onBlock32() {
        Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2 + 1, Block32.class);
        return under != null;
    }
Now, the frog stops falling when his lowest pixel is directly above the block, but not within the block itself. This should work given you already implemented danpost's very good hint that at the end of the fall, the frog has to be moved up a bit, something like
    public void checkFalling() {
        if ((onBlock32() == false)) {
            fall();
        }
        if ((onBlock32() == true)) {
            vSpeed = 0;
            placeOnBlock();
        }
    }    

    private void placeOnBlock() {
        final Actor belowBlock = this.getOneObjectAtOffset(0, getImage().getHeight() / 2, Block32.class);
        if (belowBlock != null) {
            setLocation(getX(), belowBlock.getY() - belowBlock.getImage().getHeight() / 2 - getImage().getHeight() / 2 - 1);
        }
    }
Live long and prosper, Spock47
ronald ronald

2021/12/2

#
thank you spock47 and without forgetting the unforgettable danpost I can't wait to see your scenarios, spock47 finally my little frog is doing better, sometimes it gets stuck in the corners now i will try to create a 2D array scrolling code, not sure it exists, I haven't seen it, maybe it would work better with getRandomNumber
ronald ronald

2021/12/2

#
rehello I try to switch images (background) between world and actor when my little frog touches the gift is it possible ? thank you
danpost danpost

2021/12/2

#
ronald wrote...
rehello I try to switch images (background) between world and actor when my little frog touches the gift is it possible ?
Maybe use: getWorld().setBackground(<<GreenfootImage>> or getWorld().setBackground(<<String>>)
ronald ronald

2021/12/2

#
I think it's a little more complicated than expected I didn’t think about that I will try to explain, I have a 2d array map in world with 0 and 1, then I have a block class in actor which displays walls of blocks according to 0 and 1 I thought that with switch, I will not see the class block in actor, it does not only disappear the background of world, with the switch, I try to change the background to pass to another class constitutes an array of images but we have to rely on the world background class and the actor block class to make them disappear then I make an array with getWorld (). setBackground (images ) something like that for another class in actor with lots of images thank you
danpost danpost

2021/12/2

#
If the background of your world is constantly being recreated according to the 1s and 0s, then maybe a boolean (or an int timer) can be used to put the updating on hold while a different background is being displayed. Hope I am on the right track.
ronald ronald

2021/12/2

#
not stupid as an idea, i will try this
ronald ronald

2021/12/3

#
I find it a bit complicated to change the background knowing that I have a scrolling, a 2D array map with 0 and 1, an animation of images in array for the frog which moves and still an animation of images in array like my scenario TRUMP and GEOMETRY all this to go to the scenario like TRUMP when the little frog touches the gift I tested removeObjects for the Block class in actor, it disappears well but the scrolling, the frog and the gift are still there, I looked at the background in boolean on the forum but I find that it is complicated to put all this in boolean (2d array, scrolling, animation ...) thank you
danpost danpost

2021/12/3

#
An alternate idea -- create a new subclass of World and use an instance of it to make the new background:
public class TempWorld extends World
{
    private World world;
    
    public TempWorld(World w)
    {
        super(600, 400, 1);
        world = w; // save scrolling world
        prepare();
    }
    
    ...
    
    public void act()
    {
        if (...) Greenfoot.setWorld(world); // return to scrolling world
    }
}
using the following in scrolling world:
// in World subclass
if (...) Greenfoot.setWorld(new TempWorld(this));

// or, in Actor subclass
if (...) Greenfoot.setWorld(new TempWorld(getWorld()));
ronald ronald

2021/12/4

#
I didn t do what I wanted, it will be another time. I have just published my scenario with the source code tell me why the screen is black, I think it is because of the images which are superimposed or something else, yet it works well thank you
There are more replies on the next page.
1
2