Search code examples
flutterflame

Need Help Replacing Deprecated Collision Code in Flutter's Flame Game Development"


I'm developing a game with Flutter's Flame, but my code is deprecated, and I don't know how to replace this part. Can you help me?

 void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
   super.onCollision(intersectionPoints, other);
   if (other is Attack) {
     removeFromParent();
     removeHitbox(hitboxRectangle);
     onTouch.call(other.position)
   }
 }

Thank you very much

Solution

  • Just change Collidable to PositionComponent and what you most likely want to do here is override onCollisionStart and not onCollision:

     void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
       super.onCollisionStart(intersectionPoints, other);
       if (other is Attack) {
         removeFromParent();
         onTouch.call(other.position)
       }
     }
    

    You also don't have to remove the hitbox, since you are already removing the whole parent.

    Also remember to use the correct mixin, with CollisionCallbacks instead of with HasHitboxes or whatever it used to be called.