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
danpost danpost

2016/4/8

#
khoalabear wrote...
Just realised * are already in there. So only the ones which come after 100.
Change line 5 to this:
for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
khoalabear khoalabear

2016/4/8

#
Hmm I get an error in the public void eat() now. cannot find symbol - class Fish.
Super_Hippo Super_Hippo

2016/4/8

#
Use the name of the class of the fishes which are npc.
khoalabear khoalabear

2016/4/8

#
public void eat()
    {
        AI_right f = (AI_right) getOneIntersectingObject(AI_right.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
            }
        }
    }
ok it now looks like this. f.size gives me cannot find symbol - variable size.
Super_Hippo Super_Hippo

2016/4/8

#
You need to have a 'public int size' in the 'AI_right' class which does the same as the size-variable in the User_fish class, it shows how big the fish is. Why is the name of the class 'AI_right' btw? Is there an 'AI_left' too and if yes, why?
khoalabear khoalabear

2016/4/8

#
So the size 80 which you provided will be the size of my user fish? And because the ai which spawn will be moving either from right to left or left to right of the screen so I made an AI_right and an AI_left.
Super_Hippo Super_Hippo

2016/4/8

#
Yes. Well, it is the size relative to the original size of the image. If that is the only difference between the classes, you can easily merge them into one class:
private int direction = 0; //+1=right, -1=left

public AI(int dir)
{
    direction = dir;
    
    //if they have different images
    if (direction==0) setImage(/**...*/);
    else setImage(/**...*/);
}

public void act()
{
    setLocation(getX()+direction, getY());
}
When creating the objects:
new AI(1) //for a fish moving from left to right
new AI(-1) //for a fish moving from right to left

//So for example:
addObject(new AI(1), 0, 100);
khoalabear khoalabear

2016/4/8

#
What int size am I supposed to give the AI_right class? they are created in my world class and the sizes are randomized. I also had it on a timer so it would spawn every second for both sides. Public AI(int dir) gives me invalid method declaration; return type required and I have no idea where to put the code for creating new objects.
Super_Hippo Super_Hippo

2016/4/9

#
If you name the class 'AI', then 'public AI()' will be the constructor of this class and won't need a return type. You can also call the call 'Fish' which would probably make more sense. So you don't you AI_right and AI_left, you only use one class. You can try out different sizes. Maybe it will be somewhere between 30 and 200. The scaling of the image can be done at the end of the constructor then.
khoalabear khoalabear

2016/4/9

#
I think I should just give you what I've got so far. world class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Pond_world extends World
{
    public Pond_world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1440, 810, 1); 
        prepare();
    }

    public void prepare()
    {
        User_fish user_fish = new User_fish();
        addObject(user_fish,1152,313);
        user_fish.setLocation(770,405);
    }

    public void act()
    {
        Right_spawn();
        Left_spawn();
    }
    
    public boolean canAddRight()
    {
        // in the world class, use this line
            return getObjects(AI_right.class).size() < 7;
    }
    
    public boolean canAddLeft()
    {
        // in the world class, use this line
            return getObjects(AI_left.class).size() < 7;
        }
        
    private int spawnCounter = 0;    
        
    public void Right_spawn()
    {
        if (canAddRight()) 
        {
            if (spawnCounter > 50) 
            {
                spawnCounter = 0;
                addObject(new AI_right(), 0, Greenfoot.getRandomNumber(getHeight()));
            }
            spawnCounter++;
        }
    }
    
    public void Left_spawn()
    {
        if (canAddLeft())
        {
            if (spawnCounter > 50)
            {
                spawnCounter = 0;
                addObject(new AI_left(), 1440, Greenfoot.getRandomNumber(getHeight()));
            }
            spawnCounter++;
        }
    }
}
AI_right class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class AI_right extends Actor
{
    private int movementspeed = 5;
    public int size = 70;
    private int direction = 0;
    
    public AI_right(int dir)
    {
        if (direction==0) setImage("AI_fish-right.png");
        else setImage("AI_fish-left.png");
    }
    
    public void act() 
    {
        setLocation(getX()+direction, getY());
        AI_right();
        remove();
    } 
    
    public void AI_right()
    {
        setLocation(getX() + movementspeed, getY());
    }
    
    public void remove()
    {
        if (getX() >= getWorld().getWidth() -1)
            getWorld().removeObject(this);
        else if (getX() <=1)
            getWorld().removeObject(this);
        else if (getY() >= getWorld().getHeight() -1)
            getWorld().removeObject(this);
        else if (getY() <=1)
            getWorld().removeObject(this);
    }
    
    public AI_right()
    {
        GreenfootImage img = new GreenfootImage("AI_fish-right.png");
        double scaleFactor = (Greenfoot.getRandomNumber(13)+1) / 10.0; //random number between 0.1 and 4.0
        img.scale( (int)(img.getWidth()*scaleFactor), (int)(img.getHeight()*scaleFactor));
        setImage(img);
    }
}
User_fish class:
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 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_right f = (AI_right) getOneIntersectingObject(AI_right.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);
    }
}
Could you explain what I should be doing a bit more clearer? I'm pretty new to all this so get lost quite quickly sorry.
Super_Hippo Super_Hippo

