Someone please explain this particular code block to me !

6 posts (showing 1-6)
crayonHero

Market Level 3Community Level 1
78 posts

Hello,

Please Help!

I have been following a large set of tutorials on AS3 lately and there is a particular code block that is used in the tutorial to make a object face towards a point slowly.

You can check the full function the code is used in below (I have commented it for better readablity and also the particular code piece that I have trouble understanding is contained in a commented section)

[as3]

// This is the object's update function

function update() {

var desiredRotation = MyMaths.angleBetween(this, target); // using a prewritten static function to get the angle between 2 objects

var angleDiff = desiredRotation - rotation; // Calculating the angle difference

// THIS IS WHAT I DON'T UNDERSTAND!!! How can this code helping in slowly making this object face towards the desired angle ? I am banging my head over it from last night, trying to understand how it works! It's doing what it is supposed to but I gotta know how it works or I am gonna freak out to death !

while (angleDiff>180) angleDiff -= 360;

while (angleDiff<-180) angleDiff += 360;

if (angleDiff>2) {
rotation =rotation + 2;
}
if (angleDiff<-2) {
rotation = rotation - 2;
}

// *** //

MyMaths.moveForward(this, speed); // Move forward in the direction facing, its using a prewritten function from a static class

[/as3]

Thanks a lot!

Ankit

posted 2015-03-21T11:43:59-07:00
bluebox

Market Level 6Community Level 3
334 posts

This part of code is keeping output between -180 and 180 degree for more convinient measurments The differences. also computing degree is probably returning similar data format From -180 to 180. after that code is simply folowing terger with linear speed of 2degree per step.

posted 2015-03-21T13:23:45-07:00
keybol

Market Level 9Community Level 13
2985 posts

if we visualize it, without the while condition, the object will rotate counter clockwise instead of a quick small turn clockwise. happened to me when making an arrow rotates to the direction it is flying. there is a time that the object will rotate more than a whole 360 instead of the shortest rotation.

posted 2015-03-21T14:50:34-07:00 | edited 2015-03-21T14:51:10-07:00
bluebox

Market Level 6Community Level 3
334 posts

sorry Crayon, mobile browser didnt showed whole comment.

posted 2015-03-21T15:00:26-07:00
crayonHero

Market Level 3Community Level 1
78 posts

Ohh, Thanks a lot for the reply guys, not sure how it is doing it but at least now I know that its for maintaining the angle between 180 degrees :D

How do you get yourself to understand/create such functions ? Experience over years or a genius maths brain from birth ?

BTW, Thanks a lot for nice and quick help :D

posted 2015-03-21T15:01:01-07:00
crayonHero

Market Level 3Community Level 1
78 posts

In the tutorials, the author is using different frames on main timeline to create menu and other game screens, is that ok ? (I somewhere read that its bad to code in timeline, just curious so as to if it's ok and if you guys also do it in the same way or not)

Also, as the timeline contains different game layouts on different frames, document class is only used for basic game settings and a new movieclip object is added to stage with the class script that fires up the level specific functions such as the generation of enemies etc but the thing I am most curious about is that the new movieclip object used to fire up level script also contains the main player movieclip inside it and main player class script has some parent specific functions, So, by containing the player movieclip inside it, hasn't the level movie clip become his parent or are they both still sharing direct inheritance to stage ? (code related to parent inside player class is working as before adding the level object to the selection)

A little help on this would also be very helpful :D

(I am sorry if I wasnot able to explain it well, please let me know if there is anything that is not clear in my question and I will try to rephrase it )

Thanks a lot!

posted 2015-03-21T15:12:13-07:00