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

2016/4/6

#
In my game there are fish spawning at different sizes. I want to make it so that when the player touches a fish it will eat the fish (if bigger) or get eaten (if smaller) depending on the size. I have no idea how to do this and would appreciate any help. Thanks. code for the user fish
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class User_fish extends Actor
{
    private int movementspeed = 6;
    
    public User_fish()
    {
        int percentage = 80;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }
    
    public void User_fish()
    {
        int percentage = 80;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }

    public void act() 
    {
        checkKeys();
        checkLeft();
        if (getWorld() != null) checkRight();
    }    

    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            moveLeft();
            User_fish();
        }
        if (Greenfoot.isKeyDown("Up"))
        {
            moveUp();
        }
        if (Greenfoot.isKeyDown("Down"))
        {
            moveDown();
        }
        if (Greenfoot.isKeyDown("Right"))
        {
            moveRight();
            User_fish();
        }
    }
    
    public void moveRight()
    {
            setImage("User_fish-right.png");
            int worldWidth = getWorld().getWidth();
            int x = (getX()+movementspeed)%worldWidth;
            setLocation(x, getY());
            
    }
    
    public void moveUp()
    {
        setLocation(getX(), getY() - movementspeed);
    }
        
    public void moveDown()
    {
        setLocation(getX(), getY() + movementspeed);
    }
        
    public void moveLeft()
    {
        setImage ("User_fish-left.png");
        int worldWidth = getWorld().getWidth();
        int x = (getX()-movementspeed+worldWidth)%worldWidth;
        setLocation(x, getY());
    }
    
    public void checkLeft()
    {
        Actor AI_left = getOneObjectAtOffset(0, 0, AI_left.class);
        if (AI_left != null)getWorld().removeObject(this);
    }
    
    public void checkRight()
    {
        Actor AI_right = getOneObjectAtOffset(0, 0, AI_right.class);
        if (AI_right != null) getWorld().removeObject(this);
    }
}
code for the ai
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class AI_right extends Actor
{
    private int movementspeed = 5;
    public void act() 
    {
        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);
    }
}
Super_Hippo Super_Hippo

2016/4/6

#
You could compare the sizes of the images to decide who eats whom. I would suggest to save the size as an int though. That makes things easier. It will look a bit like this:
private GreenfootImage[] images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
private int size = 80;


//somewhere in the act-method or called from it
Fish f = (Fish) getOneIntersectingObject(Fish.class);
if (f != null)
{
    if (size > f.size)
    {
        getWorld().removeObject(f);
        size++;
        updateImage();
    }
    else //if (size < f.size) //idk how you want to handle same size
    {
        getWorld().removeObject(this);
        //Greenfoot.stop(); //or something
    }
}


private void updateImages()
{
    images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
    for (int i=0; i<2; i++) images[i].scale(image.getWidth()*size, image.getHeight()*size);
}
When moving left
setImage(image[0]);
And when moving right
setImage(image[1]);
khoalabear khoalabear

2016/4/7

#
I forgot to mention that if my user eats an ai fish, it will grow in size. I haven't decided on how much by but how would I go about doing this? Also I'm not too sure about the code you provided. Do I just stick it in or will I need to remove some of my code? There was some code in there that was just to see if my player would be removed so will I need to remove that?
Super_Hippo Super_Hippo

2016/4/7

#
The growing of the fish was done in line 12. Every fish increases the size by 1% of the original size. Saying that, I just noticed that line 26 should have been this:
for (int i=0; i<2; i++) images[i].scale(image.getWidth()*size/100, image.getHeight()*size/100);
The '/100' were missing. When adding the code I provided, you should be able to remove everything in your code which is dealing with the images and with eating fishes.
khoalabear khoalabear

2016/4/7

#
Ok. I put your code in like this:
public void eat()
    {
        Fish f = (Fish) getOneIntersectingObject(Fish.class);
        if (f != null)
        {
            if (size > f.size)
            {
                getWorld().removeObject(f);
                size++;
                updateImage();
            }
            else //if (size < f.size) //idk how you want to handle same size
            {
                getWorld().removeObject(this);
                //Greenfoot.stop(); //or something
            }
        }
        
        
        private void updateImages()
        {
            images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
            for (int i=0; i<2; i++) images[i].scale(image.getWidth()*size/100, image.getHeight()*size/100);
        }
    }
I get an error for private void that says illegal start of expression. and where should I put the setImage code?
Super_Hippo Super_Hippo

2016/4/7

#
Add a closing bracket '}' in line 18. The setImages can be added like this: (I missed the 's' in 'images' there)
public void act()
{
    checkKeys();
    eat();
}

private 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]);
    }
}
khoalabear khoalabear

