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

2019/4/26

Collision not working after adding some shaking to the Actor

lubna_1988 lubna_1988

2019/4/26

#
The object of below actor class touches object of class EnemyA or EnemyB. After adding the code in the act method, the checkCollision() method is working only for the first collision. Later it works only for intermittent collisions (skips some objects after first collision) Note: touch() method returns true if the collision happens. public void act() { checkCollision(); if(delay>0){ if(delay%2==0){ turn(moveOffset); moveOffset = -6; }else{ turn(moveOffset); moveOffset = 6; } initialOffset-=moveOffset; delay--; } } public void checkCollision() { if(touch(EnemyB.class) || touch(EnemyA.class)) { Actor touched = getOneIntersectingObject(Actor.class); this.touchedClassName = touched.getClass().getName(); getWorld().removeObject(touched); if(touchedClassName.equals("EnemyA")) { getWorld().removeObject(touched); if(delay==0) delay = 20; } } }
danpost danpost

2019/4/26

#
Not sure why you use a modulus 2 condition for the turn/move code. More simply would be:
if (delay > 0)
{
    turn(moveOffset);
    moveOffset *= -1;
    initialOffset -= moveOffset;
    delay--;
}
Even after that, not sure why you have an initialOffset field at all. The rotation of the actor should be sufficient to acquire its value. Finally, not sure why touchedClassName is a field and not just a local variable. I doubt there is any reason to retain its contents from one act step to the next. Cannot pick out why you have the intermittent collision detection problem. Show your touch method code for review.
lubna_1988 lubna_1988

2019/4/26

#
Dear danpost, First of all I would like to thank you and appreciate all your help and efforts you are putting for this community. :) Also, I think my modulus condition was a problem, the intermittent collision detection problem is resolved now after using moveOffset *= -1; Regarding the initialOffset field, I did not get your point. touchedClassName is used in another functions of the same class as I need different behaviours for different enemies. I am new to greenfoot, so sorry for silly questions.
danpost danpost

2019/4/26

#
lubna_1988 wrote...
Regarding the initialOffset field, I did not get your point.
My point is the there is a one-to-one correlation between the value of the initialOffset field and what would be returned by using the getRotation method. Because this is the case here, you really do not need that field. You can just use the returned value from the method, either directly or in some simple expression, to get the value of what the field would contain.
You need to login to post a reply.