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

2017/2/5

Animating Attack

1
2
Hondrok Hondrok

2017/2/5

#
When I press space I want the actor to make the specific attack animation and shoot the sphere but unfortunately it gets stuck at the first image
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 6;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
    
    private int frame = 0;
    private int attFrame = 0;
    
    private int animationCounter = 0;
    
    private GreenfootImage fr0 = new GreenfootImage("Mario_fr0.png");
    private GreenfootImage fr1 = new GreenfootImage("Mario_fr1.png");
    private GreenfootImage fr2 = new GreenfootImage("Mario_fr2.png");
    private GreenfootImage fr3 = new GreenfootImage("Mario_fr3.png");
    private GreenfootImage fr4 = new GreenfootImage("Mario_fr4.png");
    private GreenfootImage fr5 = new GreenfootImage("Mario_fr5.png");
    private GreenfootImage fr6 = new GreenfootImage("Mario_fr6.png");
    
    private GreenfootImage fr0m = new GreenfootImage("Mario_fr0m.png");
    private GreenfootImage fr1m = new GreenfootImage("Mario_fr1m.png");
    private GreenfootImage fr2m = new GreenfootImage("Mario_fr2m.png");
    private GreenfootImage fr3m = new GreenfootImage("Mario_fr3m.png");
    private GreenfootImage fr4m = new GreenfootImage("Mario_fr4m.png");
    private GreenfootImage fr5m = new GreenfootImage("Mario_fr5m.png");
    private GreenfootImage fr6m = new GreenfootImage("Mario_fr6m.png");
    
    private GreenfootImage att_fr0m = new GreenfootImage("Mario_att_fr0m.png");
    private GreenfootImage att_fr1m = new GreenfootImage("Mario_att_fr1m.png");
    private GreenfootImage att_fr2m = new GreenfootImage("Mario_att_fr2m.png");
    
    private GreenfootImage att_fr0 = new GreenfootImage("Mario_att_fr0.png");
    private GreenfootImage att_fr1 = new GreenfootImage("Mario_att_fr1.png");
    private GreenfootImage att_fr2 = new GreenfootImage("Mario_att_fr2.png");
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
       animationCounter ++;
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            setImage("Mario_fr0.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(fr0);
        }
        else if(frame == 1) 
        {
            setImage(fr1);
        }
        else if(frame == 2) 
        {
            setImage(fr2);
        }
        else if(frame == 3) 
        {
            setImage(fr3);
        }
        else if(frame == 4) 
        {
            setImage(fr4);
        }
        else if(frame == 5) 
        {
            setImage(fr5);
        }
        else if(frame == 6) 
        {
            setImage(fr6);
            frame = 1;
            return;
        }
        
        frame++;
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateLeft();
        }
    }
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(fr0m);
        }
        else if(frame == 1) 
        {
            setImage(fr1m);
        }
        else if(frame == 2) 
        {
            setImage(fr2m);
        }
        else if(frame == 3) 
        {
            setImage(fr3m);
        }
        else if(frame == 4) 
        {
            setImage(fr4m);
        }
        else if(frame == 5) 
        {
            setImage(fr5m);
        }
        else if(frame == 6) 
        {
            setImage(fr6m);
            frame = 1;
            return;
        }
        
        frame++;
    }
    
    public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            animateAttack();
        }
    }

    public void animateAttack()
    {
        if(attFrame == 0)
        {
            setImage(att_fr0);
        }
        else if(attFrame == 1) 
        {
            setImage(att_fr1);
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
        }
        else if(frame == 2) 
        {
            setImage(att_fr2);
            frame = 1;
            return;
        }
        
        frame++;
    }
    
    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
}
Super_Hippo Super_Hippo

2017/2/5

#
Change 'frame' in lines 221 and 225 to 'attFrame'. Btw, you set the counters back to 1 and not to 0. Is that wanted?
Hondrok Hondrok

2017/2/5

