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

2013/10/31

Piggie change direction

ddvink ddvink

2013/10/31

#
Here's my code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class RibbonPig here. * * @author (Patrick van Kralingen) * @version (0.8) */ public class RibbonPig extends Mob { // Check einde platform private boolean isOnMostOutside = false; private int richting = 1; private int speed = 4; //Behaviour public int behaviourTimer = 10; public int actionRandomiser = 1; public int jumpStrength = -3; //Eigenschappen public double hp = 400.0; private boolean alive = true; //Loot public int dropCalculator = 0; public int mesoValue = 0; public int throwingstarGenerator = 0; // Damage die wordt aangericht door pig aan Rogue private int ribbonPigDamage = 25; private int pigGivenExperience = 250; private Rogue rogue; public RibbonPig(Rogue rogue){ this.rogue = rogue; // The piggies moeten een method of variabele of iets // dergelijks hebben, dat als ze dood zijn, een // bepaalde hoeveelheid XP doorpassen naar de Rogue // methods which have to start from the constructor; // loadingTheImages(); } public void act() { // Methods from the superclass checkLeftWalls(); checkRightWalls(); checkFall(); changeDirectionCheck(); timer(); // Methods from this class animate(sequence); checkTimer(); behaviourTimer--; } public void changeDirectionCheck(){ // The checkFloor is a method in the Mover-class which checks if you are // on a outside platform. Returns true when it is. if(checkFloor()){ changeDirectionPig(); } } // Verander de richting van de Pig public void changeDirectionPig(){ if(richting == 1){ richting = 2; } else if (richting == 2){ richting = 1; } speed = -speed; } public void setRibbonPigDamage(int ribbonPigDamage){ this.ribbonPigDamage = ribbonPigDamage; } public int getRibbonPigDamage(){ return ribbonPigDamage; } // Getters and setters for alive or not public void setAlive(boolean alive){ this.alive = alive; } public boolean getAlive(){ return alive; } // ------------------------------------- private void sendExperienceToRogueObject(){ // xpValue verzenden naar Rogue } public void animate(int sequence) { switch(sequence) { case 0: idle(richting); break; case 1: animateLeft(); break; case 2: animateRight(); break; case 3: die(richting); break; case 4: hitFace(richting); break; default: break; } } /************* *Richting Checker **********/ private void idle(int richting) { if(richting == 1) { animateIdleLeft(); } if (richting == 2) { animateIdleRight(); } } private void hitFace(int richting) { if(richting == 1) { animateHitFaceLeft(); } if (richting == 2) { animateHitFaceRight(); } } private void die(int richting) { if(richting == 1) { animateDieLeft(); } if (richting == 2) { animateDieRight(); } } /************* * * * * *ANIMATIONS * * * **********/ public void animateIdleLeft() { if(frame == 1) { setImage(idleLeft); } else if (frame == 2) { setImage(idleLeft); } else if(frame == 3) { setImage(idleLeft); frame = 0; return; } } public void animateIdleRight() { if(frame == 1) { setImage(idleRight); } else if (frame == 2) { setImage(idleRight); } else if(frame == 3) { setImage(idleRight); frame = 0; return; } } public void animateRight() { if(frame == 1) { setImage(animateRight); } else if (frame == 2) { setImage(animateRight); } else if(frame == 3) { setImage(animateRight); frame = 0; return; } } public void animateLeft() { if(frame == 1) { setImage(animateLeft); } else if (frame == 2) { setImage(animateLeft); } else if(frame == 3) { setImage(animateLeft); frame = 0; return; } } public void animateDieLeft() { if(frame == 1) { setImage(dieLeft); } else if (frame == 2) { } else if(frame == 3) { } else if(frame == 4) { setImage(dieLeft); } else if(frame == 5) { } else if(frame == 6) { } else if(frame == 7) { setImage(dieLeft); getImage().setTransparency(240); } else if(frame == 8) { setImage(dieLeft); getImage().setTransparency(200); } else if(frame == 9) { setImage(dieLeft); getImage().setTransparency(170); } else if(frame == 10) { removeAnimation(); frame = 0; return; } } public void animateDieRight() { if(frame == 1) { setImage(dieRight); } else if (frame == 2) { } else if(frame == 3) { } else if(frame == 4) { setImage(dieRight); } else if(frame == 5) { } else if(frame == 6) { } else if(frame == 7) { setImage(dieRight); getImage().setTransparency(240); } else if(frame == 8) { setImage(dieRight); getImage().setTransparency(200); } else if(frame == 9) { setImage(dieRight); getImage().setTransparency(170); } else if(frame == 10) { removeAnimation(); frame = 0; return; } } public void animateHitFaceLeft() { if(frame == 1) { setImage(HitFaceLeft); return; } else if(frame == 7) { frame = 0; return; } } public void animateHitFaceRight() { if(frame == 1) { setImage(HitFaceRight); return; } else if(frame == 7) { frame = 0; return; } } public void removeAnimation() { getWorld().removeObject(this); } /************* * * * * *BEHAVIOUR * * * **********/ public void checkTimer() { if(behaviourTimer == 0) { behaviourTimer(); } actionRandomiser(); } public void behaviourTimer() { behaviourTimer = Greenfoot.getRandomNumber(400) + 100; // you may want a larger number here actionRandomiser = Greenfoot.getRandomNumber(3); } public void actionRandomiser() { switch (actionRandomiser) { case 0: speed = 0; sequence = 0; break; case 1: speed = 4; sequence = 2; moveRight(); richting = 2; break; case 2: speed = 4; sequence = 1; moveLeft(); richting = 1; break; case 3: speed = 0; sequence = 3; break; } } public void moveRight() { setLocation(getX()+speed, getY()); } public void moveLeft() { setLocation(getX()-speed, getY()); } //HIT /** * */ public void hit(double damage) { hp = hp - damage; if(hp <= 0) { actionRandomiser = 3; createLoot(); behaviourTimer = behaviourTimer + 200; //Greenfoot.playSound("RibbonPig_Die.wav"); setAlive(false); rogue.addRogueExperience(pigGivenExperience); } else { //--life if ( damage >= 150) { sequence = 4; speed = 0; frame = 0; behaviourTimer = behaviourTimer + 200; } } } //Loot Generator public void createLoot() { dropCalculator = Greenfoot.getRandomNumber(100); throwingstarGenerator = Greenfoot.getRandomNumber(100); if (dropCalculator >= 70) { mesoValue = Greenfoot.getRandomNumber(100) + 50; getWorld().addObject(new Meso(mesoValue), getX(), getY()); //Greenfoot.playSound("DropItem.wav"); } else if (dropCalculator <= 3) { getWorld().addObject(new Claw1(), getX(), getY()); //Greenfoot.playSound("DropItem.wav"); } else if (throwingstarGenerator <=2 ) { getWorld().addObject(new WolbiDrop(), getX(), getY()); //Greenfoot.playSound("DropItem.wav"); } } /** * SPRITES INLADEN **/ //Idle left GreenfootImage idleLeft = new GreenfootImage { new GreenfootImage("ribbonPig_left_stand_0.png"), new GreenfootImage("ribbonPig_left_stand_1.png"), new GreenfootImage("ribbonPig_left_stand_2.png") }; //Idle right GreenfootImage idleRight = new GreenfootImage { new GreenfootImage("ribbonPig_right_stand_0.png"), new GreenfootImage("ribbonPig_right_stand_1.png"), new GreenfootImage("ribbonPig_right_stand_2.png") }; //Naar links lopen GreenfootImage animateLeft = new GreenfootImage { new GreenfootImage("ribbonPig_left_move_0.png"), new GreenfootImage("ribbonPig_left_move_1.png"), new GreenfootImage("ribbonPig_left_move_2.png") }; //naar Rechts lopen GreenfootImage animateRight = new GreenfootImage { new GreenfootImage("ribbonPig_right_move_0.png"), new GreenfootImage("ribbonPig_right_move_1.png"), new GreenfootImage("ribbonPig_right_move_2.png") }; GreenfootImage dieLeft = new GreenfootImage { new GreenfootImage("ribbonPig_left_die1_0.png"), new GreenfootImage("ribbonPig_left_die1_1.png"), new GreenfootImage("ribbonPig_left_die1_2.png") }; GreenfootImage dieRight = new GreenfootImage { new GreenfootImage("ribbonPig_right_die1_0.png"), new GreenfootImage("ribbonPig_right_die1_1.png"), new GreenfootImage("ribbonPig_right_die1_2.png") }; GreenfootImage HitFaceLeft = new GreenfootImage { new GreenfootImage("ribbonPig_left_hit1_0.png") }; GreenfootImage HitFaceRight = new GreenfootImage { new GreenfootImage("ribbonPig_right_hit1_0.png") }; } On my world a couple of these pigs are created. They run on a floor existing of platforms which all have a boolean value of false, except the most outside ones, they have true. If a piggie walks on a true-platfom it's got to change direction, but for some reason he doesn't. Can someone help? btw. I can't paste my code between code-tags, according to the browser I use here. Sorry for the inconvinience. Yours, Dennis
Tavi Tavi

