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

2015/3/5

How to get an object to push another object forward in the same direction?

LaffyTaffy65 LaffyTaffy65

2015/3/5

#
I am trying to figure out how to allow for a moving object to hit another stationary object and cause the stationary object to move in the same direction as the object that hit it. Kind of like a moving car ramming the back of a parked one. My current code is not working very well. When the object hits the other object, the object moves slightly to the right. Any ideas would be helpful.
1
2
3
4
5
6
7
8
private void checkDribble()
{
    Player1 a = (Player1) getOneIntersectingObject(Player1.class);
    if(a != null)
    {
        move();
    }
}
danpost danpost

2015/3/5

#
I believe you are trying to code it from the reactionary side. Better is to code it from the primary action side (from the Player1 class). It is that object, or its movement thereof, that is causing the other to be slightly moved (or nudged).
LaffyTaffy65 LaffyTaffy65

2015/3/5

#
I moved this code to Player1 class instead of Ball class and changed Player1 to Ball but now Player1 is moving when it hits Ball, I want it the other way around. How can I move one class from another class?
danpost danpost

2015/3/5

#
You use 'getOneIntersectingObject' to get a reference to the Ball object. Then move the ball. The following is not exactly what you will use; but, it shows line 1 getting a reference to the intersecting object and line 2 actually moving the object (if one does intersect the actor):
1
2
Actor ball = getOneIntersectingObject(Ball.class);
if (ball != null) ball.move(1);
LaffyTaffy65 LaffyTaffy65

2015/3/5

#
Right now I have:
1
2
3
4
5
6
7
8
private void checkDribble()
    {
        Actor Ball = getOneIntersectingObject(Ball.class);
        if(Ball != null)
        {
            Ball.move(10);
        }
    }
and this is working fine except Ball.class is only moving to the right horizontally every time it is hit. Instead of Ball.move(10); what could be used to move the same direction as Player1?
danpost danpost

2015/3/5

#
Like I said, that code was not going to be exactly what you needed. You will have to determine which way to move the ball and move it in the proper direction. Unfortunately, I am short on time at the moment and will probably be out for several hours.
You need to login to post a reply.