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

2016/10/31

if(isTouching(object.class)

ZoeF ZoeF

2016/10/31

#
I have a enoying problem wich i seem unable to solve. To situate my issue i wil first try to explain what i am trying to acomplish. I have a map on wich there is a road, the road has bends. on each bend i have 1px by 1px transparent block wich either should turn me left or right. My idee was to make my tank wich is moving along the road hit the transparent block and then either move right or left according to wich block i placed there (i have a obj TurnRight and one obj TurnLeft). Now my isue seems to be that while my tank is turning it hits the block again and therefore turns multiple times. Is there a way to prohibit my vehicle to do that or is there a better way?
Super_Hippo Super_Hippo

2016/10/31

#
You could have a boolean and set it to true when you touch the object and turn. When it is not touching the object anymore, you set the boolean back to false. You only check if you want to turn if the boolean is false.
danpost danpost

2016/10/31

#
Another way to handle it is to use just one object for turning; but, set its rotation to the direction the tank should go. That way, when at a block, if the rotations are different, turn; if they are the same, move. This will not work, however, if the block is used for multiple, different directional approaches.
ZoeF ZoeF

2016/10/31

#
I like to thank you all for the idees. As i needed to wait 12h to get my post approved i found another solution wich suits me pretty wel. I removed the idee of istouching and worked with 2 arrays in my enemies super.class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void turning()
    {
        int[] rightLeft = {90, -90, -90, 90, -90, -90, 90, 90, 90};
        int[][] turnCords = {
            {128, 128, 448, 448, 512, 512, 192, 192, 576},
            {448, 512, 512, 448, 448, 320, 320, 128, 128}
        };
 
        /**
         * Als getX = turnCords colom 0 waarde teller && getY = turnCords colom 1 waarde teller
         * dan draai rightLeft array waarde van teller
         * tellerTurn +1
         *
         * Dit zorgt dan daarna dat de waardes naar de volgende stap in de array springen.
         */
         
        if (getX() == turnCords[0][tellerTurn] && getY() == turnCords[1][tellerTurn]){
            turn(rightLeft[tellerTurn]);
            tellerTurn++;
        }
    }
You need to login to post a reply.