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

2016/4/6

removing object using size?

1
2
3
Super_Hippo Super_Hippo

2016/4/9

#
What happened to the constructor of the User_fish class?
public User_fish()
{
    updateImages();
}
You don't randomize the size of the AI. You set the size to 70, so it is smaller than the User_fish at the beginning (80). Add this at the end of the AI constructor:
size = Greenfoot.getRandomNumber(150)+50; //adjust the values as needed
getImage().scale(getImage().getWidth()*size/100, getImage().getHeight()*size/100);
For the last thing, you can do it the same way:
setLocation((getX()+getWorld().getWidth())%getWorld().getWidth(), getY());
khoalabear khoalabear

2016/4/9

#
is that last bit of code for the going out of the world bit? I'm not sure where to put that.
Super_Hippo Super_Hippo

2016/4/9

#
Yes and you can put that after the User_fish moved.
khoalabear khoalabear

2016/4/9

#
I don't understand after the User_fish moved. where is that in my code?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class User_fish extends Actor
{
    private int movementspeed = 6;
    private GreenfootImage[] images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
    private int size = 80;

    public User_fish()
    {
        updateImages();
    }
    
    public void act() 
    {
        checkKeys();
        eat();
    }    

    public void checkKeys()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("up")) dy--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (dx != 0 || dy != 0)
        {
            setLocation(getX()+dx*movementspeed, getY()+dy*movementspeed);
            if (dx < 0 && getImage() != images[0]) setImage(images[0]);
            else if (dx > 0 && getImage() != images[1]) setImage(images[1]);
        }
    }
    
    public void eat()
    {
        AI f = (AI) getOneIntersectingObject(AI.class);
        if (f != null)
        {
            if (size > f.size)
            {
                getWorld().removeObject(f);
                size++;
                updateImages();
            }
            else if (size < f.size) //idk how you want to handle same size
            {
                getWorld().removeObject(this);
                Greenfoot.stop(); //or something
            }
        }
    } 
         
    private void updateImages()
    {
            images[0] = new GreenfootImage("User_fish-left.png");
            images[1] = new GreenfootImage("User_fish-right.png");
            for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
    }
}
Super_Hippo Super_Hippo

2016/4/9

#
After line 28.
khoalabear khoalabear

2016/4/9

#
There's no error but it doesn't work. The user can move to the edge of the world but just stays there.
Super_Hippo Super_Hippo

2016/4/9

#
Change this line
super(1440, 810, 1);
in your world to
super(1440, 810, 1, false);
khoalabear khoalabear

2016/4/9

#
Ok. Just a slight problem now. At the start the user will shrink when either left or light is pressed to what I've set for private int size. Is there a way for the size to be changed when the world is loaded?
Super_Hippo Super_Hippo

2016/4/9

#
Oh... use this line after line 10:
setImage(images[0]);
Or course you can also use 1 instead 0. Or 'Greenfoot.getRandomNumber(2)'.
khoalabear khoalabear

2016/4/9

#
YES! It works finally! Thanks so much for your help. You've been amazing. Also thanks to Danpost for your help too ^.^
You need to login to post a reply.
1
2
3