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

2018/8/23

Shooting Help

1
2
Nallaekul Nallaekul

2018/8/23

#
Hello All, I am quite new to greenfoot and need some assistance in making my character shoot, i would like to make it so when you press space it shoots in the direction the character is facing and that the bullets disapear when touching the edge. (which is controlled by WASD) i have been trying for weeks but the code has failed many times, would love to hear back from you. As extra questions can someone tell me if there is a way to make the world edge smaller while still retaining the picture How i can make characters die on collision with the bullets How i can make it so when youve killed a certain amount of enemies the stage changes to level 2 Thanks in advance heres my current code for both King (Character thats shooting) and my Bullet
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class King here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class King extends Actor
{
    /**
     * Act - do whatever the King wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
 {
   if (Greenfoot.isKeyDown("D")) {
    move(2);
    setImage("King Right.png");
    }
    else if (Greenfoot.isKeyDown("A")) {
    move(-2);
    setImage("King Left.png");
    }
    else if (Greenfoot.isKeyDown("W")) {
    setLocation(getX(), getY()-2);
    setImage("King Up.png");
    }
    else if (Greenfoot.isKeyDown("s")) {
    setLocation(getX(), getY()+2);
    setImage("King Down.png");
   
}
}

private GreenfootImage left = new GreenfootImage("King Left.png");
private GreenfootImage right= new GreenfootImage("King Right.png");
private GreenfootImage up= new GreenfootImage("King Up.png");
private GreenfootImage down= new GreenfootImage("King Down.png");
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
       move(3);
   }
}
Again thanks in advanced
danpost danpost

2018/8/24

#
Nallaekul wrote...
i would like to make it so when you press space it shoots in the direction the character is facing
As is, the is no way to determine the facing direction. On lines 19, 23, 27 and 31, use the field names instead of the file names. Also, add a constructor to set the initial image:
public King()
{
    setImage(right);
}
With the above changes, you will be able to check the current image of the actor with code like the following, for example:
if (getImage() == left)
the bullets disapear when touching the edge.
Use the isAtEdge method in the Bullet class act method:
if (isAtEdge()) getWorld().removeObject(this);
is a way to make the world edge smaller while still retaining the picture
Yes, but you will have to set the background image with code:
public MyWold()
{
    super(600, 400, 1);
    GreenfootImage bg = new GreenfootImage("background.png");
    bg.scale(600, 400);
    setBackground(bg);
    prepare();
}
Lines 4 through 6 are the pertinent ones, where the filename should match that of the desired image in your images folder.
How i can make characters die on collision with the bullets Add code to act method of characters:
if (isTouching(Bullet.class)) getWorld().removeObject(this);
How i can make it so when youve killed a certain amount of enemies the stage changes to level 2
If all enemies are placed into the world at the beginning of a level, you can just check the number of enemies in the world:
// in world class (as example)
if (getObjects(Enemy.class).size() < 6) Greenfoot.setWorld(new Level2());
If not, you will have to track the number of enemies killed:
private int enemiesSpawned;

public void act()
{
    if (enemiesSpawned-getObjects(Enemy.class).size() >= 5) Greenfoot.setWorld(new Level2());
    if (canSpawnEnemy())
    {
        addEnemy();
        enemiesSpawned++;
    }
}
danpost danpost

2018/8/24

#
For the shooting, you will need to track the state of the space key and use it in conjunction with the isKeyDown method:
// outside of act method (King class)
private boolean spaceDown;

// inside of act method
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = !spaceDown;
    if (spaceDown) fireBullet();
}
The last line here shows a method call, fireBullet, which can be replace with a code block containing the appropriate code for the action it "says" to take. I did similarly in previously given code. When you see a method call for a method that does not exist, replace it with appropriate code.
Nallaekul Nallaekul

2018/8/27

#
public void act()
 {
   if (Greenfoot.isKeyDown("D")) {
    move(2);
    public King();
    {
    setImage(right);
    }
    }
    else if (Greenfoot.isKeyDown("A")) {
    move(-2);
    public King();
   {
    setImage(left);
   }
    }
    else if (Greenfoot.isKeyDown("W")) {
    setLocation(getX(), getY()-2);
    public King();
    {
    setImage(up);
   }
    }
    else if (Greenfoot.isKeyDown("s")) {
    setLocation(getX(), getY()+2);
    public King();
    {
    setImage(down);
    }
 }
 }
private GreenfootImage left = new GreenfootImage("King Left.png");
private GreenfootImage right= new GreenfootImage("King Right.png");
private GreenfootImage up= new GreenfootImage("King Up.png");
private GreenfootImage down= new GreenfootImage("King Down.png");
}
This is what i have so far and on the public it is saying, "illegal start of expression" what is wrong here?
danpost danpost

2018/8/27

#
A constructor is a separate block of code, like a method in itself (totally apart from the act method. It is executed only once each time, and at the time, when a King object is created (it is called when "new King()" is executed in your code):
private GreenfootImage left = new GreenfootImage("King Left.png");
private GreenfootImage right = new GreenfootImage("King Right.png");
private GreenfootImage up = new GreenfootImage("King Up.png");
private GreenfootImage down = new GreenfootImage("King Down.png");

public King()
{
    setImage(right);
}

public void act()
{
    if (Greenfoot.isKeyDown("d")) {
        move(2);
        setImage(right);
    }
    else if (Greenfoot.isKeyDown("a")) {
        move(-2);
        setImage(left);
    }
    else if (Greenfoot.isKeyDown("w")) {
        setLocation(getX(), getY()-2);
        setImage(up);
    }
    else if (Greenfoot.isKeyDown("s")) {
        setLocation(getX(), getY()+2);
        setImage(down);
    }
}
Nallaekul Nallaekul

2018/8/27

#
Hey just a quick question how would i make the fireBullet thing, and then am i required to use the check image line and if it is that image to make the bullet fire in a certain direction, as of now i dont know how to do it, thanks
Nallaekul Nallaekul

2018/8/27

#
/**
     * Act - do whatever the King wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public King()
    {
        setImage(right);
        }
        private void fireBullet()
        {
            }         
  public void act()
{
    if (Greenfoot.isKeyDown("d")) {
        move(2);
        setImage(right);
    }
    else if (Greenfoot.isKeyDown("a")) {
        move(-2);
        setImage(left);
    }
    else if (Greenfoot.isKeyDown("w")) {
        setLocation(getX(), getY()-2);
        setImage(up);
    }
    else if (Greenfoot.isKeyDown("s")) {
        setLocation(getX(), getY()+2);
        setImage(down);
    }
    if (spaceDown != Greenfoot.isKeyDown("space"))
    {
    spaceDown = !spaceDown;
    if (spaceDown) fireBullet();
    }
}
private GreenfootImage left = new GreenfootImage("King Left.png");
private GreenfootImage right= new GreenfootImage("King Right.png");
private GreenfootImage up= new GreenfootImage("King Up.png");
private GreenfootImage down= new GreenfootImage("King Down.png");
private boolean spaceDown;
}
Sorry that im asking so many questions but is this right? and if so how do i use the get image code line and then after how do i make it fire the bullet in that certain direction? will it be coded that so that the bullet will travel in a certain direction when the getImage code is used and do i set that up like how i did my movement for my character (eg, move (-2) and setLocation)
danpost danpost

2018/8/27

#
Nallaekul wrote...
Hey just a quick question how would i make the fireBullet thing, and then am i required to use the check image line and if it is that image to make the bullet fire in a certain direction, as of now i dont know how to do it, thanks << Code Omitted >> Sorry that im asking so many questions but is this right? and if so how do i use the get image code line and then after how do i make it fire the bullet in that certain direction? will it be coded that so that the bullet will travel in a certain direction when the getImage code is used and do i set that up like how i did my movement for my character (eg, move (-2) and setLocation)
Looks okay so far. In the fireBullet method, you need to (1) create a Bullet object; (2) add it into the World object that the King object is in; and (3) set its rotation according to the current image of the King object. The act method in the Bullet class should have a move command and check for edge of world to remove a Bullet object from the world.
Nallaekul Nallaekul

2018/8/27

#
Can you please send through a basic code on how to do it so i can work off that?
danpost danpost

2018/8/27

#
Nallaekul wrote...
Can you please send through a basic code on how to do it so i can work off that?
Create a bullet:
Bullet bullet = new Bullet();
Add it to world:
getWorld().addObject(bullet, getX(), getY());
Set its rotation:
if (getImage() == down) bullet.setRotation(90);
else if (getImage() == left) bullet.setRotation(180);
else // etc.
Nallaekul Nallaekul

2018/8/27

#
Hey thank you so much it is working now, sorry to bombard you with questions and you have been a great help but i want to make it so there can only be 20 enemies on the screen at once, and when one is killed i want it to be recycled to the edge of the world, and start moving towards the character again until the score hits 100 killed enemies, how would i go as doing that?
danpost danpost

2018/8/27

#
Nallaekul wrote...
i want to make it so there can only be 20 enemies on the screen at once, and when one is killed i want it to be recycled to the edge of the world, and start moving towards the character again until the score hits 100 killed enemies, how would i go as doing that?
First, you will need a field in your world class to count enemies killed. Then, in your world act method, you would check the number of enemies in the world and if less than 20, add one to the field and if its value is not yet 100, add a new enemy into the world; else stop the program.
Nallaekul Nallaekul

2018/9/3

#
Hey sorry but if i wanted the game to advance to level 2 when 50 enemies have been killed, and I start with 5 characters in teh world how do i make it so the characters when killed respawn around the edge of the world?
danpost danpost

2018/9/4

#
Nallaekul wrote...
how do i make it so the characters when killed respawn around the edge of the world?
The following method, when placed in a subclass of World, will add the given actor to the World instance along some edge with equal probability for all possible points:
public void addActorSomewhereAlongAnyEdge(Actor actor)
{
    int rand = Greenfoot.getRandomNumber(2*getWidth()+2*getHeight()-4); // pick an edge point
    int spawnX = Math.min(rand, getWidth()-1); // go right along top edge
    int spawnY = Math.min(rand-spawnX, getHeight()-1); // go down right edge
    rand -= (spawnX + spawnY);
    spawnX -= Math.min(rand, getWidth()-1); // go left along bottom edge
    spawnY -= Math.max(0, rand-(getWidth()-1-spawnX)); // go up left edge
    addObject(actor, spawnX, spawnY);
}
Nallaekul Nallaekul

2018/9/11

#
So ive added that code into myworld and have changed actor to Ogre (which is my enemy) when i kill the enemy its not respawning, any ideas?
There are more replies on the next page.
1
2