From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch01.spacesprites;
002
003 importimport means to make the classes and/or packages available in this program java.awt.Color;
004
005 importimport means to make the classes and/or packages available in this program fang2.attributes.Vector2D;
006 importimport means to make the classes and/or packages available in this program fang2.core.Game;
007 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
008 importimport means to make the classes and/or packages available in this program fang2.core.Transformer;
009 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
010 importimport means to make the classes and/or packages available in this program fang2.transformers.HitTarget;
011 importimport means to make the classes and/or packages available in this program fang2.transformers.Shootable;
012 importimport means to make the classes and/or packages available in this program fang2.transformers.VelocityTransformer;
013 importimport means to make the classes and/or packages available in this program fang2.transformers.WrapTransformer;
014
015 /**
016 * Demonstration game sprite. Represents a Meteor in a Meteor avoidance
017 * game. Has automatic checking forfor is a looping structure for repeatedly executing a block of code being shot and forfor is a looping structure for repeatedly executing a block of code hitting a ship.
018 * That is why the ship and shots are all kept track of here.
019 */
020 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 Meteor
021 extendsextends means to customize or extend the functionality of a class OvalSprite
022 implementsimplements means providing method bodies for the methods declared in the corresponding interface HitTarget, Shootable {open braces start code blocks and must be matched with a close brace
023 /** the color the meteor is when it is created */
024 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Color meteorColor =this assignment operator makes the left side equal to the right side Game.getColor("SCG Red");
025
026 /** ratio of size number to scale */
027 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions SCALETOSIZE =this assignment operator makes the left side equal to the right side 0.075;
028
029 /** what size is thisthis means the current object (the implicit parameter) meteor? 1-3 are standard values */
030 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer sizeNumber;
031
032 /** the game to which thisthis means the current object (the implicit parameter) meteor belongs */
033 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) Game myGame;
034
035 /**
036 * 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@link Meteor}close braces end code blocks and must match an earlier open brace. A {open braces start code blocks and must be matched with a close brace@link Meteor}close braces end code blocks and must match an earlier open brace has movement
037 * (built in) and knows how to split itself.
038 *
039 * @paramthis is the Javadoc tag for documenting the purpose of parameters myGame
040 * @paramthis is the Javadoc tag for documenting the purpose of parameters sizeNumber
041 */
042 publicpublic is used to indicate unrestricted access (any other class can have access) Meteor(Game myGame, intint is the type for whole numbers and it is short for integer sizeNumber) {open braces start code blocks and must be matched with a close brace
043 super(sizeNumber * SCALETOSIZE, sizeNumber * SCALETOSIZE);
044 thisthis means the current object (the implicit parameter).sizeNumber =this assignment operator makes the left side equal to the right side sizeNumber;
045 thisthis means the current object (the implicit parameter).myGame =this assignment operator makes the left side equal to the right side myGame;
046 addTransformer(newnew is used to create objects by calling the constructor WrapTransformer());
047 addTransformer(newnew is used to create objects by calling the constructor VelocityTransformer(
048 newnew is used to create objects by calling the constructor Vector2D(myGame.randomDouble(-45, 45), myGame.randomDouble(0.05, 0.4))));
049
050 }close braces end code blocks and must match an earlier open brace
051
052 publicpublic is used to indicate unrestricted access (any other class can have access) Meteor(Game myGame) {open braces start code blocks and must be matched with a close brace
053 thisthis means the current object (the implicit parameter)(myGame, 3);
054 }close braces end code blocks and must match an earlier open brace
055
056 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value split() {open braces start code blocks and must be matched with a close brace
057 VelocityTransformer v =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
058 forfor is a looping structure for repeatedly executing a block of code (Transformer transform : transformers) {open braces start code blocks and must be matched with a close brace
059 ifif executes the next statement only if the condition in parenthesis evaluates to true (transform instanceofinstanceof is an operator for determining if the types are compatible VelocityTransformer) {open braces start code blocks and must be matched with a close brace
060 v =this assignment operator makes the left side equal to the right side (VelocityTransformer) transform;
061 breakbreak terminates the loop immediately;
062 }close braces end code blocks and must match an earlier open brace
063 }close braces end code blocks and must match an earlier open brace
064 ifif executes the next statement only if the condition in parenthesis evaluates to true (v !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
065 split(v.getVelocity());
066 }close braces end code blocks and must match an earlier open brace
067 }close braces end code blocks and must match an earlier open brace
068
069 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value split(Vector2D travel) {open braces start code blocks and must be matched with a close brace
070 doubledouble is the type for numbers that can contain decimal fractions layer =this assignment operator makes the left side equal to the right side myGame.getLayer(thisthis means the current object (the implicit parameter));
071 thisthis means the current object (the implicit parameter).removeFromCanvas();
072 disableTransformer();
073 Vector2D normal =this assignment operator makes the left side equal to the right side travel.getNormal();
074 ifif executes the next statement only if the condition in parenthesis evaluates to true (sizeNumber > 1) {open braces start code blocks and must be matched with a close brace
075 Meteor a =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Meteor(myGame, sizeNumber - 1);
076 a.setColor(getColor());
077 doubledouble is the type for numbers that can contain decimal fractions aVelocity =this assignment operator makes the left side equal to the right side myGame.randomDouble(0.5, 1.5) * travel.getMagnitude();
078 a.addTransformer(newnew is used to create objects by calling the constructor VelocityTransformer(
079 normal.multiply(aVelocity)));
080 a.setLocation(getLocation());
081 myGame.addSprite(layer, a);
082
083 Meteor b =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Meteor(myGame, sizeNumber - 1);
084 b.setColor(getColor());
085 doubledouble is the type for numbers that can contain decimal fractions bVelocity =this assignment operator makes the left side equal to the right side myGame.randomDouble(-1.5, -0.5) * travel.getMagnitude();
086 b.addTransformer(newnew is used to create objects by calling the constructor VelocityTransformer(
087 normal.multiply(bVelocity)));
088 b.setLocation(getLocation());
089 myGame.addSprite(layer, b);
090 }close braces end code blocks and must match an earlier open brace
091 }close braces end code blocks and must match an earlier open brace
092
093 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false react(Sprite target) {open braces start code blocks and must be matched with a close brace
094 split();
095 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
096 }close braces end code blocks and must match an earlier open brace
097
098 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value shot(Sprite shooter) {open braces start code blocks and must be matched with a close brace
099 split();
100 }close braces end code blocks and must match an earlier open brace
101 }close braces end code blocks and must match an earlier open brace
102
103 //Uploaded on Mon Mar 29 21:39:10 EDT 2010
|
Download/View scg/ch01/spacesprites/Meteor.java