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

2020/1/17

How do i make one specific actor follow my mouse?

Yuuki Yuuki

2020/1/17

#
I dont have any code because im pretty lost
Yuuki Yuuki

2020/1/17

#
nvm, i got it, instead i wanna know how to make an actor home on a different actor, like a homing missile, and how to increase the amount over time
danpost danpost

2020/1/17

#
Yuuki wrote...
how to make an actor home on a different actor, like a homing missile, and how to increase the amount over time
You will probably need a smooth moving/smooth rotating class, like my QActor class (found here) and a field for maximum turn rate with code to utilize it. My Zombie Shooter Demo scenario, which uses the QActor class has the following code for zombies to zero in on the player:
/** chase */
// the first code line below does what is described in the following commented lines
        // determine shortest turn direction to face chased
    // int angleDiff = getRotation()-(int)(Math.atan2(dy, dx)*180/Math.PI); // range: -359 to 359
    // int anglet = (angleDiff+360+180)%360-180; // range: -180 to 179 (sign is turn direction)
        // turn and move
    // turn(16*rate*(int)Math.signum(anglet)); 
turn(16*rate*(int)Math.signum((getRotation()-(int)(Math.atan2(dy, dx)*180/Math.PI)+540)%360-180));
move(rate/8, getQR()); // between 'move(0.1)' to 'move(0.4)' for a non-QActor, which is not possible
Yuuki Yuuki

2020/1/22

#
Thanks. Ima pick your brain some more, how do i make an actor grow for a specific amount of cycles.
danpost danpost

2020/1/22

#
Yuuki wrote...
Thanks. Ima pick your brain some more, how do i make an actor grow for a specific amount of cycles.
Grow a little each cycle or be larger for so many cycles, then be smaller again?
Yuuki Yuuki

2020/1/23

#
no, it stays the same size it only gets bigger
danpost danpost

2020/1/23

#
Yuuki wrote...
no, it stays the same size it only gets bigger
Hold the original image in a field and copy it to scale bigger each time.
Yuuki Yuuki

2020/1/24

#
and you do that how?
danpost danpost

2020/1/24

#
danpost wrote...
Hold the original image in a field and copy it to scale bigger each time.
Yuuki wrote...
and you do that how?
In class, I'll call it Fish, add a field to retain the original image and an int field to track sizing:
private GreenfootImage originalImage;
private int imageSizePct = 100;
Assign the image in a constructor block:
public Fish()
{
    originalImage = getImage();
}
You can then add a method that scales the image:
public void grow(int pctChange)
{
    imageSizePct += pctChange;
    GreenfootImage img = new GreenfootImage(originalImage);
    img.scale(img.getWidth()*imageSizePct/100, img.getHeight()*imageSizePct/100);
    setImage(img);
}
Yuuki Yuuki

2020/1/28

#
What does constructor mean?
danpost danpost

2020/1/28

#
Yuuki wrote...
What does constructor mean?
It is the block of code that is executed when you create a new instance of a class (a new object). The block looks similar to a method, but it does not have a return type given (nothing where "void" usually is) and its name is the same as the class. Your World subclass constructor starts with:
public MyWorld()
My Fish class constructor above will execute when
new Fish()
is found in your codee. Constructors automatically return the new object created. So, you would either assign the new object to a variable:
Fish fish = new Fish();
to be used later, or use it immediately e.g.:
addObject(new Fish(), 0, 0);
You need to login to post a reply.