Game Development Reference
In-Depth Information
dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_PLAYER_SHOOT, false, 0, 8, 1));
tempProjectile = projectileManager.projectilePool.pop();
var projectileRadians:Number = (player.frame / 360) * 6.28;
//+ 16 to get it to the center of a 32x32 sprite
tempProjectile.x=(player.point.x+16)+Math.cos(projectileRadians);
tempProjectile.y =(player.point.y+16) + Math.sin(projectileRadians);
tempProjectile.x = player.x+16;
tempProjectile.y = player.y + 16;
tempProjectile.nextX = tempProjectile.x;
tempProjectile.nextY = tempProjectile.y;
tempProjectile.dx = rotationVectorList[player.frame].x;
tempProjectile.dy = rotationVectorList[player.frame].y;
tempProjectile.speed = 5;
tempProjectile.frame = 0;
tempProjectile.bitmapData = tempProjectile.animationList[0];
projectileManager.projectiles.push(tempProjectile);
projectileManager.lastProjectileShot=0;
}else {
projectileManager.lastProjectileShot+=step;
}
The preceding function is the actual entire projectile firing code. We only need be concerned with
these two lines:
tempProjectile.dx = rotationVectorList[player.frame].x;
tempProjectile.dy = rotationVectorList[player.frame].y;
The player object's current
frame
index is the rotated
BitmapData
in the
player.animationList
array that represents the player's current rotated direction. The projectiles pull their
dx
and
dy
values from the
rotationVectorList
array using the same index using the x value for
dx
and the
y value for
dy
.
Optimizing screen-based blit scrolling with
ScrollRect
There are many ways to scroll a screen when programming in AS3. In the previous chapter, we
looked at tile-based blit rendering over a 360-degree scrolling playfield. In this chapter, we will
look at an optimized method to scroll a screen that is not necessarily made up of tiles (although it
could be). We are going to draw all of our assets for the game as vector shapes, add some
simple glow filters, and cache those as
BitmapData
. We will draw all of these game objects to a
world-sized
Bitmapdata
canvas, but only display the current viewable window to the user. We will
scroll our screen using a different method than the
Camera2D
class from Chapter 10.
In our experience, the fastest way to scroll a bitmap screen in AS3 is to use the
scrollRect
Rectangle
property of the
Bitmap
instance that holds our world canvas. We simply change the x