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

2017/1/18

Check Boundaries heelp

Hondrok Hondrok

2017/1/18

#
Sooo... when I press space I want the actor to periodically shoot spheres. Can somebody help me with this? Actor's code
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkKeys();
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
    }
    
    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; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Goku_idle.png");
           }
        }
        else
        {
            setImage("Goku_jump.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 hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            getImage().setTransparency(0);
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_2());
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_3());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Goku_move_right.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Goku_move_left.png");
    }
     
    public void attack()
    {
        if(Greenfoot.isKeyDown("space")) 
        {
             getWorld().addObject(new Sphere_right(), getX(), getY());
        }
    }
}
Sphere's code
import greenfoot.*;

public class Sphere_right extends Actor
{
   private int speed = 10;
   
   public void act()
   {
       setLocation(getX() + speed, getY());
  
       checkBoundaries();
       return;
   }
   
   public void checkBoundaries()
   {
       if(getX() > getWorld().getWidth() - 5) 
       {
            getWorld().removeObject(this);
            return;
        }
       else if(getX() < 5) 
       {
            getWorld().removeObject(this);
            return;
        }
       
        if(getY() > getWorld().getHeight() - 5) 
       {
            getWorld().removeObject(this);
            return;
        }
       else if(getY() < 5) 
       {
            getWorld().removeObject(this);
            return;
        }
        Actor enemy = getOneIntersectingObject(Goomba.class);
       if(enemy != null) 
       {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
            return;
       }
   }

   
}
Super_Hippo Super_Hippo

2017/1/18

#
Add an int variable which you can use as a timer. Set it to some number whenever a shot is fired. Each act cycle, you decrease that number if it above 0 and only if it is 0, you can shoot. (Obviously you can also increase it and do it the other way around.)
You need to login to post a reply.