2013/10/31

#
Here you co :) You can manually type "" and "" without the under scores to create code syntax :) .
/**
 * Write a description of class RibbonPig here.
 * 
 * @author (Patrick van Kralingen) 
 * @version (0.8)
 */
public class RibbonPig extends Mob
{
    // Check einde platform
    private boolean isOnMostOutside = false;

    private int richting = 1;
    private int speed = 4;

    //Behaviour
    public int behaviourTimer = 10;
    public int actionRandomiser = 1;
    public int jumpStrength = -3;   

    //Eigenschappen 
    public double hp = 400.0;
    private boolean alive = true;

    //Loot
    public int dropCalculator = 0;
    public int mesoValue = 0;
    public int throwingstarGenerator = 0;

    // Damage die wordt aangericht door pig aan Rogue
    private int ribbonPigDamage = 25;
    private int pigGivenExperience = 250;

    private Rogue rogue;

    public RibbonPig(Rogue rogue){
        this.rogue = rogue;
        // The piggies moeten een method of variabele of iets
        // dergelijks hebben, dat als ze dood zijn, een 
        // bepaalde hoeveelheid XP doorpassen naar de Rogue

        // methods which have to start from the constructor;
        // loadingTheImages();
    }

    public void act() 
    {
        // Methods from the superclass
        checkLeftWalls();
        checkRightWalls();
        checkFall();
        changeDirectionCheck();
        timer();

        // Methods from this class
        animate(sequence);
        checkTimer();
        behaviourTimer--;
    }    
    
