First have a good position. 

Play positionaly and get a position where the attack will work.  You can not just decide to go out and attack from the gitgo. 

Or else become a tactical genius and learn how to make complex sacrifices that will not sound offer chances to win.  

Your play was poor in the example. But black was not especially red hot either.  

On 9 you should play NxB to double his pawns and get the two bishops.  

On 5 you should play Qxd8 and win a pawn or else black cannot castle for some positional advantage.