Game Development Reference
In-Depth Information
if
(target ==
null
)
// we're done
Rectangle sourceRectangle =
new
Rectangle((
int
)randomPos.X + xoffset,
(
int
)randomPos.Y, 1, 1);
Color[] retrievedColor =
new
Color[1];
target.GetData<Color>(0, sourceRectangle, retrievedColor, 0, 1);
if
(retrievedColor[0].A == 255)
// we're done
}
The condition of this
while
-instruction is
true
, meaning that the instruction will never
stop, unless we do something within the body of the
while
-instruction to stop it.
There are two cases when we have to stop the
while
-instruction. The first one is that
the target sprite is
null
, meaning that we do not have a target sprite to check. In that
case, we assume that any random glitter position in the rectangle is fine. The second
reason can be that we found a random position that is on a fully opaque pixel in
the target sprite. Both cases are indicated using comments in the above example.
So how can we force the
while
-instruction to stop? One way would be to use the
return
-instruction, for example as follows:
if
(target ==
null
)
return
randomPos;
The
return
-instruction ends the execution of a method (including a
while
-instruction
such as this one) and returns a value to the caller of the method. A second way of
stopping the
while
-instruction is by using the
break
keyword:
if
(target ==
null
)
break
;
The
break
keyword stops the execution of the closest enclosing loop instruction (
for
or
while
) in which it appears. In the example above, this is the
while
-loop inside the
createRandomPosition
method. Once that loop is stopped, the program continues on
the line after that instruction. The only thing that remains to be done then is returning
the randomly generated position:
return
randomPos;
18.4.4 Updating the Glitter Field
In the constructor, we set the scale for each of the glitters to 0. The result is that
when these glitters are drawn, they will not be visible to the player. In the
Update
method, we are going to increase and decrease this scale again until it returns to zero.