    public void changeDirectionCheck(){
        // The checkFloor is a method in the Mover-class which checks if you are
        // on a outside platform. Returns true when it is.
        if(checkFloor()){
            changeDirectionPig();
        }
    }

    
    // Verander de richting van de Pig
    public void changeDirectionPig(){
        if(richting == 1){
            richting = 2;
        } else if (richting == 2){
            richting = 1;
        }
        
        speed = -speed;
    }
    
    public void setRibbonPigDamage(int ribbonPigDamage){
        this.ribbonPigDamage = ribbonPigDamage;
    }

    public int getRibbonPigDamage(){
        return ribbonPigDamage;
    }

    // Getters and setters for alive or not
    public void setAlive(boolean alive){
        this.alive = alive;
    }

    public boolean getAlive(){
        return alive;
    }
    // -------------------------------------

    private void sendExperienceToRogueObject(){
        // xpValue verzenden naar Rogue
    }

    public void animate(int sequence) {
        switch(sequence)
        {
            case 0:
            idle(richting);
            break;
            case 1:
            animateLeft();
            break;
            case 2: 
            animateRight();
            break;
            case 3: 
            die(richting);
            break;
            case 4: 
            hitFace(richting);
            break;
            default:
            break;
        }
    }

    /*************
     *Richting Checker
     **********/
    private void idle(int richting)
    {
        if(richting == 1)
        {
            animateIdleLeft();
        }
        if (richting == 2)
        {
            animateIdleRight();
        }
    }

    private void hitFace(int richting)
    {
        if(richting == 1)
        {
            animateHitFaceLeft();
        }
        if (richting == 2)
        {
            animateHitFaceRight();
        }
    }

