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

2014/5/6

How to Shoot

wasp1928 wasp1928

2014/5/6

#
I am working on a project for class and i need to know how to shoot an object (ammunition) at another object (the topic). Also when the Ammo and the target collide they both (Ammo and Target) disappear and if i miss aim, it will go to the worlds edge and then disappear from the worlds edge. The way i need it is how to make it go like "W" the ammo goes up, "D" it goes right, "S" it goes down, and "A" it goes left. I need the code for this and if you could that would be great.
Tentacle_Panther Tentacle_Panther

2014/5/6

#
Well, I can give you a good chunk of the solution to this, but I'm not quite good enough to explain why it works or how you can use it yet.
public int atkDir()
    {
        int dir = 0;
        boolean fired = false;
        if (Greenfoot.isKeyDown("right"))
        {
            dir = 90;
            fired = true;
        }
        if (Greenfoot.isKeyDown("left"))
        {
            dir = 270;
            fired = true;
        }
        if (Greenfoot.isKeyDown("up"))
        {
            dir = 0;
            fired = true;
        }
        if (Greenfoot.isKeyDown("down"))
        {
            dir = 180;
            fired = true;
        }
        if (fired) {return (dir);}
        else {return (500);}
    }
Hope this helps.
wasp1928 wasp1928

2014/5/6

#
thank you. do you or anyone know any more solutions about my questions?
Tentacle_Panther Tentacle_Panther

2014/5/7

#
public void spawnObject(int rotation)
    {
        setRotation(rotation);
        dRotation = rotation + 0.0;
        //setImage("spawningObject.png");
        /**
         * The following code results in an object spawning one square in the indicated direction.
         * the dRotation variable is reduced by 90 to account for Greenfoot placing 0 degrees as north.
         */
        int X = (int) Math.round(getX() + Math.cos(Math.toRadians(dRotation-90)));
        int Y = (int) Math.round(getY() + Math.sin(Math.toRadians(dRotation-90)));
        
        getWorld().addObject(new Stab(), X, Y);
    }
I was tired when I responded earlier, so sorry about forgetting this part. This is the part that will actually be of some use. Call it using atkDir() for the parameter and it'll spawn an object where you want it. (This specific code is used by the protagonist actor; that's what the setRotation and setImage lines are for.)
BeYouself BeYouself

2014/5/13

#
String key=Greenfoot.getKey(); if (key != null) { if (key.equals("space")) { getWorld().addObject(new Bullet(),279,301); } } You could use one key to fire it forward. In the Bullet subclass,print the following code: public void act() { move (20); if (variable <= 0) { getWorld().removeObject(this); } dist--; }
danpost danpost

2014/5/13

#
What is the class of the object that spawn the bullets? if it is an Actor object of some kind, what is its movement code, if it moves (which I presume it does)?
You need to login to post a reply.