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

2018/4/26

How do I make my minnows move on the y axis

applecider124 applecider124

2018/4/26

#
Ive gotten this far.. They move down just not back up. Thank you xx import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.lang.Object; /** * Write a description of class Minnow here. * * @author (your name) * @version (a version number or a date) */ public class Minnow extends Animal { private GreenfootImage image3; private GreenfootImage image4; private int sharksEaten; /** * Act - do whatever the Minnow wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Minnow() { image3 = new GreenfootImage("Minnow1.png"); image4 = new GreenfootImage("Minnow2.png"); } public void act() { changeImage(); movingMinnow();// Add your action code here. eatShark(); } public void changeImage() { if (Greenfoot.isKeyDown("n")) { setImage(image4); } else { setImage(image3); } } public void movingMinnow() { setLocation(getX(),getY()+5); if (atWorldEdge()) { setLocation(getX(),getY()-5); setRotation(180); } } public void eatShark() { if (canSee(Shark.class)) { eat(Shark.class); sharksEaten = sharksEaten +1; } if (canSee(Shark2.class)) { eat(Shark2.class); sharksEaten = sharksEaten +1; } if (sharksEaten == 2) { Greenfoot.stop(); } } }
danpost danpost

2018/4/26

#
What is the user pressing "n" supposed to do?
applecider124 applecider124

2018/4/27

#
danpost wrote...
What is the user pressing "n" supposed to do?
"n" is just changing the picture that represents the minnow. Its just an added annimation.
danpost danpost

2018/4/27

#
applecider124 wrote...
"n" is just changing the picture that represents the minnow. Its just an added annimation.
Oh. okay. Add a direction control field of type int. Allow values of either one (for down) or negative one (for up). Use it both for moving and for turning around.
private int direction = 1;

public void movingMinnow()
{
    setLocation(getX()+getY()+direction*5); // move
    if (atWorldEdge())
    {
        direction = -direction; // change direction
        turn(180); // turn around
    }
}
applecider124 applecider124

2018/4/30

#
danpost wrote...
applecider124 wrote...
"n" is just changing the picture that represents the minnow. Its just an added annimation.
Oh. okay. Add a direction control field of type int. Allow values of either one (for down) or negative one (for up). Use it both for moving and for turning around.
private int direction = 1;

public void movingMinnow()
{
    setLocation(getX()+getY()+direction*5); // move
    if (atWorldEdge())
    {
        direction = -direction; // change direction
        turn(180); // turn around
    }
}
Thank you so much!!
You need to login to post a reply.