    private void die(int richting)
    {
        if(richting == 1)
        {
            animateDieLeft();
        }
        if (richting == 2)
        {
            animateDieRight();
        }

    }

    /*************
     *
     *
     *
     *
     *ANIMATIONS
     *
     *
     *
     **********/

    public void animateIdleLeft()
    {
        if(frame == 1)
        {
            setImage(idleLeft[0]);
        }
        else if (frame == 2)
        {
            setImage(idleLeft[1]);
        }
        else if(frame == 3)
        {
            setImage(idleLeft[2]);
            frame = 0;
            return;
        }
    }

    public void animateIdleRight()
    {
        if(frame == 1)
        {
            setImage(idleRight[0]);
        }
        else if (frame == 2)
        {
            setImage(idleRight[1]);
        }
        else if(frame == 3)
        {
            setImage(idleRight[2]);
            frame = 0;
            return;
        }
    }

    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(animateRight[0]);
        }
        else if (frame == 2)
        {
            setImage(animateRight[1]);
        }
        else if(frame == 3)
        {
            setImage(animateRight[2]);
            frame = 0;
            return;
        }
    }

    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(animateLeft[0]);
        }
        else if (frame == 2)
        {
            setImage(animateLeft[1]);
        }
        else if(frame == 3)
        {
            setImage(animateLeft[2]);
            frame = 0;
            return;
        }
    }

    public void animateDieLeft()
    {
        if(frame == 1)
        {
            setImage(dieLeft[0]);
        }
        else if (frame == 2)
        {
        }
        else if(frame == 3)
        {
        }
        else if(frame == 4)
        {
            setImage(dieLeft[1]);
        }
        else if(frame == 5)
        {
        }
        else if(frame == 6)
        {
        }
        else if(frame == 7)
        {
            setImage(dieLeft[2]);
            getImage().setTransparency(240);
        }
        else if(frame == 8)
        {
            setImage(dieLeft[2]);
            getImage().setTransparency(200);
        }
        else if(frame == 9)
        {
            setImage(dieLeft[2]);
            getImage().setTransparency(170);
        }
        else if(frame == 10)
        {
            removeAnimation();
            frame = 0;
            return;
        }
    }

    public void animateDieRight()
    {
        if(frame == 1)
        {
            setImage(dieRight[0]);
        }
        else if (frame == 2)
        {
        }
        else if(frame == 3)
        {
        }
        else if(frame == 4)
        {
            setImage(dieRight[1]);
        }
        else if(frame == 5)
        {
        }
        else if(frame == 6)
        {
        }
        else if(frame == 7)
        {
            setImage(dieRight[2]);
            getImage().setTransparency(240);
        }
        else if(frame == 8)
        {
            setImage(dieRight[2]);
            getImage().setTransparency(200);
        }
        else if(frame == 9)
        {
            setImage(dieRight[2]);
            getImage().setTransparency(170);
        }
        else if(frame == 10)
        {
            removeAnimation();
            frame = 0;
            return;
        }
    }

    public void animateHitFaceLeft()
    {
        if(frame == 1)
        {
            setImage(HitFaceLeft[0]);
            return;
        }

        else if(frame == 7)
        {
            frame = 0;
            return;
        }

    }

    public void animateHitFaceRight()
    {
        if(frame == 1)
        {
            setImage(HitFaceRight[0]);
            return;
        }

        else if(frame == 7)
        {
            frame = 0;
            return;
        }

    }

    public void  removeAnimation()
    {
        getWorld().removeObject(this);
    }

    /*************
     *
     *
     *
     *
     *BEHAVIOUR
     *
     *
     *
     **********/

    public void checkTimer()   
    {  
        if(behaviourTimer == 0)  
        {  
            behaviourTimer();  
        }  
        actionRandomiser();  
    }  

    public void behaviourTimer()  
    {  
        behaviourTimer =  Greenfoot.getRandomNumber(400) + 100; //  you may want a larger number here  
        actionRandomiser = Greenfoot.getRandomNumber(3);  
    }  

    public void actionRandomiser()  
    {  
        switch (actionRandomiser)  
        {  
            case 0:  
            speed = 0;  
            sequence = 0;  
            break;  
            case 1:  
            speed = 4;  
            sequence = 2;  
            moveRight();  
            richting = 2;  
            break;  
            case 2:  
            speed = 4;  
            sequence = 1;  
            moveLeft();  
            richting = 1;  
            break;  
            case 3:  
            speed = 0;  
            sequence = 3;
            break;
        }  
    }  

    public void moveRight()
    {
        setLocation(getX()+speed, getY());
    }

    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
    }

    //HIT
    /**
     * 
     */
    public void hit(double damage) {
        hp = hp - damage;
        if(hp <= 0) 
        {
            actionRandomiser = 3;
            createLoot();
            behaviourTimer = behaviourTimer + 200;
            //Greenfoot.playSound("RibbonPig_Die.wav");
            setAlive(false);
            rogue.addRogueExperience(pigGivenExperience);
        }
        else 
        {
            //--life
            if ( damage >= 150)
            {
                sequence = 4;
                speed = 0;
                frame = 0;
                behaviourTimer = behaviourTimer + 200;
            }
        } 
    }

    //Loot Generator
    public void createLoot()
    {
        dropCalculator = Greenfoot.getRandomNumber(100);
        throwingstarGenerator = Greenfoot.getRandomNumber(100);
        if (dropCalculator >= 70)
        {
            mesoValue = Greenfoot.getRandomNumber(100) + 50;
            getWorld().addObject(new Meso(mesoValue), getX(), getY());
            //Greenfoot.playSound("DropItem.wav");
        }
        else if (dropCalculator <= 3)
        {
            getWorld().addObject(new Claw1(), getX(), getY());
            //Greenfoot.playSound("DropItem.wav");
        }

        else if (throwingstarGenerator <=2  )
        {
            getWorld().addObject(new WolbiDrop(), getX(), getY());
            //Greenfoot.playSound("DropItem.wav");
        }
    }

    /**
     * SPRITES INLADEN
     **/
    //Idle left
    GreenfootImage[] idleLeft = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_left_stand_0.png"),
            new GreenfootImage("ribbonPig_left_stand_1.png"),
            new GreenfootImage("ribbonPig_left_stand_2.png")
        };

    //Idle right   
    GreenfootImage[] idleRight = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_right_stand_0.png"),
            new GreenfootImage("ribbonPig_right_stand_1.png"),
            new GreenfootImage("ribbonPig_right_stand_2.png")
        };   

    //Naar links lopen
    GreenfootImage[] animateLeft = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_left_move_0.png"),
            new GreenfootImage("ribbonPig_left_move_1.png"),
            new GreenfootImage("ribbonPig_left_move_2.png")
        };
    //naar Rechts lopen    
    GreenfootImage[] animateRight = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_right_move_0.png"),
            new GreenfootImage("ribbonPig_right_move_1.png"),
            new GreenfootImage("ribbonPig_right_move_2.png")
        };

    GreenfootImage[] dieLeft = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_left_die1_0.png"),
            new GreenfootImage("ribbonPig_left_die1_1.png"),
            new GreenfootImage("ribbonPig_left_die1_2.png")
        };

    GreenfootImage[] dieRight = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_right_die1_0.png"),
            new GreenfootImage("ribbonPig_right_die1_1.png"),
            new GreenfootImage("ribbonPig_right_die1_2.png")
        };

    GreenfootImage[] HitFaceLeft = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_left_hit1_0.png")
        };
    GreenfootImage[] HitFaceRight = new GreenfootImage[]
        {
            new GreenfootImage("ribbonPig_right_hit1_0.png")

        };
}
danpost danpost

2013/10/31

#
The 'checkFloor' method only lets you know if you are on an end floor; but does not let you know which end. So, your actor needs more information to determine which way to go. The simple solution is to ask whether the x location of the actor is greater than or less than half the width of the world.
if (checkFloor())
{
    if (getX() < getWorld().getWidth()/2) richting = 1; else richting = 2;
}
I may have '1' and '2' arranged incorrectly; but, the main idea should be apparent.
You need to login to post a reply.