Star Game
Today I wasn't able to come to the Career Center because an English test I was taking took too long, and I missed the bus. However, I have been doing some Actionscript with Adobe Flash, and I have built a star field simulator. The illusion of the stars going past the ship was achieved by increasing the width and height of the group of stars. To control the "movement past" stars and planets and give an illusion of depth, I used code like this:
onClipEvent(load){
thrust = 0; // stars start out not moving.
}
onClipEvent (enterFrame) {
approachSpeed = (.3 * thrust); // Speed a function of width
this._height+=approachSpeed;
this._width+=approachSpeed;
if (Key.isDown(Key.SPACE)) { // Pressing space gives illusion of acceleration
if (thrust <=9)
{
thrust+=1;
}
}
if (Key.isDown(Key.CONTROL)) //CTRL is the brakes
{
if (thrust >= -12)
{
thrust-=1;
}
}
}
Planets, however, work differently. The rate of the increasing of the"approachSpeed" is controlled by how large the planet appears to be. As the planet gets larger, it gets larger quicker.
onClipEvent(load){
this._height = 3; //defines size of planet
this._width = 3;
this._x = 205; //defines placement
this._y = 105;
thrust = 0; //begins steady
this.swapDepths( _root.player1 );
}
onClipEvent (enterFrame) {
if (this.hitTest(_root.player1)) //if this hits the crosshairs
{
_root.description = "Targeting: Planet Isoya"; //display "Targeting Planet Isoya"
this._x+=approachSpeed; //Now the planet approaches the player instead
this._y+=approachSpeed; // just flying by like the stars
}
else
{
_root.description = "";
}
if (this._width <= 10) // these lines of code control how fast the planet
{ // is coming at you, based on how big the planet
approachSpeed = (.01 * thrust); // already is
}
else if (this._width <=25)
{
approachSpeed = (.012 * thrust);
}
else if (this._width <=45)
{
approachSpeed = (.023 * thrust);
}
else
{
approachSpeed = (.035 * thrust);
}
this._height+=approachSpeed; // makes the speed of the planet a function of its size
this._width+=approachSpeed;
this._x-=approachSpeed;
this._y-=approachSpeed;
if (Key.isDown(Key.SPACE))
{
if (thrust <= 9)
{
thrust+=1;
}
}
if (Key.isDown(Key.CONTROL))
{
if (thrust >= -12)
{
thrust-=1;
}
}
}
No comments:
Post a Comment