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

2015/9/9

player won't move

hoothoot hoothoot

2015/9/9

#
why doesn't my actor move? how do i fix it?
   import greenfoot.*;

/**
 * Write a description of class Gohan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Gohan extends Actor
{
     private int health = 900000; //its over 9000   
    public void setHealth(int points) {
        health += points;  
    }
    public int getHealth() {
        return health;
    }
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6; 
    private GreenfootImage image7;
    private GreenfootImage image8;
    public Gohan() 
    {
        image1 = new GreenfootImage("gohan.png"); 
        image2 = new GreenfootImage("gohanstep.png");
        image3 = new GreenfootImage("gohanstep2.png");
        image4 = new GreenfootImage("powerattack.png");
        image5 = new GreenfootImage("gohanpunch.png");
        image6 = new GreenfootImage("Gohankick1.png");
        image7 = new GreenfootImage("HealthBar.png");
        image8 = new GreenfootImage("Gpowerup.png");
        setImage(image1); 
    }/**
     * Act - do whatever the Gohan wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeypress ();
}
    public void checkKeypress() 
    {
        if (Greenfoot.isKeyDown("left"))
        {
           if ( getImage() == image1 ) 
            {   setImage(image2);
            }
            else
                {if ( getImage() == image8 ){}
                 else{   setImage(image1);
        }
            move(-30);
        }
        if (Greenfoot.isKeyDown("right"))
            {   if ( getImage() == image1 ) 
                {       setImage(image2);
                }
                
                 else{   setImage(image1);
                    
        }
            move (30);
        }
        if (Greenfoot.isKeyDown("s"))
        {
                setImage(image3);
                hitEnemy();
        }
        else if ( getImage() == image3)
        {
            setImage(image1);}
                if (Greenfoot.isKeyDown("z"))
        {
                setImage(image3);
                hitEnemy();
        }
                if (Greenfoot.isKeyDown("x"))
        {
                setImage(image6);
                hitEnemy();
        }
            else if ( getImage()== image6)
            {setImage(image1);}
                if (Greenfoot.isKeyDown("a"))
        {   
                setImage(image5);
                hitEnemy();
        }
        else if ( getImage()== image5)
            {setImage(image1);}
                if (Greenfoot.isKeyDown("space"))
        {   
                setImage(image4);
                hittingEnemy();
        }
        else if ( getImage() == image4 )
            {setImage(image1);}
        if (health<=0)
        {getWorld().removeObject(this);
        } 
        }

   }
        private void hitEnemy() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
            cell.setHealth(-1001);
            cell.move(30);
        } 
    }
        private void hittingEnemy() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
            cell.setHealth(-4000);
            cell.move(200);
        }
    }      
            private void celldies() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
           if (cell.health <=0){ setImage(image8);}
    }  
}
}

sorry if its messy i did my best to clean it up for you guys i was also wondering how to change my image after cell dies. the last void doesn't seem to be working so maybe I've just been putting it in the wrong spot. thanks in advance!
hoothoot hoothoot

2015/9/9

#
i think i see why he isn't moving and fixed it but why won't he punch and kick? (images3-6)
danpost danpost

2015/9/9

#
It appears that all the code within the 'checkKeys' method is in the first 'if' block, meaning that the "left" key must be down for any code in that method to execute.
hoothoot hoothoot

2015/9/10

#
thanks danpost. do you know a code where i can set this image if cell dies?
danpost danpost

2015/9/10

#
hoothoot wrote...
thanks danpost. do you know a code where i can set this image if cell dies?
Well, I do not see where the 'celldies' method is reachable. It is not being called at all within the class (at least, not that I see -- hope I did not miss it).
hoothoot hoothoot

2015/9/10

#
do you know of a code where I could make the image change when he dies? I can't seem to figure out how I would do that. the "celldies" method wasn't working last I checked, so I took it out. I'm going to delete the code if I find one that works.
hoothoot hoothoot

2015/9/10

#
 private void celldies() {
        Cell cell = (Cell) getOneObjectAtOffset(0, 0, Cell.class);
        if (cell != null) {
           if (cell.health <=0){ setImage(image8);}
that wasn't working so I's taking it out. do you know how I would change my other object's image when cell dies?
hoothoot hoothoot

2015/9/10

#
just clarifying
danpost danpost

2015/9/10

#
hoothoot wrote...
<Code Omitted> that wasn't working so I's taking it out. do you know how I would change my other object's image when cell dies?
It cannot work unless it is called to begin with (it was probably within the 'if' block and was not being called with everything else).
You need to login to post a reply.