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

2013/2/25

How do you get a bullet to fire in the direction that the gun is pointing?

1
2
ScratchMan ScratchMan

2013/2/25

#
Please reply it is driving me mad!
danpost danpost

2013/2/25

#
Show the code you are trying to use to create and add the bullet to the world (and any other bullet related code in that general area of your code.
forteddyt forteddyt

2013/2/26

#
Cosidering the following - You have a "Bullet" class extending from Actor, a "Gun" class extending from Actor(and that they are named that, this is what you will want to do. Keep in mind the "Bullet" and "Gun" are the classes I used). *Also Im sure that their are better and more efficient ways to do this, this is just one way I do it* *This will only give you code for when something is pressed, make an object1 appear from another object2 and move at a constant rate in the direction that your object2 is facing* In the Gun class, you'll want this : import greenfoot.*; public class Bullet extends Actor { private int direction, speed; public Bullet(int dir) { direction = dir; speed = 15; } public void act() { setRotation(direction); move(speed); } } *This set of code will make a Bullet class that, when dragged into the world, will ''move()'' at 15, and move in a direction you give it* Now that you have that, you'll want to head over to your Gun class and put the following in your "public void act()" : if(Greenfoot.isKeyDown("//key that when pressed will shoot the Bullet") ) { getWorld().addObject(new Bullet(getRotation()), getX(), getY()); } *This code will, when you press a certain key, make a Bullet come out of the Gun and move at a constant, move(15), rate in the same direction that your Gun is pointed at* I hope this helped, it works for me so hopefully it'll work for you. *However, this will give you a 'stream' of Bullets, I'm not sure how to make it shoot one bullet at a time. I have a post up at that asks how to do this (I think you can do it with an if getObjectInRange() method, but Im not sure...)* Link to my post here - Pulsating Bullets If you can help please do :)
danpost danpost

2013/2/26

#
Take a look at this discussion post, which explains the usage of 'isKeyDown' and 'getKey' as triggers for events. It also has example code that sets the rotation of the created objects.
ScratchMan ScratchMan

2013/2/27

#
Thanks for replying so quickly, the code is what I was looking for. However, the following error occured in the "gun" class. :( "constructor bullet in class bullet cannot be applied to given types; required: int; found: no arguments; reason: actual and formal argument lists differ in length;"
danpost danpost

2013/2/27

#
@ScratchMan, please supply the code in question. This would include the area around which the error occurs noting which line is highlighted and your bullet constructor(s).
forteddyt forteddyt

2013/2/28

#
Hmm... I ran the copied and pasted the code into a Greenfoot 'project' with those classes and it seems to work. You should try out the code in a separate Greenfoot 'project' and see if it works. However in the mean time, Ill try to figure out whats going on. Also, if you could Copy and Paste your 'Gun' and 'Bullet' class info here, it would help. :) PS - Did this happen when you Compiled the code, or when you "Run" the code, or when you actually tried shooting the 'Bullet's while the code was running?
forteddyt forteddyt

2013/2/28

#
Found it! After about 15 min of trying to get the same error as you, I think I've figured it out. This SHOULD be whats wrong : In the 'Gun' class under the " public void act() " in the code : if(Greenfoot.isKeyDown("space") ) { getWorld().addObject(new Bullet(getRotation()), getX(), getY()); } Your : getWorld().addObject(new Bullet(getRotation()), getX(), getY()); is probably missing the " getRotation() " in the " (new Bullet(getRotation()), getX(), getY()); " Without the " getRotation() " you get that error message. Hope this fixes it! :D
ScratchMan ScratchMan

2013/2/28

#
In reply to danpost, here is the code in question. I am not sure what forteddyt means as the getRotation() appears to be present. Thanks for spending so much time on this apparently oomed project-this version is the fourth attempt! public class spitfire extends bullet { /** * Act - do whatever the spitfire wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { { move(4); } turn(); shoot(); isWorldEdge(); } public void turn() { if(Greenfoot.isKeyDown("left")) { turn(-4); } if(Greenfoot.isKeyDown("right")) { turn(4); } } public void shoot() { if(Greenfoot.isKeyDown("space") ) { getWorld().addObject(new bullet(getRotation()), getX(), getY()); } } public void isWorldEdge() { if(atWorldEdge() == true ) { turn(178); } } public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) return true; else return false; } }
danpost danpost

2013/2/28

#
Need to see the bullet class code.
ScratchMan ScratchMan

2013/3/1

#
Here it is!
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
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
 
{
private int direction, speed;
    public bullet(int dir)
    {
        direction = dir;
        speed = 15;
    }
    /**
     * 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()
    {
        setLocation(getX() + speed, getY());
        isWorldEdge();
        setRotation(direction);
        move(speed);
    }
        public void isWorldEdge()
    {
            if(atWorldEdge() == true )
            {
        Actor bullet;
        bullet = getOneObjectAtOffset(0, 0, bullet.class);
        if (bullet != null)
        {
        World world;
        world = getWorld();
        world.removeObject(this);
        }
    }
}
    public boolean atWorldEdge()
    {
    if(getX() < 20 || getX() > getWorld().getWidth() - 20)
    return true;
    if(getY() < 20 || getY() > getWorld().getHeight() - 20)
    return true;
    else
    return false;
}
}
ScratchMan ScratchMan

2013/3/1

#
Happy now?
danpost danpost

2013/3/1

#
The thing that most probably is causing problems is the fact that a spitfire is not a bullet and should not extend bullet. spitfire and bullet should both extend actor. In the bullet class remove line 40, lines 33 through 36 and line 24. Move the 'setRotation' statement from the 'act' method to the bullet constructor (after 'direction = dir;'). Then reverse the order of the remaining two statements in the 'act' method. That should help.
ScratchMan ScratchMan

2013/3/3

#
thanks, but spitfire should extend bullet as spitfire is a subclass of bullet:)
danpost danpost

2013/3/3

#
Then, why does the spitfire class have a shoot method that creates a bullet? and, how come the spitfire object is controlled by the user with the arrow keys?
There are more replies on the next page.
1
2