#
Super_Hippo wrote...
Change 'frame' in lines 221 and 225 to 'attFrame'. Btw, you set the counters back to 1 and not to 0. Is that wanted?
Still not working
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 6;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
    
    private int frame = 0;
    private int attFrame = 0;
    
    private int animationCounter = 0;
    
    private GreenfootImage fr0 = new GreenfootImage("Mario_fr0.png");
    private GreenfootImage fr1 = new GreenfootImage("Mario_fr1.png");
    private GreenfootImage fr2 = new GreenfootImage("Mario_fr2.png");
    private GreenfootImage fr3 = new GreenfootImage("Mario_fr3.png");
    private GreenfootImage fr4 = new GreenfootImage("Mario_fr4.png");
    private GreenfootImage fr5 = new GreenfootImage("Mario_fr5.png");
    private GreenfootImage fr6 = new GreenfootImage("Mario_fr6.png");
    
    private GreenfootImage fr0m = new GreenfootImage("Mario_fr0m.png");
    private GreenfootImage fr1m = new GreenfootImage("Mario_fr1m.png");
    private GreenfootImage fr2m = new GreenfootImage("Mario_fr2m.png");
    private GreenfootImage fr3m = new GreenfootImage("Mario_fr3m.png");
    private GreenfootImage fr4m = new GreenfootImage("Mario_fr4m.png");
    private GreenfootImage fr5m = new GreenfootImage("Mario_fr5m.png");
    private GreenfootImage fr6m = new GreenfootImage("Mario_fr6m.png");
    
    private GreenfootImage att_fr0m = new GreenfootImage("Mario_att_fr0m.png");
    private GreenfootImage att_fr1m = new GreenfootImage("Mario_att_fr1m.png");
    private GreenfootImage att_fr2m = new GreenfootImage("Mario_att_fr2m.png");
    
    private GreenfootImage att_fr0 = new GreenfootImage("Mario_att_fr0.png");
    private GreenfootImage att_fr1 = new GreenfootImage("Mario_att_fr1.png");
    private GreenfootImage att_fr2 = new GreenfootImage("Mario_att_fr2.png");
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
       animationCounter ++;
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            setImage("Mario_fr0.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(fr0);
        }
        else if(frame == 1) 
        {
            setImage(fr1);
        }
        else if(frame == 2) 
        {
            setImage(fr2);
        }
        else if(frame == 3) 
        {
            setImage(fr3);
        }
        else if(frame == 4) 
        {
            setImage(fr4);
        }
        else if(frame == 5) 
        {
            setImage(fr5);
        }
        else if(frame == 6) 
        {
            setImage(fr6);
            frame = 1;
            return;
        }
        
        frame++;
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateLeft();
        }
    }
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(fr0m);
        }
        else if(frame == 1) 
        {
            setImage(fr1m);
        }
        else if(frame == 2) 
        {
            setImage(fr2m);
        }
        else if(frame == 3) 
        {
            setImage(fr3m);
        }
        else if(frame == 4) 
        {
            setImage(fr4m);
        }
        else if(frame == 5) 
        {
            setImage(fr5m);
        }
        else if(frame == 6) 
        {
            setImage(fr6m);
            frame = 1;
            return;
        }
        
        frame++;
    }
    
    public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            animateAttack();
        }
    }

    public void animateAttack()
    {
        if(attFrame == 0)
        {
            setImage(att_fr0);
        }
        else if(attFrame == 1) 
        {
            setImage(att_fr1);
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
        }
        else if(attFrame == 2) 
        {
            setImage(att_fr2);
            frame = 0;
            return;
        }
        
        frame++;
    }
    
    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
}
Nosson1459 Nosson1459

2017/2/5

#
Lines 221 and 225 say "frame" instead of "attFrame".
Super_Hippo Super_Hippo

2017/2/5

#
Did you change anything? Oh, you changed 1 to 0. You missed the variable name though.
Nosson1459 Nosson1459

2017/2/5

#
Only line 225 was changed to 0 but not the main part of attFrame and frame.
Hondrok Hondrok

2017/2/5

