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

2015/8/15

Need Help with Projectiles!

1
2
AtomStation AtomStation

2015/8/15

#
In my platform game, I added a power up that allows you to use projectiles using the letter j. Here is the code I have used:
public void firePower()
    {
        if (getOneIntersectingObject(FireFlower.class) != null && songDuration <= 0)
        {
            getWorld().removeObject(getOneIntersectingObject(FireFlower.class));
            fireDuration = 600;
        }
    }
    
    public void fireProjectile()
    {
        if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0 && facingLeft == false)
        {
            getWorld().addObject(new Shot(), getX(), getY());
            shotDelay = 75;
            
            Greenfoot.playSound("FireShot.wav");
        } else if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0 && facingLeft == true) {
            getWorld().addObject(new Shot(), getX(), getY());
            shotDelay = 75;
            
            Greenfoot.playSound("FireShot.wav");
        }
I am trying to make the projectile fire in the direction that the character is facing. Is there anyway to do that? Thanks for reading!
Super_Hippo Super_Hippo

2015/8/15

#
public void fireProjectile()
{
    if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0)
    {
        Shot s = new Shot(); //creates a new Shot object
        s.setRotation(getRotation); //sets the rotation of the new Shot object to the same rotation as the shooting object
        getWorld().addObject(s, getX(), getY()); //adds the object to the world
        shotDelay = 75;
        Greenfoot.playSound("FireShot.wav");
    }
}
AtomStation AtomStation

2015/8/16

#
Do you know how I would set the variable getRotation?
danpost danpost

2015/8/16

#
'getRotation' is a method, not a variable, and requires an empty set of round brackets after it. Line 6 should be:
s.setRotation(getRotation());
AtomStation AtomStation

2015/8/16

#
Oh, I see. But my problem was that in the Shot class in the act method I had it set to move(10). Is there a way to change the way the shot class is facing according to which way the player class is facing?
danpost danpost

2015/8/16

#
The 'fireProjectile' method updated by Super_Hippo with the correction given by me should be sufficient in having it face the right direction. This is provided that the unrotated shot object faces to the right.
AtomStation AtomStation

2015/8/17

#
Strange... it doesn't work. Maybe it has to do with the code in my Shot class? Here is my code for the Shot class.
public void act() 
    {
        move(10);
        remove();
    }    
    
    public void remove()
    {
        if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
And here is the code for my Player class.
public void firePower()
    {
        if (getOneIntersectingObject(FireFlower.class) != null && songDuration <= 0)
        {
            getWorld().removeObject(getOneIntersectingObject(FireFlower.class));
            fireDuration = 500;
        }
    }

public void fireProjectile()
    {
        if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0)
        {
            Shot s = new Shot(); //creates a new Shot object
            s.setRotation(getRotation()); //sets the rotation of the new Shot object to the same rotation as the shooting object
            getWorld().addObject(s, getX(), getY()); //adds the object to the world
            shotDelay = 75;
            Greenfoot.playSound("FireShot.wav");
        }
    }
danpost danpost

2015/8/17

#
There is nothing wrong with the Shot class -- at least with that part which was given. The Player class code given appears alright as well (as far as the Shot object is concerned). You say "it doesn't work"; but, that is quite vague. How is it not working? Be as detailed and complete as possible.
AtomStation AtomStation

2015/8/18

#
Well, the only problem I have that I can tell is that it is not firing in the direction that the character is facing. Sorry for being too vague!
danpost danpost

2015/8/18

#
Do all shots move in the same direction? If so, which direction do the shots move in? If not, please describe the relationship between the direction the character is facing and the direction the shot is fired at.
AtomStation AtomStation

2015/8/19

#
All my shots move in the same direction. They move to the right. The direction does not change.
danpost danpost

2015/8/19

#
Please post your Shot class code for checking (the entire class).
AtomStation AtomStation

2015/8/19

