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

2013/11/8

Having issues with my shoot() method

PrezHawkeye PrezHawkeye

2013/11/8

#
my actor is controlled by the basic up, down, left, right and space buttons. When he moves left his image flips to face the left as he moves left however when he will only shoot right which poses an issue. He is only supposed to shoot left or right, never up or down. these are my checkKeypress and shoot methods. How do I get my actor to shoot to the left? public void checkKeypress() { if (Greenfoot.isKeyDown("space")) { setImage(aubieShoot); shoot(); } if(!Greenfoot.isKeyDown("space") && !Greenfoot.isKeyDown("left")) { setImage(aubieRight); } if (Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("left")) { setImage(aubieShootLeft); moveLeft(); shoot(); } if (!Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("left")) { setImage(aubieLeft); moveLeft(); } if (Greenfoot.isKeyDown("right")) { moveRight(); } if (Greenfoot.isKeyDown("up")) { moveUp(); } if (Greenfoot.isKeyDown("down")) { moveDown(); } shotsFired += 1; } public void shoot() { if (shotsFired >= 8) { Bullets bullets = new Bullets(); getWorld().addObject(bullets, getX() + getImage().getWidth(), getY()); shotsFired = 0; } }
shrucis1 shrucis1

2013/11/8

#
Add a constructor to the Bullet Class, then whenever you shoot a bullet, do it like this: getWorld().addObject(new Bullets(getRotation()), getX() + getImage().getWidth(), getY());
danpost danpost

2013/11/8

#
Instead of just flipping the image horizontally, rotate it 180 and flip it vertically.
PrezHawkeye PrezHawkeye

2013/11/8

#
Hmmm adding a constructor and flipping the image helped to get it to shoot to the left. however my character now basically barrel rolls across the screen and gets stuck shooting left upside down
danpost danpost

2013/11/8

#
Please, show the current code you are using; and use the 'code' tag below the 'Post a reply' box to insert your code with.
PrezHawkeye PrezHawkeye

2013/11/8

#
 public class Bullets extends Characters
{
    private GreenfootImage bullet;
    
    public Bullets(int rotation)
    {
        bullet = getImage();
        bullet.scale(20, 8);
        setRotation(rotation);
        
    }
    
    public void act() 
    {
        move();
    }    
    
    private void move()
    {
        if(isTouching(Wall.class))
        {
            remove(this);
        }
        
        else
        {
            run(getX(), getY());
        }
    }
}

/**
     * Checks for a certain key before it can perform an action 
     * (i.e. left arrow key moves Aubie to the left)
     */
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("space"))
        {
           setImage(aubieShoot);
           //aubieShoot.mirrorVertically();
           shoot();
        }
        
         if (!Greenfoot.isKeyDown("space") && !Greenfoot.isKeyDown("left"))
        {
           setImage(aubieRight);
        }
        
        if (Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("left"))
        {
            this.setImage(aubieShoot);
            this.setRotation(180);
            //aubieShoot.mirrorVertically();
            shoot();
            moveLeft();
        }
        
        if (!Greenfoot.isKeyDown("space") && Greenfoot.isKeyDown("left"))
        {
            this.setImage(aubieRight);
            this.setRotation(180);
            //aubieRight.mirrorVertically();
            moveLeft();
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
            
            moveRight();
        }
        
        if (Greenfoot.isKeyDown("up"))
        {
            moveUp();
        }
        
        if (Greenfoot.isKeyDown("down"))
        {
            moveDown();
        }
        
        shotsFired += 1;
        
    }



/**
     * Controls Aubie's shooting function.
     */
    public void shoot()
    {
        if (shotsFired >= 8) 
        {
            Bullets bullets = new Bullets(getRotation());
            bullets.setRotation(getRotation());
            
            
            getWorld().addObject(bullets, getX() + getImage().getWidth(), getY());
            shotsFired = 0;
        }
    
    }
danpost danpost

2013/11/8

#
Why do you have the bullets creating bullets? If Aubie is the character that shoots, then the bullets should be created there.
PrezHawkeye PrezHawkeye

2013/11/9

#
? I was setting the image for bullets
PrezHawkeye PrezHawkeye

2013/11/9

#
well the scale of the bullets at least
danpost danpost

2013/11/9

#
How many images for the bullets do you have and what are they (or why do you need more than one)?
danpost danpost

2013/11/9

#
PrezHawkeye wrote...
well the scale of the bullets at least
The only place I see you scaling the bullet image is at line 8 (which is OK). I was referring to ... OH. I see. Is everything from line 31 on in the Aubie class?
PrezHawkeye PrezHawkeye

2013/11/9

#
Yes sorry I thought I separated it by class
danpost danpost

2013/11/9

#
At line 41: put 'if (getRotation() == 180) getImage().mirrorVertically(); At lines 54, and 63: put 'getImage().mirrorVertically(); At line 100: this needs adjusted as you are always adding the width of Aubie to the x location and never subtracting it; better would be to use 'move' with an argument of half the width of the image of Aubie.
You need to login to post a reply.