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

2012/5/24

Opposite running

solo1512 solo1512

2012/5/24

#
My enemy characters are running in the opposite direction and I don't know what's wrong. Here is the code, import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class shadow here. * * @author (your name) * @version (a version number or a date) */ public class Shadow extends Enemy { public int lifeCounter = 20; public boolean state; public int Srate = 6; /** * Act - do whatever the Rouge wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { die(); isKnucklesThere(); changeImage(); } public void die() { if(lifeCounter == 0) { getWorld().removeObject(this); } } public void killKnuckles() { Mobius Mobius = (Mobius) getWorld(); Knuckles Knuckles = Mobius.getKnuckles(); Knuckles.life=Knuckles.life-2; } public void isKnucklesThere() { Mobius Mobius = (Mobius) getWorld(); Knuckles Knuckles = Mobius.getKnuckles(); if(Knuckles.getY()==334) { attackKnuckles(); } else { setImage("Shadow.gif"); } } public void attackKnuckles() { Actor actor = getOneObjectAtOffset(0,0,Knuckles.class); if(actor != null) { killKnuckles(); changingAttack(); } } public void changeImage() { Mobius Mobius = (Mobius) getWorld(); Knuckles Knuckles = Mobius.getKnuckles(); if(getX()<Knuckles.getX()) { setImage("ShadowRunningLeft.gif"); state = false; move(-Srate); } if(getX()>Knuckles.getX()) { setImage("ShadowRunningRight.gif"); state = true; move(Srate); } } public void changingAttack() { if(state == false) { setImage("ShadowAttackLeft.gif"); } if(state == true) { setImage("ShadowAttackRight.gif"); } } }
ttamasu ttamasu

2012/5/24

#
Just a logical error if you are less than the x position of Knuckles ( you are to the right of Knuckles so switch the if statement to:
 if(getX()>Knuckles.getX())
        {
            setImage("ShadowRunningLeft.gif");
            state = false;
            move(-Srate);
        }
        if(getX()<Knuckles.getX())
        {
            setImage("ShadowRunningRight.gif");
            state = true;
            move(Srate);
        }
solo1512 solo1512

2012/6/6

#
if(Knuckles.isAlive==true&& etc...)
{
     etc....
}
Thanks but I worked everything out, I just had to add this code to every boolean.
You need to login to post a reply.