2016/4/7

#
I get a ton of errors in the updateImages() method.
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()
    {
        int percentage = 80;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }
    
    public void User_fish()
    {
        int percentage = 80;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }

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

    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            moveLeft();
            User_fish();
        }
        if (Greenfoot.isKeyDown("Up"))
        {
            moveUp();
        }
        if (Greenfoot.isKeyDown("Down"))
        {
            moveDown();
        }
        if (Greenfoot.isKeyDown("Right"))
        {
            moveRight();
            User_fish();
        }
    }
    
    public void moveRight()
    {
            setImage("User_fish-right.png");
            int worldWidth = getWorld().getWidth();
            int x = (getX()+movementspeed)%worldWidth;
            setLocation(x, getY());
            
    }
    
    public void moveUp()
    {
        setLocation(getX(), getY() - movementspeed);
    }
        
    public void moveDown()
    {
        setLocation(getX(), getY() + movementspeed);
    }
        
    public void moveLeft()
    {
        setImage ("User_fish-left.png");
        int worldWidth = getWorld().getWidth();
        int x = (getX()-movementspeed+worldWidth)%worldWidth;
        setLocation(x, getY());
    }
   
    public void eat()
    {
        Fish f = (Fish) getOneIntersectingObject(Fish.class);
        if (f != null)
        {
            if (size > f.size)
            {
                getWorld().removeObject(f);
                size++;
                updateImage();
            }
            else //if (size < f.size) //idk how you want to handle same size
            {
                getWorld().removeObject(this);
                //Greenfoot.stop(); //or something
            }
        }
    }
        
    private void updateImages()
    {
        images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
        for (int i=0; i<2; i++) images[i].scale(image.getWidth()*size/100, image.getHeight()*size/100);
    }
   
}
here's what my code looks like now. Can you tell me which lines of code I need to remove as I'm not sure and might mess it up.
Super_Hippo Super_Hippo

2016/4/7

#
I think you can remove all code instead of the given methods (change the checkKeys method like suggested). The only thing missing is the constructor then:
public User_fish()
{
    updateImages()
}
khoalabear khoalabear

2016/4/7

#
I'm still getting errors in updateImages() and checkKeys(). Can I send you the game or is that asking for too much? I'm totally lost right now.
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()
    {
        Fish f = (Fish) getOneIntersectingObject(Fish.class);
        if (f != null)
        {
            if (size > f.size)
            {
                getWorld().removeObject(f);
                size++;
                updateImage();
            }
            else //if (size < f.size) //idk how you want to handle same size
            {
                getWorld().removeObject(this);
                //Greenfoot.stop(); //or something
            }
        }
    }
        
    private void updateImages()
    {
        images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
        for (int i=0; i<2; i++) images[i].scale(image.getWidth()*size/100, image.getHeight()*size/100);
    }
   
}
Super_Hippo Super_Hippo

2016/4/8

#
Please show where exactly the errors occur. I am not sure if line 55 is working like this. Sometimes arrays don't work as I hope they do... If it doesn't, try this: Well, I missed a part there in the last line as well. If line 55 works as it is, keep it and only update line 56.
    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(image.getWidth()*size/100), images[i].scale(image.getHeight()*size/100));
    }
I am not seeing the error in the checkKeys-method yet, so please indicate the line and show the error message. This will make it easier. If there is an error telling you that a method needs an int as parameter and not a double (scale and setLocation), cast it to an int: (I am not sure if that is the case)
(int) (...)
danpost danpost

2016/4/8

#
You cannot get the width or height of an array. You are missing the index values to point to a specific image of the array in the 'for' loop (line 56).
Super_Hippo Super_Hippo

2016/4/8

#
Ugh right, I don't know how that happened... although it looked even worse before the last "correction".
for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100), images[i].scale(images[i].getHeight()*size/100));
I think there should be a 'scale(int x)' method (with x being the size in % of the original size or as a double, so 0.5 would be 50%) implemented to the GreenfootImage-class which does exactly that. The scale method is often used like that.
khoalabear khoalabear

2016/4/8

#
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].scale(images[i].getHeight()*size/100))*;
   }
the line starting with for, it says something like ',' expected where I've indicated with *. I think you also missed the ')' near the end of each isKeyDown line so it would be like ("up"))
khoalabear khoalabear

2016/4/8

#
Just realised * are already in there. So only the ones which come after 100.
Super_Hippo Super_Hippo

2016/4/8

#
Yes, the ')' are missing there. For the other line...okay... if I didn't know it better, I would say I am drunk...my last correction was worse than everything else. This time I thought about it... it should work finally -.-
for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
There are more replies on the next page.
1
2
3