我正在尝试为我的课程编写一个随机生成器来控制,游戏是一个太空入侵者的版本,但游戏将包括每10秒随机化一次的控件,它会改变5秒。我有的代码
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent
public class Main extends MovieClip {
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var bullet
var myTimer:Timer = new Timer (10000, 90)
//initaialises the variables by setting them to false
public function Main()
{
// constructor code
Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, whichKey);
stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnPres);
} //adds an evernt listener that will check for any key presses
function whichKey(e:KeyboardEvent)
{ // these are the key codes for the keys that will be pressed
// it also checks for if the key code is pressed
if (e.keyCode == 37)
{ // that is left
leftPressed = true;
// that sets the variable to true
//stating that it only has two conditions
// leads to a condition later, on line 54
}
if (e.keyCode == 39)
{ // that is right
// performss the exact same task as the one above
// leads to a condition later, on line 59
rightPressed = true;
}
// both of the funtions are triggered by pressing the
//keys on your keyboard.
// the one above is triggered when you press the keys
//the one beneath is triggered when the key is pressed
}
function setKeyUnPres(e: KeyboardEvent)
{ // function sets the variable to false, meaning that it doesn't activate
//these functions are only trigged when the key is pressed down
if (e.keyCode == 37)
{
leftPressed = false;
}
if (e.keyCode == 39)
{
rightPressed = false;
}
}
function moveToKey (e:Event)
{ // these function is run if both of the statements are run.
// this is true for both conditions listed
if (leftPressed && (Spaceship.x>0))
// this function makes the Spaceship move 5 pixels to the left
// when the variable leftPressed is true and the spaceship is greater than 0 pixels
// the function will run
{
Spaceship.x -= 5;
}
if (rightPressed && (Spaceship.x<550))
// this function makes the Spaceship move 5 pixels to the right
// when the variable rightPressed is true and the shapscehip is less than 550 pixels
// the function will run
{
Spaceship.x += 5;
}
if (e.keyCode == 32) {
var bulletshot:bullet = new bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y;
}
}
}
}
这是我到目前为止的代码,我需要帮助,因为我不完全确定代码如何适用于randmomised函数