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

2015/2/4

I need help with Counter

AmericanaStorm AmericanaStorm

2015/2/4

#
I already only have the counter on the background, but I don't know how to create my game where the shooter only shoots out one bullet at a time to hit an enemy where the Counter goes up by one. I also want where if the enemy gets passed the actual shooter the game ends. How do I do this? This is the bullet code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import greenfoot.*;
 
/**
 * 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()
    {
       destroyEnemies ();
    }
     
    public void hitanenemy()
    {
        background backgroundWorld = (background) getWorld(); // get a reference to the world
        Counter counter = backgroundWorld.getCounter(); // get a reference to the counter counter.bumpCount(5);
    }
     
    public void destroyEnemies()
    {
    Actor enemy = getOneIntersectingObject(enemy.class);
    if(enemy != null)
    {
        World myWorld = getWorld();
        getWorld().removeObject(enemy);
        Counter.addScore();
        if (this.atWorldEdge()) {
            getWorld().removeObject(this);
        }
    }
 
}
 
public void kill()
{
    Actor enemy = getOneIntersectingObject(enemy.class);
    if(enemy != null)
    {
        getWorld().removeObject(enemy);
    }
}
}
This is the background code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import greenfoot.*;
 
/**
 * Write a description of class background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class background extends World
{
     
    private int score;
    private int bulletInPlay;
     
    
    private Counter theCounter;
         
    public background()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        score=0;
        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()
    {
        shooter shooter = new shooter();
        addObject(shooter, 310, 349);
        theCounter = new Counter();
        addObject(theCounter, 540, 20);
    }
     
    public Counter getCounter()
    {
        return theCounter;
    }
}
Ask me if you need anymore class codes. Thanks
davmac davmac

2015/2/4

#
Can you please be clear about how it doesn't work. Does it not do what you want (in which case - what does it do?) or does it not compile (in which case - what line do you get the error on, and what is the error message?)
danpost danpost

2015/2/4

#
The code for your bullet class seems a bit much. The act method only calls the 'destroyEnemies' method; but, you also have a 'kill' and a 'hitanenemy' method. What are these doing in the class? Is 'kill' being called from another class? and, the 'hitanenemy' method really does not do anything. It creates a local (to the method) variable to hold the 'theCounter' Counter object retrieved from the background World object; but, it does nothing with it. When the method is exited, the variable is trashed. The 'destroyEnemies' method tries to call 'addScore' on the Counter class; the class does not have a score, only the instances (objects created from the class -- the Counter objects) have scores. You should create the local field created in the 'hitanemeny' method in the 'destroyEnemies' method and 'addScore' to that object. Also, in your 'destroyEnemies' method, you have some code that removes the bullet from the world when at a world edge. That has nothing to do with destroying an enemy and should not be in that method -- and certainly not at a place where the bullet is hitting an enemy. The bullet will only be removed (with the code as is) if the bullet reaches the world edge at the same time it hits an enemy (which probably never will happen -- unless the enemy runs into a bullet that is lodged at a world edge).
AmericanaStorm AmericanaStorm

2015/2/4

#
@davmac - the error is on line 33 and the error message is: cannot find symbol - variable counter @danpost: I dont really know....All I'm trying to figure out my game on how it should work like I've previous stated. And next time please, don't do it in a long paragraph. It's hard to read. With your questions, I only want the bullet to kill, and the shooter to shoot the bullet. I'll remove the hitanenemy and the destroyEnemies method. How do I work with the kill method now to add +1 to the counter everytime an enemy is killed and the gameend screen pops up when the enemy passes the shooter?
AmericanaStorm AmericanaStorm

2015/2/4

#
Also, I don't know how to only shoot one bullet or even make the shooter at all on the y axis.... Here's the shooter code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import greenfoot.*;
 
/**
 * Write a description of class shooter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class shooter extends Actor
{
    GreenfootImage img;
    private int offset;
    private int count;
     
    public shooter()
    {
        img=new GreenfootImage("karo.png");
        setImage(img);
        count=0;
    }
     
    /**
     * Act - do whatever the shooter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveAndTurn();
        shoot();
    }
    public void moveAndTurn()
    {
        move(3);
        move(-3);
         
        if (Greenfoot.isKeyDown("d"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-3);
        }
    }
     
    // using the instance field
    private boolean spaceDown;
    // the Shoot method
    public void shoot()
    {
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown=true;
        }
        if(spaceDown && !Greenfoot.isKeyDown("space")) spaceDown=false;
    }
                     
 
     
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
        }
    }
danpost danpost

2015/2/4

#
AmericanaStorm wrote...
please, don't do it in a long paragraph. It's hard to read.
Do you not wish to learn? The only way I can let you know what I see wrong is by describing what I see and make suggestions on how to fix it. By asking what I quoted above, you are making a suggestion that you do not want to learn and you want the code to be handed to you without any understanding of how it works. I hope that is not the case.
AmericanaStorm AmericanaStorm

2015/2/4

#
@danpost I do wish to learn, but it hurts my eyes in that long paragraph and to find the code that you gave. Also, I fixed an error in the meantime. I don't want the code handed to me. Just that I wish to know how to move in the y axis, how to add +1 to the counter when the bullet 'eats' an enemy.
danpost danpost

2015/2/4

#
AmericanaStorm wrote...
it hurts my eyes in that long paragraph and to find the code that you gave.
Okay, I will break it up into segments. And I did not provide any code -- just suggestions.
I wish to know how to move in the y axis
Either rotate your character 90 or 270 degrees or use the 'setLocation' method of the Actor class.
how to add +1 to the counter when the bullet 'eats' an enemy.
You had all the bits and pieces in your original code post (getting a reference to the Counter object and using the 'addScore' method). You just need to put them together properly.
davmac davmac

2015/2/5

#
@davmac - the error is on line 33 and the error message is: cannot find symbol - variable counter
I assume you mean in the 'bullet' class (which should be named 'Bullet' btw). Your code on that line says:
1
Counter.addScore();
Counter is a class, right? So, it's just not possible that you'd get the error you've said you got, unless you have changed 'Counter' to 'counter' in the code.
AmericanaStorm AmericanaStorm

2015/2/5

#
Ok so far thanks davmac and danpost! :D I have completed the problem of the y axis. Now time to work on the Counter! I'll come back later if I run into any problems.
AmericanaStorm AmericanaStorm

2015/2/9

#
I need the counter to be done in less than an hour. If one of you guys are on, I need help pronto please. I had no time to work on it over the weekend, so im still at square one. All I need is to get the counter working by increasing the score by +1 every time a bullet 'eats' the enemy. Thanks
danpost danpost

2015/2/9

#
danpost wrote...
< Quote omitted > You had all the bits and pieces in your original code post (getting a reference to the Counter object and using the 'addScore' method). You just need to put them together properly.
I need not add more (everything needed was given somewhere in this discussion thread).
You need to login to post a reply.