12(b) motions attack the sufficiency of the complaint or jurisdiction/service and may be submitted either in a pre-Answer motion or in the Answer. Some of them must be raised on the first response (either pre-answer motion or in the Answer). Some can be raised later, including the 12(b)(6).
Affirmative defenses are not attacks on the complaint. They are defenses that, if true, allow the D to escape liability even if the P proves everything in his Conplaint. The D has the burden of proving them, and they are raised in the Answer.