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

2018/4/24

Help Me!!!!

1
2
Heet_Patel Heet_Patel

2018/4/24

#
I have left the source code for my new game out. This is because they game doesn't work properly. The fire method for level three robot is broken so could some please help me with the code.
Heet_Patel Heet_Patel

2018/4/24

#
http://www.greenfoot.org/scenarios/21125 link to my new game.
danpost danpost

2018/4/25

#
Heet_Patel wrote...
The fire method for level three robot is broken
The reason it does not work is because you use the getKey method for firing cannonballs in both the Enemy and cannon classes and an instance of both classes are in the world at the same time. Using the getKey method in game play is not recommended; first, because of the issue you are experiencing and secondly because of the lack of good feel for the user in that firing is not instantaneous on the press of a key as well as repeating multiple times if the key is held down. Use the isKeyDown method with a boolean field to track the state of the key so that you can determine exactly when the key is initially pressed (in both classes).
Heet_Patel Heet_Patel

2018/4/25

#
i dont get what you mean
danpost danpost

2018/4/25

#
Heet_Patel wrote...
i dont get what you mean
I mean that you add a boolean field to those classes to track the state of the firing key. For example, if "space"is used:
private boolean spaceDown;
Any time this field is not equal to what the isKeyDown method returns, then the keys state has changed and you can update the value of the field:
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = ! spaceDown;
Upon updating, you want to know if the change was for pressing or releasing the key. So, at this point, you ask:
    if (spaceDown == true)
    {
        fire() // or whatever, for producing a projectile
    }
}
The above is to fire the projectile when the key is pressed.
Heet_Patel Heet_Patel

2018/4/25

#
does this all go in the act method
Heet_Patel Heet_Patel

2018/4/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class cannon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends thrun
{
    /**
     * Act - do whatever the cannon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("W"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("S"))
        {
            turn(3);
        }
        if (spaceDown != Greenfoot.isKeyDown("space"))
        {
            spaceDown = ! spaceDown;
        } 
        if (spaceDown == true)
        {
            fire(); // or whatever, for producing a projectile
        }
    }

    /**
     * Fire the cannon
     */
    private void fire()
    {
        Cannonball stuff = new Cannonball();
        getWorld().addObject(stuff, getX(), getY());
        stuff.setRotation(getRotation());
        stuff.move(35.0);
    }    
}

This is the code for the enemy
Heet_Patel Heet_Patel

2018/4/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Road here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Road extends World
{
    private boolean spaceDown;
    /**
     * Constructor for objects of class Road.
     * 
     */
    public Road()
    {    
        super(1000, 700, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        house house2 = new house();
        addObject(house2,75,82);
        house2.setLocation(31,32);
        cannon cannon = new cannon();
        addObject(cannon,104,283);
        cannon.setLocation(125,332);
        cannon.setLocation(69,331);
        Enemy enemy = new Enemy();
        addObject(enemy,948,323);
        enemy.setLocation(948,328);
    }
}
This is the code for the world
Heet_Patel Heet_Patel

2018/4/25

#
it says cant find variable spacedown
danpost danpost

2018/4/25

#
Heet_Patel wrote...
it says cant find variable spacedown
Move line 28 to 33 and:
danpost wrote...
add a boolean field to those classes to track the state of the firing key. For example, if "space" is used:
private boolean spaceDown;
not in the Road class.
Heet_Patel Heet_Patel

2018/4/25

#
which class then do i add the boolean code
Heet_Patel Heet_Patel

2018/4/25

#
is it possible for ou to ust edit my code and send it to me
Heet_Patel Heet_Patel

2018/4/25

#
on this
Heet_Patel Heet_Patel

2018/4/25

#
now it says illegal stat of expression when i chaned line 22 to 38 in the actor class
Heet_Patel Heet_Patel

2018/4/25

#
i have put the boolean act by itself
There are more replies on the next page.
1
2