From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch09;
002
003 importimport means to make the classes and/or packages available in this program fang2.sprites.CompositeSprite;
004
005 /**
006 * A classclass is a group of fields and methods used for making objects which factors out the velocity and movement fields and
007 * methods that are common among the moving elements in RescueMission.
008 */
009 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects SpriteWithVelocity
010 extendsextends means to customize or extend the functionality of a class CompositeSprite {open braces start code blocks and must be matched with a close brace
011 /** The sprite's velocity */
012 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaX;
013 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions deltaY;
014
015 /**
016 * Default constructor makes a sprite with a (0, 0) velocity.
017 */
018 publicpublic is used to indicate unrestricted access (any other class can have access) SpriteWithVelocity() {open braces start code blocks and must be matched with a close brace
019 thisthis means the current object (the implicit parameter)(0.0, 0.0);
020 }close braces end code blocks and must match an earlier open brace
021
022 /**
023 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@code MovingCompositeSprite}close braces end code blocks and must match an earlier open brace with the given
024 * velocity vector. The velocity is set to the given value
025 *
026 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX initial horizontal velocity
027 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY initial vertical velocity
028 */
029 publicpublic is used to indicate unrestricted access (any other class can have access) SpriteWithVelocity(doubledouble is the type for numbers that can contain decimal fractions deltaX, doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
030 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;
031 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;
032 }close braces end code blocks and must match an earlier open brace
033
034 /**
035 * Default advance behavior: Move according to the current velocity.
036 * That is, the velocity vector (in screens/sec) is multiplied by the
037 * time since the last frame (sec) and the result is the amount to
038 * move thisthis means the current object (the implicit parameter) sprite (screens).
039 *
040 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT seconds since last frame
041 */
042 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
043 translate(dT * deltaX, dT * deltaY);
044 }close braces end code blocks and must match an earlier open brace
045
046 /**
047 * Bounce thisthis means the current object (the implicit parameter) sprite off an edge of the screen. This is intended to
048 * be overridden by subclasses to specialize what should happen.
049 */
050 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffEdges() {open braces start code blocks and must be matched with a close brace
051 // intentionally does nothing
052 }close braces end code blocks and must match an earlier open brace
053
054 /**
055 * Get the current horizontal velocity.
056 *
057 * @returnnull current horizontal velocity in screens/second
058 */
059 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getDeltaX() {open braces start code blocks and must be matched with a close brace
060 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaX;
061 }close braces end code blocks and must match an earlier open brace
062
063 /**
064 * Get the current vertical velocity.
065 *
066 * @returnnull current vertical velocity in screens/second
067 */
068 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getDeltaY() {open braces start code blocks and must be matched with a close brace
069 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaY;
070 }close braces end code blocks and must match an earlier open brace
071
072 /**
073 * Scale the velocity (in both dimensions) by the given factor. factor
074 * > 1 speeds up; factor < 1 slows down. Positive keeps the direction
075 * the same, negative reverses the direction
076 *
077 * @paramthis is the Javadoc tag for documenting the purpose of parameters factor the factor to multiply the velocity by
078 */
079 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value multiplyVelocity(doubledouble is the type for numbers that can contain decimal fractions factor) {open braces start code blocks and must be matched with a close brace
080 deltaX *=this multiplies the variable on the left by the value on the right and stores the result in the variable factor;
081 deltaY *=this multiplies the variable on the left by the value on the right and stores the result in the variable factor;
082 }close braces end code blocks and must match an earlier open brace
083
084 /**
085 * Reverse the velocity of the object in both dimensions.
086 */
087 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value reverseVelocity() {open braces start code blocks and must be matched with a close brace
088 multiplyVelocity(-1);
089 }close braces end code blocks and must match an earlier open brace
090
091 /**
092 * Set the horizontal velocity
093 *
094 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX newnew is used to create objects by calling the constructor horizontal velocity in screens/second
095 */
096 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setDeltaX(doubledouble is the type for numbers that can contain decimal fractions deltaX) {open braces start code blocks and must be matched with a close brace
097 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;
098 }close braces end code blocks and must match an earlier open brace
099
100 /**
101 * Set the vertical velocity
102 *
103 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY newnew is used to create objects by calling the constructor vertical velocity in screens/second
104 */
105 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setDeltaY(doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
106 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;
107 }close braces end code blocks and must match an earlier open brace
108
109 /**
110 * Set the velocity of thisthis means the current object (the implicit parameter) sprite to the given x and y values.
111 *
112 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX horizontal velocity of the victims (screens/sec)
113 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY vertical velocity of the victims (screens/sec)
114 */
115 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setVelocity(doubledouble is the type for numbers that can contain decimal fractions deltaX, doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
116 thisthis means the current object (the implicit parameter).deltaX =this assignment operator makes the left side equal to the right side deltaX;
117 thisthis means the current object (the implicit parameter).deltaY =this assignment operator makes the left side equal to the right side deltaY;
118 }close braces end code blocks and must match an earlier open brace
119
120 /**
121 * Is the sprite moving down the screen?
122 *
123 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true moving down, falsefalse is a value for the boolean type and means not true otherwise
124 */
125 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false movingDown() {open braces start code blocks and must be matched with a close brace
126 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaY > 0;
127 }close braces end code blocks and must match an earlier open brace
128
129 /**
130 * Is the sprite moving left on the screen?
131 *
132 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true moving left, falsefalse is a value for the boolean type and means not true otherwise
133 */
134 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false movingLeft() {open braces start code blocks and must be matched with a close brace
135 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaX < 0;
136 }close braces end code blocks and must match an earlier open brace
137
138 /**
139 * Is the sprite moving right on the screen?
140 *
141 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true moving right, falsefalse is a value for the boolean type and means not true otherwise
142 */
143 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false movingRight() {open braces start code blocks and must be matched with a close brace
144 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaX > 0;
145 }close braces end code blocks and must match an earlier open brace
146
147 /**
148 * Is the sprite moving up the screen?
149 *
150 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true moving up, falsefalse is a value for the boolean type and means not true otherwise
151 */
152 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false movingUp() {open braces start code blocks and must be matched with a close brace
153 returnreturn means to provide the result of the method and/or cease execution of the method immediately deltaY < 0;
154 }close braces end code blocks and must match an earlier open brace
155
156 /**
157 * Has the sprite hit any of the four walls?
158 *
159 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true sprite has hit; falsefalse is a value for the boolean type and means not true otherwise
160 */
161 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoAnyWall() {open braces start code blocks and must be matched with a close brace
162 returnreturn means to provide the result of the method and/or cease execution of the method immediately runIntoVerticalWall() ||this is boolean or, meaning if either or both are true then the result is true runIntoHorizontalWall();
163 }close braces end code blocks and must match an earlier open brace
164
165 /**
166 * Has the sprite hit the bottom of the screen?
167 *
168 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the sprite has hit the bottom; falsefalse is a value for the boolean type and means not true otherwise
169 */
170 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoBottomWall() {open braces start code blocks and must be matched with a close brace
171 returnreturn means to provide the result of the method and/or cease execution of the method immediately (movingDown() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (1.0 <=this evaluates to true if the left side is not more than the right side getMaxY()));
172 }close braces end code blocks and must match an earlier open brace
173
174 /**
175 * Has sprite hit either horizontal wall?
176 *
177 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true sprite has hit; falsefalse is a value for the boolean type and means not true otherwise
178 */
179 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoHorizontalWall() {open braces start code blocks and must be matched with a close brace
180 returnreturn means to provide the result of the method and/or cease execution of the method immediately runIntoTopWall() ||this is boolean or, meaning if either or both are true then the result is true runIntoBottomWall();
181 }close braces end code blocks and must match an earlier open brace
182
183 /**
184 * Has the sprite hit the left edge of the screen?
185 *
186 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the sprite has hit the left edge; falsefalse is a value for the boolean type and means not true otherwise
187 */
188 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoLeftWall() {open braces start code blocks and must be matched with a close brace
189 returnreturn means to provide the result of the method and/or cease execution of the method immediately (movingLeft() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (getMinX() <=this evaluates to true if the left side is not more than the right side 0.0));
190 }close braces end code blocks and must match an earlier open brace
191
192 /**
193 * Has the sprite hit the right edge of the screen?
194 *
195 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the sprite has hit the right edge; falsefalse is a value for the boolean type and means not true otherwise
196 */
197 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoRightWall() {open braces start code blocks and must be matched with a close brace
198 returnreturn means to provide the result of the method and/or cease execution of the method immediately (movingRight() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (1.0 <=this evaluates to true if the left side is not more than the right side getMaxX()));
199 }close braces end code blocks and must match an earlier open brace
200
201 /**
202 * Has the sprite hit the top of the screen?
203 *
204 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the sprite has hit the top; falsefalse is a value for the boolean type and means not true otherwise
205 */
206 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoTopWall() {open braces start code blocks and must be matched with a close brace
207 returnreturn means to provide the result of the method and/or cease execution of the method immediately (movingUp() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (getMinY() <=this evaluates to true if the left side is not more than the right side 0.0));
208 }close braces end code blocks and must match an earlier open brace
209
210 /**
211 * Has the sprite hit either vertical wall
212 *
213 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true sprite has hit; falsefalse is a value for the boolean type and means not true otherwise
214 */
215 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false runIntoVerticalWall() {open braces start code blocks and must be matched with a close brace
216 returnreturn means to provide the result of the method and/or cease execution of the method immediately runIntoLeftWall() ||this is boolean or, meaning if either or both are true then the result is true runIntoRightWall();
217 }close braces end code blocks and must match an earlier open brace
218 }close braces end code blocks and must match an earlier open brace
219
220 //Uploaded on Mon Mar 29 21:38:39 EDT 2010
|
Download/View scg/ch09/SpriteWithVelocity.java