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

2012/7/11

making "walls" and detecting impact

sparrow72 sparrow72

2012/7/11

#
Hi, I was wondering if I could get some help with a game in trying to create It is a tank game and I have basic functions down but I am having trouble with the following: 1 make the bricks impenetrable from the tanks (trying to make them a "wall") 2 make the bullet invisible when on the tank & for it to follow the tank (I have it as just using the same code as the tank so they move together but am trying to make it follow it) 3 make the bullet come back so I can fire more than once (how can I locate the tank) 4 make the shots stop on impact (for some reason I cannot get it to detect right) 5 also my tank freezes when I fire and I don't know why here is a link to my scenario
danpost danpost

2012/7/11

#
Evidently, you are missing out on some basic concepts of Object Oriented Programming. Each type of object has its own characteristics (which you implement). The only thing a bullet should do is 'quickly move until it either hits something or reaches the edge of the world. It is the tank that 'fires' the bullet (a bullet cannot fire itself). Another misunderstanding you may have is that you say you need the bullet to come back so you can fire it again. That is not how it works. The tank can create multiple bullets (usually triggered by a specific user keypress). The bullets should remove themselves upon reaching the edge of the world, or an obstacle it cannot pass through. As far as your bricks, they do nothing but exist wherever they are at. The only thing you should have in its class code is a constructor, if even that (if you set a class image, then there is not anything to construct). Getting back to you bullets, they are not directed by user keystroke as they blindly follow the trajectory they are put into; so most of the code in that class, as you have it, can be discarded and replaced with appropriate code for the bullet. If you have any questions concerning the above, please do not hesitate to post a reply. I would also suggest that you take a look at the Java tutorials. Start with 'Learning the Java Language'. It is the second one listed in the second section on the left, called 'Trails Covering the Basics'. Once you get going, you can check out the others.
sparrow72 sparrow72

2012/7/11

#
thanks! I really needed some help. although i did have some questons too, if all the bullet does is
danpost wrote...
quickly move until hit
how will it "follow" or stay with the tank? also
danpost wrote...
most of the code in that class, as you have it, can be discarded and replaced with appropriate code for the bullet
what is "the appropriate code for the bullet"? as far as the object only doing what it would do in real life, where would the code for the not passing through bricks go? and what code would "could" I use... I've tried many different times the tank to not (ghost) through the bricks but it either does not work or "sticks" to the brick any code examples or more suggestions of what Java tutorials would teach me about them?
danpost danpost

2012/7/11

#
For a basic bullet, you would use something like the following for the class code:
import greenfoot.*;

public class Bullet extends actor
{
    int speed = 10;
    public Bullet(int rotation)
    {
        setRotation(rotation);
    }

    public void act()
    {
        travel(speed);
    }

    private void travel(int howFar)
    {
        move(speed);
        // check for bricks and edges of world
        if (!getIntersectingObjects(Brick.class).isEmpty() || getX() == 0 || getY() == 0 || getX() == getWorld().getWidth() - 1 || getY() == getWorld().getHeight() - 1) 
        {
            getWorld().removeObject(this);
        }
    }
}
In the Tank class act() (or whatever method act calls to check keypresses, you would put this line:
if (getWorld().getObjects(Bullet.class).isEmpty() && Greenfoot.isKeyDown("space")) addObject(new Bullet(getRotation()), getX(), getY());
Also, in the tank class, you would want to check to see if a bullet has hit the tank. So, you would might have the act method call a method called checkDamage(), and that method would look something like:
private void checkDamage()
{
    Bullet bullet = (Bullet) getOneIntersectingObject(Bullet.class);
    if (bullet != null)
    {
        getWorld().removeObject(bullet);
        healthPoints -= 5;
        if (healthPoints <= 0) 
        {
            // do what you do when GAME OVER
        }
    }
}
Like I said this is just an example and not to be used exactly as is, but the general idea is here. The tank will spawn a bullet when the right circumstances present themselves, the bullet does what it does (and removes itself if it did not hit anything before reaching the edge of the world), and the tank checks for damage (and removes damaging bullet). The tank should also check for the walls. HINT: let the tank move and check to see if a wall intersects the tank. If so, move the tank back where it started (if you used (move(3), then use move(-3) to move it back). The player must then steer the tank away from the wall. There is a minor problem with the bullet image. I will see if I can forward you a better image.
danpost danpost

2012/7/11

#
My Snake Demo scenario uses a modified version of the same image you are currently using for the bullet. Download one of them and copy the 'beeper.png' image from its images folder to your scenario's image folder. The image size is the same as the objects image size (no excess whitespace around the image making the 'size' of the image larger than the 'size' of the object itself).
sparrow72 sparrow72

2012/7/11

#
ok, so have been attempting to apply your code and i come across a error "constructor Bullet in class Bullet cannot be applied to the given types; required: int; found: no arguments; reason: actual and formal arguments differ in length" the error is in the "prior acceptable" code line in (Background)
            Bullet bullet = new Bullet();
so how would i go about correcting this error?
danpost danpost

2012/7/11

#
Remove any code referencing the Bullet class from the Background. No bullets are in the world to start. Only after the scenario is running and the user presses the spacebar will a bullet appear in it.
sparrow72 sparrow72

2012/7/12

#
ok i have tryed this in the tank class only, and it still gives me the same error.
sparrow72 sparrow72

2012/7/12

#
also how do i reference functions in other actors?
danpost danpost

2012/7/12

#
What is the code you have in the Tank class?
sparrow72 sparrow72

2012/7/12

#
well all of the code is fine except
if (Greenfoot.isKeyDown("space"))
        {
            Bullet bullet = new Bullet();
            World myWorld = getWorld();
            myWorld.addObject(new Bullet(getRotation()), getX(), getY());
            fire();
            bullet.setTimer(101);
        }
the line "Bullet bullet = new bullet();" gives the error "constructor Bullet in class Bullet cannot be applied to the given types; required: int; found: no arguments; reason: actual and formal arguments differ in length" and the line "fire();" simply says "cannot find symbol - method fire()" (the method fire is in the bullet) in the last line it says it cant find the method setTimer for the same reason as "fire" but it is doubly odd because i referenced this in the joy of code trial "trick the turtle"... i made sure to copy everything to the capitalization. so why wont it work? i will update my here ok as i was updating i now find that the tank is adapting to the same error. you will find these errors in the background with all 3 of the lines including "tank" are noted out with // thanks again, i have to go to a week camp now but will be back July 15 :) thanks for your help
danpost danpost

2012/7/12

#
Change that code to
if (getWorld().getObjects(Bullet.class).isEmpty() && Greenfoot.isKeyDown("space"))
{
    getWorld().addObject(new Bullet(getRotation()), getX(), getY());
}
You can then remove the 'fire()' method; and the bullet does not need a timer.
09blackn 09blackn

2012/8/14

#
how do you stop an actor from traveling through a object Thanks
SPower SPower

2012/8/14

#
@09blackn Why don't you just read all the posts up above? It answers your question...
danpost danpost

2012/8/14

#
@09blackn, Look at the code of my Barriers and Bars Demo.
You need to login to post a reply.