Game Development Reference
In-Depth Information
public class Mine extends BasicBlitArrayObject{
public function Mine(xMin:int,xMax:int, yMin:int, yMax:int) {
super(xMin, xMax, yMin, yMax);
}
public function update(step:Number=1):void {
nextX+=dx*speed*step;
nextY+=dy*speed*step;
//bounce
if (nextX > xMax) {
nextX = xMax;
dx *= -1;
}else if (nextX < xMin) {
nextX = xMin;
dx *= -1;
}
if (nextY > yMax) {
nextY = yMax;
dy *= -1;
}else if (nextY < yMin) {
nextY = yMin;
dy *= -1;
}
}
}
}
The most important thing to note about this class is the code that bounces the Mine around the
screen when it hits the edge of the world. When a Mine hits the edge of the world, it is sent off at
the inverse angle from where it came. We only do this for the axis of the side of the world the
Mine collided with. This way, it will not simply bounce back but reflect around the world.
That's the Mine class. Now, we will take a look at the BlasterMines Game.as subclass and all of the
logic we will implement to create the game. For the BlasterMines.as class technical design, we are
going to iterate through code in sections and describe any new parts for functions that have not
been discussed so far. You will find that we have covered much of BlasterMines.as already.
Building the Blaster Mines class
We are going to build the BlasterMines.as class in small, bite-sized chunks. This workflow will
not be exactly the same as in previous chapters, where we iterated though a working game file. In
this chapter, we want to move quickly though a lot of code that you have seen previously,
stopping to point out implementations of the theory discussed earlier in the chapter and any new
code or theory that has not already been discussed. Because we cover a lot of advanced topics
in this chapter, if you have not read the earlier chapters, you might want to verse yourself on
some of them before attempting to go straight to this optimized game code.
Let's create this class as the next new file for the Blaster Mines game package. Here is the class
file name and location for the Flex SDK (using Flash Develop):
Search WWH ::




Custom Search