In a platform activity, you can make a collectable run away from a character.
Within a platform activity select an "apple". Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the apple. Click on the red fish (for animation settings) and click on the "adv" option. in the script box that appears, enter the following code;
if (( Math.abs(_root.player._x - this._x) < 100) and ( Math.abs(_root.player._y - this._y) < 100)) {
var angle = Math.atan2(this._y - _root.player._y, this._x - _root.player._x);
this._x += Math.cos(angle)*2;
this._y += Math.sin(angle)*2;
}
The first step specifies that the apple should only run away if the player is closing in. The 2nd step works out the angle between the 2 objects by taking the arctan of the difference in y over the difference in x. The next two lines calculate the x and y position of the apple by taking the cos or sin of the angle.
View example; Download Object_runs_away


Recent Comments