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

2020/3/26

Why can't I shoot left even when the character turns left?

Shrace Shrace

2020/3/26

#
my character is meant to shoot both left and right. i have successfully created it to shoot right, but even when the character turns left he doesn't shoot left. the bubbles (bullet) keep shooting right, even as he moves left. Pleas help someone.. it for my AP exams and its due very soon.
Shrace Shrace

2020/3/26

#
public class avatar extends Actor
{
    private int speed = 6;
    private int verSpeed = 0;
    private int increase = 2;
    private int jumpstrength = 12;
    private boolean jumping;
    boolean touchingenemy = false;
    boolean touchingfood = false;
    boolean formula = false;
    int count = 0;
    
    
    /**
     * Act - do whatever the avatar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        checkfall();
        ceiling();
        eatfood();
        hitenemy();
        Fire();
            
    } 
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
           setImage("spongeleft.png");           
           moveLeft();
        }
        if (Greenfoot.isKeyDown("right"))
        {
           setImage("spongeright.png");
           moveRight();
        }
        if (Greenfoot.isKeyDown("up"))
        {
           jump();
        }
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
    
    public void fall()
    {
        setLocation ( getX(), getY() + verSpeed);
        verSpeed = verSpeed + increase;
        jumping = true;
    }
    
    public void jump()
    {
        verSpeed = - jumpstrength;
        jumping = true;
        fall();
    }
    
    public void movetoground(Actor log)
    {
        int logHeight = log.getImage().getHeight();
        int newY = log.getY() - (logHeight +getImage().getHeight())/2;
        
        setLocation(getX(), newY); 
        
    }
    
    public void checkfall()
    {
        if(onground()) {
            verSpeed = 0;
        }
        else{
            fall();
        }
        
    }
    
    public boolean onground()
    {
        int avatarHeight = getImage().getHeight();
        int lookforground = (int) (avatarHeight/2) + 5;
        
        Actor ground = getOneObjectAtOffset ( 0, getImage().getHeight()/2, log.class);
        if(ground == null){
            jumping = true;
            return false;
        }
        else{
            movetoground(ground);
            return true;
        }
         
    
       
    }
    
    public boolean ceiling()
    {
        int avatarHeight = getImage().getHeight();
        int Ydistance = (int) (avatarHeight / -2);
        
        Actor ceiling = getOneObjectAtOffset ( 0, Ydistance, log.class);
        if(ceiling != null){
            
            verSpeed =1;
            bopHead(ceiling);
            return true;
        }
        else{
           
            return false;
        }
        
    }
    
    public void bopHead(Actor ceiling)
    {
       int ceilingHeight = ceiling.getImage().getHeight();
       int newY = ceiling.getY() + (ceilingHeight +getImage().getHeight())/2;
        
        setLocation(getX(), newY); 
        
    }
    
    public void hitenemy() 
    {
        Actor enemy = getOneIntersectingObject(enemy.class);
        if (enemy != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            healthbar healthbar = myworld.gethealthbar();
            if(touchingenemy == false)
            {
               healthbar.losehealth();
               touchingenemy = true;
               if(healthbar.health<= 0)
                {
                   myWorld.removeObject(this);
                   gameover mygameover = new gameover(); 
                   Greenfoot.setWorld(mygameover);
                }
            }
        } else
        {
            touchingenemy = false;
        }
    } 
    
    public void Fire() 
    {
        if(Greenfoot.isKeyDown("space"))
        {
            bubble bubbles = new bubble();
            World myWorld = getWorld();
            myWorld.addObject(new bubble(getRotation()), getX(), getY());            
            bubbles.setLocation(getX(), getY());
            bubbles.setRotation(getRotation());
          
        }
    } 
    
    public void eatfood() 
    {
        Actor food = getOneIntersectingObject(food.class);
        if (food != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            healthbar healthbar = myworld.gethealthbar();
            if(touchingfood == false)
           {
             healthbar.gainhealth();
             touchingfood = true;
           }
        } else
        {
            touchingfood = false;
        }
    }
    

        
   }

danpost danpost

2020/3/27

#
Shrace wrote...
Why can't I shoot left even when the character turns left?
Because you are not turning the character to face left (you are just changing its image to one that faces left).
You need to login to post a reply.