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

2019/5/29

Zombie game

1
2
Anna_p Anna_p

2019/5/29

#
Hello everyone, i am trying to make a simple zombie survival game but I can't seem to make my character recognize objects correctly in the world. It stops in front of them but when try to make him move again everything goes wrong and I don't know what's wrong.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class homme here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class homme extends Actor
{
    GreenfootImage homme1 = new GreenfootImage("Surviver1.png");
    GreenfootImage homme2 = new GreenfootImage("Surviver2.png");
    
    public int frame = 1;
    private int imageCounter;
    
    
    /**
     * Act - do whatever the homme wants to do. This method is called whenever
       * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        
        
    }    
    
    
        public void move(){
        
        int y = getY();
        int x = getX();

        if(Greenfoot.isKeyDown("up"))
        {
            
                y--;
                imageCounter = (++imageCounter)%50; 
                if (imageCounter == 0) animate();
                if (isTouching(brick.class))y = y+3;
        }
            
        if(Greenfoot.isKeyDown("down"))
        {
            y++;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))y = y-3;
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            x--;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))x = x+3;
        }
        
        if(Greenfoot.isKeyDown("right"))
        {  
            x++;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))x = x-3;
        }
        setLocation(x,y);
    }
    
    
    public void animate()
    {
        if(frame == 1)
        {
            setImage(homme1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage(homme2);
            frame=1;
        }
    
    
    }
    
    
    
    
    
}
    
    
    
    
    
    
    
danpost danpost

2019/5/29

#
Looks like you are checking for bricks before you character has even moved.
Anna_p Anna_p

2019/5/29

#
danpost wrote...
Looks like you are checking for bricks before you character has even moved.
But it does except that once its in front of the bricks it stops and doesn't move.
danpost danpost

2019/5/29

#
Anna_p wrote...
But it does except that once its in front of the bricks it stops and doesn't move.
Please, if you could, upload the scenario with source so we can see what you are working with. It is important to test it as is because of your animation.
Anna_p Anna_p

2019/5/30

#
danpost wrote...
Anna_p wrote...
But it does except that once its in front of the bricks it stops and doesn't move.
Please, if you could, upload the scenario with source so we can see what you are working with. It is important to test it as is because of your animation.
I have unpoaded the scenario to my profile (i don't know if that's what you meant) Thank you for taking the time to look at it.
danpost danpost

2019/5/30

#
Anna_p wrote...
I have unpoaded the scenario to my profile (i don't know if that's what you meant) Thank you for taking the time to look at it.
Yes, but you missed one thing.
danpost wrote...
upload the scenario with source
You need to basically do it again (this time it should update it), but this time check the Publish source code box.
Anna_p Anna_p

2019/5/30

#
danpost wrote...
Anna_p wrote...
I have unpoaded the scenario to my profile (i don't know if that's what you meant) Thank you for taking the time to look at it.
Yes, but you missed one thing.
danpost wrote...
upload the scenario with source
You need to basically do it again (this time it should update it), but this time check the Publish source code box.
I updated it with the source code.
danpost danpost

2019/5/30

#
Anna_p wrote...
But it does except that once its in front of the bricks it stops and doesn't move.
No. Your code only moves the actor after checking for obstacles. Add the following line after lines 38, 46, 54 and 62 above:
setLocation(x, y);
Anna_p Anna_p

2019/5/30

#
danpost wrote...
Anna_p wrote...
But it does except that once its in front of the bricks it stops and doesn't move.
No. Your code only moves the actor after checking for obstacles. Add the following line after lines 38, 46, 54 and 62 above:
setLocation(x, y);
I did as you've said but as you can see in the updated version that I posted, the glitch continues to happen.
danpost danpost

2019/5/30

#
Anna_p wrote...
I did as you've said but as you can see in the updated version that I posted, the glitch continues to happen.
Do you mean the bobbling back and forth when against an obstacle?
Anna_p Anna_p

2019/5/30

#
No. When the character finds an obstacle it stops in front of it (like it should) but when you move again the commands work in the opposite way (the left arrow makes it go right and the other way around) you kind of have to play with it for you to see it happen.
danpost danpost

2019/5/30

#
Anna_p wrote...
No. When the character finds an obstacle it stops in front of it (like it should) but when you move again the commands work in the opposite way (the left arrow makes it go right and the other way around) you kind of have to play with it for you to see it happen.
I am not getting that behavior. Please show your current Homme class codes.
Anna_p Anna_p

2019/5/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class homme here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class homme extends Actor
{
    GreenfootImage homme1 = new GreenfootImage("Surviver1.png");
    GreenfootImage homme2 = new GreenfootImage("Surviver2.png");
    
    public int frame = 1;
    private int imageCounter;
    
    
    /**
     * Act - do whatever the homme wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        atWorldEdge();

        
        
    }    
    
    
        public void move(){
        
        int y = getY();
        int x = getX();

        if(Greenfoot.isKeyDown("up"))
        {
            
                setLocation(x,y);
                y--;
                imageCounter = (++imageCounter)%50; 
                if (imageCounter == 0) animate();
                if (isTouching(brick.class))y = y+3;
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(x,y);
            y++;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))y = y-3;
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(x,y);
            x--;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))x = x+3;
        }
        
        if(Greenfoot.isKeyDown("right"))
        {  
            setLocation(x,y);
            x++;
            imageCounter = (++imageCounter)%50; 
            if (imageCounter == 0) animate();
            if (isTouching(brick.class))x = x-3;
        }
        setLocation(x,y);
    }
    
    public void animate()
    {
        if(frame == 1)
        {
            setImage(homme1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage(homme2);
            frame=1;
        }
    
    
    }
    
    public boolean atWorldEdge()
    {
        if(getX() < 1 || getX() > getWorld().getWidth() - 1)
            return true;
        if(getY() < 1 || getY() > getWorld().getHeight() - 1)
            return true;
        else
            return false;
        
    
    
    }
    
    
    
    
}
    
    
    
    
    
    
    
danpost danpost

2019/5/30

#
Anna_p wrote...
<< Code Omitted >>
You did not do as instructed.
danpost wrote...
Add the following line after lines 38, 46, 54 and 62 above:
setLocation(x, y);
Anna_p Anna_p

2019/5/30

#
OOPS. my bad! I got the wrong line and i guess tha'ts why it didn't work. Thank you!
There are more replies on the next page.
1
2