Creating a Pesky Ad Part 3

In this last page of this tutorial you'll find out how to make our ad run away and disappear.

How to make it run away

if (distance<30 && _root.advert._xmouse<_root.advert.button._x) {
_root.advert._x += (40-distance)/2;
}
if (distance<30 && _root.advert._xmouse>_root.advert.button._x) {
_root.advert._x -= (40-distance)/2;
}

These lines of code are responsible for our advertisments pesky habit of running away from the mouse.

if (distance<30 && _root.advert._xmouse<_root.advert.button._x) {
 


Note: As you can see, I've placed a && sign between the two conditions, and it is worth knowing that you can also use and instead of this sign.

The first line of code is an if statement which checks 2 conditions.

1. whether the variable distance is smaller than 30.

2. whether the x coodinate of the mouse (within the Movieclip called ad ) is smaller than the x coordinate of the cancel-button (that is within the Movieclip called ad).

If both conditions are true then the code within the if statement is executed.

_root.advert._x += (40-distance)/2;

This line of code tells the Movieclip called ad to increase its x coodinate by the value specified on the right side of the equation. As you can see, it is not a static value since it contains a variable called distance. If you have previewed the code, you might have noticed a certain correlation: the smaller the variable distance, the bigger the value by which the Movieclip changes its x coordinate. This is what the variable distance does here.

I think it is a good thing to make a correlation of this kind, because it makes the move of the ad more natural.



Deleting the ad

}
_root.ad.button.onPress = function() {
_root.ad._visible = false;
};
}

The last chunk of code allows to delete the ad when the cancel-button is clicked. Well, in fact, the object is not really deleted, but merely made invisible.


Let's see how it works step by step:

}
_root.ad.button.onPress = function() {
_root.ad._visible = false;
};
}



we say that on the main Timeline (_root) there is an object called ad within which there is an object called button (that's our cancel-button) ...

}
_root.ad.button.onPress = function() {
_root.ad._visible = false;
};
}

... which, if pressed we want to ...

}
_root.ad.button.onPress = function() {
_root.ad._visible = false;
};
}

... do a function which is ...

}
_root.ad.button.onPress = function() {
_root.ad._visible = false;
};
}

... switching the _visible property of ad ( located on the main Timeline (_root)) to false.


 

Ufff, looks like we're done with that. I must say that was a rather long tutorial. Nenertheless, I really hope you've enjoyed it and learned something useful at the same time.

Also, you might find interesting other tutorials on this website.

Cheers!

 

Average: 4.4 (9 votes)