#
Super_Hippo wrote...
Did you change anything? Oh, you changed 1 to 0. You missed the variable name though.
Nosson1459 wrote...
Only line 225 was changed to 0 but not the main part of attFrame and frame.
Still not working
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 6;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    public int armour = 3;
    
    private int frame = 0;
    private int attFrame = 0;
    
    private int animationCounter = 0;
    
    private GreenfootImage fr0 = new GreenfootImage("Mario_fr0.png");
    private GreenfootImage fr1 = new GreenfootImage("Mario_fr1.png");
    private GreenfootImage fr2 = new GreenfootImage("Mario_fr2.png");
    private GreenfootImage fr3 = new GreenfootImage("Mario_fr3.png");
    private GreenfootImage fr4 = new GreenfootImage("Mario_fr4.png");
    private GreenfootImage fr5 = new GreenfootImage("Mario_fr5.png");
    private GreenfootImage fr6 = new GreenfootImage("Mario_fr6.png");
    
    private GreenfootImage fr0m = new GreenfootImage("Mario_fr0m.png");
    private GreenfootImage fr1m = new GreenfootImage("Mario_fr1m.png");
    private GreenfootImage fr2m = new GreenfootImage("Mario_fr2m.png");
    private GreenfootImage fr3m = new GreenfootImage("Mario_fr3m.png");
    private GreenfootImage fr4m = new GreenfootImage("Mario_fr4m.png");
    private GreenfootImage fr5m = new GreenfootImage("Mario_fr5m.png");
    private GreenfootImage fr6m = new GreenfootImage("Mario_fr6m.png");
    
    private GreenfootImage att_fr0m = new GreenfootImage("Mario_att_fr0m.png");
    private GreenfootImage att_fr1m = new GreenfootImage("Mario_att_fr1m.png");
    private GreenfootImage att_fr2m = new GreenfootImage("Mario_att_fr2m.png");
    
    private GreenfootImage att_fr0 = new GreenfootImage("Mario_att_fr0.png");
    private GreenfootImage att_fr1 = new GreenfootImage("Mario_att_fr1.png");
    private GreenfootImage att_fr2 = new GreenfootImage("Mario_att_fr2.png");
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
       animationCounter ++;
    }
    
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
        }
        else
        {
            setImage("Mario_fr0.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 0)
        {
            setImage(fr0);
        }
        else if(frame == 1) 
        {
            setImage(fr1);
        }
        else if(frame == 2) 
        {
            setImage(fr2);
        }
        else if(frame == 3) 
        {
            setImage(fr3);
        }
        else if(frame == 4) 
        {
            setImage(fr4);
        }
        else if(frame == 5) 
        {
            setImage(fr5);
        }
        else if(frame == 6) 
        {
            setImage(fr6);
            frame = 1;
            return;
        }
        
        frame++;
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        
        if(animationCounter % 4 == 0)
        {
            animateLeft();
        }
    }
    
    public void animateLeft()
    {
        if(frame == 0)
        {
            setImage(fr0m);
        }
        else if(frame == 1) 
        {
            setImage(fr1m);
        }
        else if(frame == 2) 
        {
            setImage(fr2m);
        }
        else if(frame == 3) 
        {
            setImage(fr3m);
        }
        else if(frame == 4) 
        {
            setImage(fr4m);
        }
        else if(frame == 5) 
        {
            setImage(fr5m);
        }
        else if(frame == 6) 
        {
            setImage(fr6m);
            frame = 1;
            return;
        }
        
        frame++;
    }
    
    public void attack()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            animateAttack();
        }
    }

    public void animateAttack()
    {
        if(attFrame == 0)
        {
            setImage(att_fr0);
        }
        else if(attFrame == 1) 
        {
            setImage(att_fr1);
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
        }
        else if(attFrame == 2) 
        {
            setImage(att_fr2);
            attFrame = 0;
            return;
        }
        
        attFrame++;
    }
    
    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
    
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
}
Super_Hippo Super_Hippo

2017/2/5

#
Note that if you need to hold space for some time (depending on the speed of your scenario). If you only click it for one act cycle, the animation won't continue on its own. If you already do this, add the following line in line 224 and report back the output when pressing (and holding) space for a bit. (No need to hold space forever and copy+paste thousands of lines.)
System.out.println(attFrame);
Hondrok Hondrok

2017/2/5

#
Super_Hippo wrote...
Note that if you need to hold space for some time (depending on the speed of your scenario). If you only click it for one act cycle, the animation won't continue on its own. If you already do this, add the following line in line 224 and report back the output when pressing (and holding) space for a bit. (No need to hold space forever and copy+paste thousands of lines.)
System.out.println(attFrame);
It still blocks and it doesn't show the attFrame even if I press it for some time
danpost danpost

2017/2/5

#
If you did a search in the Mario class for 'attFrame', you will find only 3 instances of it -- (1) where declared at line 12; (2) when check for equal to zero at line 209; and (3) where checked for equal to one at line 213. Nowhere is the value changing from its initially set value of zero.
Nosson1459 Nosson1459

2017/2/5

#
I found 6 instances of it. On line... 1) 12 - declaration 2) 209 - animation if 3) 213 - animation if 4) 218 - animation if 5) 221 - reset to 0 6) 225 - increase 1
Super_Hippo Super_Hippo

2017/2/5

#
Hondrok wrote...
It still blocks and it doesn't show the attFrame even if I press it for some time
What do you mean with block? Does the scenario stop on its own? Do you get an error message? Did you get any output at all?
danpost danpost

2017/2/5

#
Nosson1459 wrote...
I found 6 instances of it.
Maybe I was looking at the earlier code post for the class. For now, the best I think you can do without a total work-over is inserting the following line at line 46:
if (animationCounter % 4 == 0) setImage(R0);
Nosson1459 Nosson1459

2017/2/6

#
There is no image "R0".
danpost danpost

2017/2/6

#
Nosson1459 wrote...
There is no image "R0".
"fr0". I did not have access to your images; so, I had to use make-shift images and do some code adjustments for testing purposes.
There are more replies on the next page.
1
2