2016/4/9

#
Remove the AI_left class. Rename the AI_right class and everytime you wrote AI_right to AI. Pond_world class: I am sure you are able to merge lines 15 and 16 into one line. AI_class: You use a 'scaleFactor' there. Instead, you should use the size variable. The size variable should be used exactly like it is done in the User_fish class. So the size should be 100 times as much as the scaleFactor variable. Yes, you should randomize it there. (My mistake here:) In line 11, it should be 'if (direction==1)'. Use this as the act-method and remove the 'AI_right' and the 'remove' methods:
setLocation(getX()+direction*movementspeed, getY());
if (getX() < 1 || getX() > getWorld().getWidth()-2) getWorld().removeObject(this);
Adding the AI class will look like this:
int r = Greenfoot.getRandomNumber(2); //0 or 1
addObject(new AI(r*2-1), 1440-1440*r, Greenfoot.getRandomNumber(getHeight()));
khoalabear khoalabear

2016/4/9

#
it now looks like this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Pond_world extends World
{
    public Pond_world()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1440, 810, 1); 
        prepare();
    }

    public void prepare()
    {
        User_fish user_fish = new User_fish();
        addObject(user_fish,770,405);
        
    }

    public void act()
    {
        Right_spawn();
        
    }
    
    public boolean canAdd()
    {
        // in the world class, use this line
            return getObjects(AI.class).size() < 7;
    }
        
    private int spawnCounter = 0;    
    int r = Greenfoot.getRandomNumber(2);    
    public void Right_spawn()
    {
        if (canAdd()) 
        {
            if (spawnCounter > 50) 
            {
                spawnCounter = 0;
                addObject(new AI(r*2-1), 0, Greenfoot.getRandomNumber(getHeight()));
            }
            spawnCounter++;
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class AI extends Actor
{
    private int movementspeed = 5;
    public int size = 70;
    private int direction = 0;
    
    public AI(int dir)
    {
        if (direction==1) setImage("AI_fish-right.png");
        else setImage("AI_fish-left.png");
    }
    
    public void act() 
    {
        setLocation(getX()+direction*movementspeed, getY());
        if (getX() < 1 || getX() > getWorld().getWidth()-2) getWorld().removeObject(this);
    } 
}
nothing is spawning now.
Super_Hippo Super_Hippo

2016/4/9

#
Move line 31 to the top (after line 4). (This doesn't change anything, but the variables should be visible right at the top of the class.) Move line 32 right after line 40. Change the '0' in line 40 to '1440-1440*r' as suggested. Well, actually it will be '1339-1339*r' but it doesn't really matter. I am just seeing that you didn't add this line at the beginning of the constructor:
direction = dir;
Without it, direction stays at 0, the fish isn't moving and is instantly removed from the world because it is at the edge.
khoalabear khoalabear

2016/4/9

#
WOW ok it's finally started working again. Just that my user will start big and when touching the first fish will shrink in size. the sizes of the ai fish are also all the same but still get eaten even though they are bigger.
khoalabear khoalabear

2016/4/9

#
I also had some code that made my fish reappear on the other side if they went out of the world. it was:
public void moveRight()
    {
            setImage("User_fish-right.png");
            int worldWidth = getWorld().getWidth();
            int x = (getX()+movementspeed)%worldWidth;
            setLocation(x, getY());
            
    }
and this:
public void moveLeft()
    {
        setImage ("User_fish-left.png");
        int worldWidth = getWorld().getWidth();
        int x = (getX()-movementspeed+worldWidth)%worldWidth;
        setLocation(x, getY());
    }
How would I add this?
There are more replies on the next page.
1
2
3