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

2015/12/27

Problem with Moving object forward direction it's facing

RendraDT RendraDT

2015/12/27

#
You can see above the bullet is not moving correctly Here is my code in the head class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Head extends Shooter
{
    public boolean isGrabbed;
    /**
     * Act - do whatever the Head wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        grab();
        setRotation(((Actor) getWorld().getObjects(Slider.class).get(0)).getRotation());
        if(Greenfoot.mouseClicked(this))
        {
            Bullet peluru = new Bullet();
            getWorld().addObject(peluru,getX(),getY());
            peluru.setRotation(((Actor) getWorld().getObjects(Head.class).get(0)).getRotation());
            peluru.setImage("bullet3.png");
        }
        // Add your action code here.
    }   
}
And this one in the class bullet :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()
    {
         
        move(2);
        if(this.isAtEdge())
            getWorld().removeObject(this);
        //System.out.println(getRotation());
        // Add your action code here.
    }   
}
danpost danpost

2015/12/27

#
I believe your issue deals with the layout of pixel. Imagine a checkerboard, where each square represents a pixel -- an absolute position any actor can be located at. When you move two spots away (I use '2' because that is how fast a bullet is moving in your scenario) from any square, there are only 8 possible new locations that can be arrived at. Basically, if the actor is not moving at one of these 8 angles exactly, there is a loss in precision in the angle being moved (there is a loss in precision in the distance moved when not moving at one of the 4 ordinal directions). Knowing this should give an understanding of why something more needs done to control their movement. There are several ways to handle it. All require at least two fields in the bullet class and two ways are given here. The fields could be more precise coordinate values for the current location of the actor ( 'double' type fields ). Or, you could use three int fields holding the starting coordinates (where placed into world at) and total distance moved since placed in the world. The movement of the actors will then utilize the value of the fields to determine where they need to be placed on successive act cycles.
You need to login to post a reply.