Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
- /**
- * An enemy which is stationary on the ground, but is periodicly jumping up, from the ground
- * and then is going down again.
- *
- * @author Martin Rohwedder
- * @version 1.0
- */
- public class SandMan extends Enemy
- {
- //Constants defining the minimum and maxiumum y position this enemy can 'jump' to.
- private final int MAX_Y_POS = 695;
- private final int MIN_Y_POS = 645;
- private int speed = 3;
- private boolean doJumpAnimation = false;
- private boolean stretching = false;
- private int timeCycle = 120;
- /**
- * Act - do whatever the SandMan wants to do. This method is called whenever
- * the 'Act' or 'Run' button gets pressed in the environment.
- */
- public void act()
- {
- checkForOnGoingAnimation();
- doJumpAnimation();
- }
- /**
- * Check if the animation is in action or not. If not the time cycle will be resetted, and
- * the animation can begin again.
- */
- private void checkForOnGoingAnimation()
- {
- //If not doing jump animation, then go ahead and do it.
- if(!doJumpAnimation)
- {
- if (timeCycle <= 0)
- {
- doJumpAnimation = true;
- if (doJumpAnimation)
- {
- //Reset Time cycle to between 1 and 4 seconds.
- timeCycle = Greenfoot.getRandomNumber(240) + 60;
- }
- }
- else
- {
- //Decrement time by 1
- timeCycle--;
- }
- }
- }
- /**
- * Do the actual jump animation. If the enemy is stretching (it's at its highest), then
- * make the animation, so the enemy go down, and otherwise up.
- */
- private void doJumpAnimation()
- {
- if (doJumpAnimation)
- {
- //If stretching, make the enemy animate downwards
- if (stretching)
- {
- if (getY() < MAX_Y_POS)
- {
- setLocation(getX(), getY() + speed );
- }
- else
- {
- stretching = false;
- doJumpAnimation = false;
- }
- }
- else
- {
- if (getY() > MIN_Y_POS)
- {
- setLocation(getX(), getY() - speed );
- }
- else
- {
- stretching = true;
- doJumpAnimation = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement