Hi there, Im making a simple game where the up key needs to be tapped to keep the character (bird) in the air. A bit like a helicopter effect. The code used must be AS3, and at the moment I have no movie clips on the stage everything is called via classes. Currently I have
There is a class that handles the bird
package
{
import flash.display.MovieClip;
public class Bird extends MovieClip
{
public function Bird()
{
}
public function moveABit( xDistance:Number, yDistance:Number ):void
{
var baseSpeed:Number = 3;
x += ( xDistance * baseSpeed );
y += ( yDistance * baseSpeed );
}
*and a game driver class
*
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class GameDriver extends MovieClip
{
public var allheads:Array;
public var bird:Bird;
public var gameTimer:Timer;
public var downKeyPress:Boolean;
public var upKeyPress:Boolean;
public var leftKeyPress:Boolean;
public var rightKeyPress:Boolean;
public function GameDriver() // constructor
{
downKeyPress = false;
upKeyPress = false;
leftKeyPress = false;
rightKeyPress = false;
allheads = new Array();
var newHead = new Head( 100, 200 ); // numbers in brackets represent starting position
allheads.push( newHead );
addChild( newHead );;
bird = new Bird();
addChild( bird );
gameTimer = new Timer( 25 );
gameTimer.addEventListener( TimerEvent.TIMER, onTickofTimer);
gameTimer.start();
addEventListener( Event.ADDED_TO_STAGE, onAddToStage );
} // end constructor
public function onAddToStage( event:Event ):void
{
stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress );
stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease );
} // end onAddtoStage
public function onKeyPress( keyboardEvent:KeyboardEvent ):void
{
if ( keyboardEvent.keyCode == Keyboard.DOWN )
{
downKeyPress = true;
}
else if ( keyboardEvent.keyCode == Keyboard.UP )
{
upKeyPress= true;
}
else if ( keyboardEvent.keyCode == Keyboard.LEFT )
{
leftKeyPress = true;
}
else if ( keyboardEvent.keyCode == Keyboard.RIGHT )
{
rightKeyPress = true;
}
}// end on keypress
public function onKeyRelease( keyboardEvent:KeyboardEvent ):void
{
if ( keyboardEvent.keyCode == Keyboard.DOWN )
{
downKeyPress = false;
}
else if ( keyboardEvent.keyCode == Keyboard.UP )
{
upKeyPress= false;
}
else if ( keyboardEvent.keyCode == Keyboard.LEFT )
{
leftKeyPress = false;
}
else if ( keyboardEvent.keyCode == Keyboard.RIGHT )
{
rightKeyPress = false;
}
}// end on keyrelease
public function onTickofTimer( timerEvent:TimerEvent ):void
{
if ( Math.random() < 0.3 ) // speed at which heads appear
{
var headDistributionX:Number = Math.random() * 400;
var newHead:Head = new Head( headDistributionX, 200 );// start position of head
allheads.push( newHead );
addChild( newHead );
}
/////////controls
if ( downKeyPress )
{
bird.moveABit( 0, 1 );
}
else if ( upKeyPress )
{
bird.moveABit( 0, -1 );
}
else if ( leftKeyPress )
{
bird.moveABit( -1, 0 );
}
else if ( rightKeyPress )
{
bird.moveABit( 1, 0 );
} ///////////// end controls
///// WHAT HAPPENS WHEN BIRD HITS HEAD
for each ( var head:Head in allheads )
{
head.moveHead();
if ( bird.hitTestObject( head ) )
{
gameTimer.stop();
var gameOver:EndGame = new EndGame();
gameOver.x = 0;
gameOver.y = 0;
addChild( gameOver );
}
}
} // end for each
}// end tick of timer
}
Aologies for the long post, but my hair has been turning grey over this. The only tutorial i have found is for a helicopter and is great but uses AS2 to create the gravity like effect, unfortunately I cant use AS2 and i am in general i complete beginner so any help would be very much appreciated!!