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

2018/10/21

isTouching only works once

bommijn bommijn

2018/10/21

#
Hello, i'm making a Game with random flying objects. When they touch eachother they have to turn 180°. I got this part working however there is a slight issue. When 2 objects meet for the first time they will both change there dirrection 180°. But when they meet eachother again they will just ignore eachother ea dont do any collision and just float over eachother. IS there any reason why isTouching only works once ? the code looks as follows:
private void checkTouching(){
     if (isTouching(BigAsteroid.class)){
        setRotation((rotation + 180) % 360);
        }
I know the same result can be achieved by using get one intersecting object than changing those rotations respectively. Just clueless about why isTouching only works once .
danpost danpost

2018/10/21

#
bommijn wrote...
When 2 objects meet for the first time they will both change there direction 180°. But when they meet each other again they will just ignore each other ea don;t do any collision and just float over each other. Is there any reason why isTouching only works once ? << Code Omitted >>
Are you sure it only works once? or, does it only appear to work only once? What is 'rotation' in the class? Does it ever change its value? ea., where are the lines in the class code that begin with 'rotation =' that sets its value to something other than zero? The 'rotation' field is probably not even needed as you only need to turn 180 degrees. As a sidenote, the superclass, Actor, already gives all actors a 'rotation' field. Although it is private to the Actor class, its value can be acquired with getRotation and changed with setRotation. In other words, you could just change 'rotation' in line 3 to 'getRotation()'.
bommijn bommijn

2018/10/21

#
You were right i overlooked this. field Rotation was initialized in my constructor and given a random value which was than assigned to setRotation(). I could have actually used setRotation(Greenfoot.getRandomNumber(...)); When they collide it would set the rotation field from Actor to the new value but not rotation in my class which was used to calculate the new rotation. So it appeared as it was being ignored but it actually just set it to the same amount every time. Thanks for changing my point of view, was clueless here for a while.
You need to login to post a reply.