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

2018/5/16

Enemy Actor which increases and decreases its size (loop)

TheWaffelmeister TheWaffelmeister

2018/5/16

#
I know that I have to create a loop with a scale command which gets reversed but I don't really know what to do

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class ScaleDot extends Enemy
{
    public void act() 
    {
        for(int i=1; i<11; i++){
            GreenfootImage image = getImage();                   
            image.scale(image.getWidth() + 1, image.getHeight() + 1);
            setImage(image);// Add your action code here.
        }
        
    }
}

danpost danpost

2018/5/16

#
TheWaffelmeister wrote...
I know that I have to create a loop with a scale command which gets reversed but I don't really know what to do << Code Omitted >>
Are you wanting only two sizes, normal and larger, or a gradual changing in size, expanding and shrinking?
TheWaffelmeister TheWaffelmeister

2018/5/16

#
danpost wrote...
TheWaffelmeister wrote...
I know that I have to create a loop with a scale command which gets reversed but I don't really know what to do << Code Omitted >>
Are you wanting only two sizes, normal and larger, or a gradual changing in size, expanding and shrinking?
yes, I want a smooth transition between a min and max value so that the enemy actor is able to expand and shrink (it's non-moving, just an obstacle)
danpost danpost

2018/5/16

#
TheWaffelmeister wrote...
I want a smooth transition between a min and max value so that the enemy actor is able to expand and shrink (it's non-moving, just an obstacle)
Then, because multiple scaling will distort an image, you will need to keep the original image in a field; plus, you will need an int field to "time" the phases of expanding and shrinking. The value of the timer field can both regulate the speed and direction of change (expanding or shrinking). Always create a new image by copying the original image and then scale it to some new value. Basic codes would be:
// fields
private GreenfootImage baseImage;
private int imageTimer;

// in act (or in a method called from act)
imageTimer = (imageTimer+1)%120;  // 10 = #sizes, 6 = delay, 2 = #directions (10*6*2 = 120)
if (imageTimer%6 == 0) // time to change image
{
    GreenfootImage image = new GreenfootImage(baseImage); // new copy of original
    int imgWidth = image.getWidth(); // image width
    int imgHeight = image.getHeight(); // image height
    int additional = imageTimer/6; // amount to add to width and height (if growing)
    if (imageTimer > 60) additional = 20-imageTimer/6; // amount to add to width and height (if shrinking)
    image.scale(imgWidth+additional, imgHeight+additional);
    setImage(image);
}
The only thing you may want to adjust is the delay. If you adjust it, all values that are a multiple of 6 (as well as the 6's) will need adjusted accordingly.
You need to login to post a reply.