#
Those were the only code I used. But just in case, here is the entire Shot class.
import greenfoot.*;

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shot extends Actor
{
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);
        remove();
    }    
    
    public void remove()
    {
        if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2015/8/19

#
Okay. That seems alright. Now the Player class, please.
AtomStation AtomStation

2015/8/19

#
The WHOLE thing? Okay its like 400 lines of code... but here it is...
import greenfoot.*;

/**
 * Write a description of class MarioJumpmanMario here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MarioJumpmanMario extends Actor
{
    private int vSpeed = 3;
    private int acceleration = 1;
    private int jumpStrength = 20;
    private int jumpDelay = 0;
    private int shotDelay = 0;
    
    private int songDuration = 0;
    private int fireDuration = 0;

    private GreenfootImage[] images;
    private int currentImage = 0;
    private int animationDelay = 0;
    private boolean facingLeft = false;

    private int invulnerability = 0;

    private static int coinCount = 0;

    public MarioJumpmanMario(String basename, String suffix, int noOfImages)
    {
        images = new GreenfootImage[noOfImages];

        for (int i=0; i < noOfImages; i++)
        {
            images[i] = new GreenfootImage(basename + i + suffix);
        }

        setImage(images[currentImage]);
    }

    /**
     * Act - do whatever the MarioJumpmanMario wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkFall();
        keyPressEvent();
        // movePlaces();
        respawn();
        
        getCoin();
        deathByBadCoin();
        
        jumpDelay--;
        shotDelay--;
        animationDelay--;
        
        songDuration--;
        fireDuration--;
        
        invulnerability--;
        godMode();
        
        starPowerUp();
        firePower();
        
        fireProjectile();
        // getWorld().removeObjects(getWorld().getObjects(StarredMarioMario.class));
        if (!onGround())
        {
            setImage("Mario0.png");

            if (facingLeft)
            {
                getImage().mirrorHorizontally();
            }
        }
    } 

    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        vSpeed += acceleration;

        // setImage("Mario0.png");

        if (vSpeed > 25) vSpeed = 25;
    }

    private void keyPressEvent()
    {
        cheatPressEvent();

        if (Greenfoot.isKeyDown("space") || (Greenfoot.isKeyDown("up") || (Greenfoot.isKeyDown("w"))))
        {

            if (onGround())

                jump();

        }

        if (Greenfoot.isKeyDown("shift") && onGround())
        {
            setLocation(getX(), getY() - 5);
        }

        if (Greenfoot.isKeyDown("6"))
        {
            getWorld().addObject(new Coin(), getX(), getY());
        }

        if (Greenfoot.isKeyDown("5"))
        {
            getWorld().removeObjects(getWorld().getObjects(BadCoin.class));
        }

        if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 5, getY());
            facingLeft = false;
            if (animationDelay <= 0 && onGround())
            {
                animate();
                animationDelay = 5;
            }
        } else if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) {
            setLocation(getX() - 5, getY());
            facingLeft = true;
            if (animationDelay <= 0 && onGround())
            {
                animate();
                animationDelay = 5;
            }
        } else {
            setImage("Mario2.png");
            if (facingLeft)
            {
                getImage().mirrorHorizontally();
            }
        }

        if (songDuration > 0)
        {
            starPressEvent();
        }
        
        if (Greenfoot.isKeyDown("control"))
        {
            setLocation(getX(), getY() - 5);
        }
    }

    public void animate()
    {
        if (Greenfoot.isKeyDown("left") || (Greenfoot.isKeyDown("a")))
        {
            currentImage = (currentImage + 1) % images.length;
            setImage(images[currentImage]);

            GreenfootImage reversed = new GreenfootImage(images[currentImage]);
            reversed.mirrorHorizontally();
            setImage(reversed);
        } else {
            currentImage = (currentImage + 1) % images.length;
            setImage(images[currentImage]);
        }
    }

    private void cheatPressEvent()
    {
        if (Greenfoot.isKeyDown("b") && Greenfoot.isKeyDown("o"))
        {
            cheatJump();
        }
    }

    private void jump()
    {
        if (jumpDelay <= 0)
        {
            vSpeed = -jumpStrength;
            fall();

            // 3Greenfoot.playSound("Jump.wav");

            jumpDelay = 30;

            // setImage("Mario0.png");
        }
    }

    private boolean onGround()
    {
        Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);

        return (platform != null);
    }

    public void checkFall()
    {
        if (onGround() && !Greenfoot.isKeyDown("shift"))
        {
            setPosition();
            vSpeed = 0;
        } else {
            fall();
        }
    }

    public void respawn()
    {
        if (getY() > 590 && songDuration <= 0)
        {
            Greenfoot.playSound("Died.mp3");
            getWorld().stopped();

            setLocation(125, 500);
            Greenfoot.delay(150);

            getWorld().started();

            invulnerability = 50;
            vSpeed = 3;
        }
        
        if (songDuration > 0)
        {
            starRespawn();
        }
    }
    
    public void starRespawn()
    {
        if (getY() > 590)
        {
            setLocation(125, 500);
        }
    }

    public void getCoin()
    {
        if (getOneIntersectingObject(Coin.class) != null)
        {
            getWorld().removeObject(getOneIntersectingObject(Coin.class));
            Scoreboard scoreboard = new Scoreboard("Score: 0");
            getWorld().addObject(new Coin(), Greenfoot.getRandomNumber(630) + 20, Greenfoot.getRandomNumber(500));

            Greenfoot.playSound("GetCoin.mp3");

            coinCount++;

            if (Greenfoot.getRandomNumber(10) < 5)
            {
                getWorld().addObject(new BadCoin(), Greenfoot.getRandomNumber(630) + 50, Greenfoot.getRandomNumber(400) + 25);
            }

            if (Greenfoot.getRandomNumber(50) < 1 && songDuration <= 0)
            {
                getWorld().addObject(new Star(), Greenfoot.getRandomNumber(630) + 50, Greenfoot.getRandomNumber(400) + 25);
            }

            if (coinCount == 25)
            {
                GravitationalUniverseOfJumping gravity = (GravitationalUniverseOfJumping) getWorld();
                gravity.level2();
            }

            if (coinCount == 50)
            {
                GravitationalUniverseOfJumping gravity = (GravitationalUniverseOfJumping) getWorld();
                gravity.level3();
            }

            if (coinCount == 75)
            {
                GravitationalUniverseOfJumping gravity = (GravitationalUniverseOfJumping) getWorld();
                gravity.level4();
            }
        }
    }

    public void deathByBadCoin()
    {
        if (getOneIntersectingObject(BadCoin.class) != null && invulnerability <= 0 && songDuration <= 0)
        {
            Greenfoot.playSound("Died.mp3");
            getWorld().stopped();

            getWorld().removeObject(getOneIntersectingObject(BadCoin.class));

            setLocation(125, 500);
            Greenfoot.delay(150);

            getWorld().started();

            invulnerability = 50;
            vSpeed = 3;
        }
    }

    public void godMode()
    {
        if (Greenfoot.isKeyDown("9") && (Greenfoot.isKeyDown("o") && (Greenfoot.isKeyDown("0"))))
        {
            invulnerability = 1;
        }
    }

    public void setPosition()
    {
        if (!Greenfoot.isKeyDown("shift"))
        {
           Actor platform = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
           int platformTop = platform.getY() - platform.getImage().getHeight()/2;
           int myY = platformTop - getImage().getHeight()/2;
           setLocation(getX(), myY);
        }
    }

    public void cheatJump() 
    {
        if (jumpDelay <= 0)
        {
            vSpeed = -jumpStrength;
            fall();

            Greenfoot.playSound("Jump.wav");

            jumpDelay = 20;
        }
    }

    public void starPowerUp()
    {
        GreenfootSound invsong = new GreenfootSound("Invincible.mp3");
        if (getOneIntersectingObject(Star.class) != null && songDuration <= 0)
        {
            Greenfoot.playSound("Invincible.mp3");
            getWorld().stopped();

            getWorld().removeObject(getOneIntersectingObject(Star.class));

            // getWorld().removeObject(this);

            // getWorld().started();
            // getWorld().addObject(new StarredMarioMario("Mario", ".png", 3), getX(), getY());
            songDuration = 1050;
            jumpStrength = 25;
        }

        if (songDuration > 0)
        {
            songDuration--;
        } 
        
        if (songDuration <= 0) 
        {
            GravitationalUniverseOfJumping gravity = (GravitationalUniverseOfJumping) getWorld();
            
            gravity.invStopped();
            gravity.started();
            jumpStrength = 20;
        }
    }

    public void starPressEvent()
    {
        if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 8, getY());
            facingLeft = false;
            if (animationDelay <= 0 && onGround())
            {
                animate();
                animationDelay = 5;
            }
        }

        else if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 8, getY());
            facingLeft = true;
            if (animationDelay <= 0 && onGround())
            {
                animate();
                animationDelay = 5;
            }
        } else {
            setImage("Mario2.png");
            if (facingLeft)
            {
                getImage().mirrorHorizontally();
            }
        }
    }
    
    public void firePower()
    {
        if (getOneIntersectingObject(FireFlower.class) != null && songDuration <= 0)
        {
            getWorld().removeObject(getOneIntersectingObject(FireFlower.class));
            fireDuration = 500;
        }
    }
    
    public void notFireProjectile()
    {
        if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0 && facingLeft == false)
        {
            getWorld().addObject(new Shot(), getX(), getY());
            shotDelay = 75;
            
            Greenfoot.playSound("FireShot.wav");
        } else if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0 && facingLeft == true) {
            getWorld().addObject(new Shot(), getX(), getY());
            shotDelay = 75;
            
            Greenfoot.playSound("FireShot.wav");
        }
        
        if (fireDuration > 0 && Greenfoot.isKeyDown("shift"))
        {
            //getWorld().addObject(new Shot(), getX(), getY());
            //shotDelay = 30;
            
            //Greenfoot.playSound("FireShot.wav");
        }
    }
    
    public void fireProjectile()
    {
        if (Greenfoot.isKeyDown("j") && shotDelay <= 0 && fireDuration > 0)
        {
            Shot s = new Shot(); //creates a new Shot object
            s.setRotation(getRotation()); //sets the rotation of the new Shot object to the same rotation as the shooting object
            getWorld().addObject(s, getX(), getY()); //adds the object to the world
            shotDelay = 75;
            Greenfoot.playSound("FireShot.wav");
        }
    }
}
Yeah...
There are more replies on the next page.
1
2