Combining boolean and button

Hi there, I have a piece of actionscript which I would like to have executed when a button is pressed. That same piece of actionscript should execute when a boolean statement is met. Is it possible to combine the two in one script? For instance I now have:

button1.onPress = function (){
gotoAndPlay (2)
}

plus:

if (flag1 = true) {
gotoAndPlay (2);
}

Can this be made in to a working script like this:

button1.onPress = function () “PLUS” if (flag1 = true) {
gotoAndPlay (2);
}

Any help much appeciated. Thanks!