Flash I';我是AS 3.0编程新手,我希望为键盘控件创建一个随机发生器
我正试图为我的课程编写一个随机生成器,用于控制,游戏是一个版本的太空入侵者,但游戏将包括每10秒随机一次的控制,并且会在5秒内改变。我拥有的代码Flash I';我是AS 3.0编程新手,我希望为键盘控件创建一个随机发生器,flash,actionscript,Flash,Actionscript,我正试图为我的课程编写一个随机生成器,用于控制,游戏是一个版本的太空入侵者,但游戏将包括每10秒随机一次的控制,并且会在5秒内改变。我拥有的代码 package { import flash.display.MovieClip; import flash.events.*; import flash.utils.Timer; import flash.events.KeyboardEvent; import flash.events.TimerEven
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;
}
}
}
}
包
{
导入flash.display.MovieClip;
导入flash.events.*;
导入flash.utils.Timer;
导入flash.events.KeyboardEvent;
导入flash.events.TimerEvent
公共类Main扩展了MovieClip{
var leftPressed:Boolean=false;
var rightPressed:Boolean=false;
var子弹
var myTimer:Timer=新定时器(10000,90)
//通过将变量设置为false来初始化变量
公共功能Main()
{
//构造代码
Spaceship.addEventListener(Event.ENTER_FRAME,moveToKey);
stage.addEventListener(KeyboardEvent.KEY\u DOWN,whichKey);
stage.addEventListener(KeyboardEvent.KEY\u UP,setkeyunpos);
}//添加一个evernt侦听器,该侦听器将检查是否有任何按键
按键功能(e:键盘事件)
{//这些是将要按下的键的键代码
//它还检查是否按下了键代码
如果(e.keyCode==37)
{//就剩这个了
leftPressed=true;
//这将变量设置为true
//声明它只有两个条件
//随后在第54行导致一种情况
}
如果(e.keyCode==39)
{//没错
//执行与上述任务完全相同的任务
//随后在第59行导致一种情况
rightspressed=true;
}
//这两种功能都是通过按下
//键盘上的按键。
//当您按键时,会触发上面的一个
//按下该键时会触发下面的一个
}
函数SetKeyUnpus(e:键盘事件)
{//函数将变量设置为false,这意味着它不会激活
//这些功能仅在按键时触发
如果(e.keyCode==37)
{
leftPressed=false;
}
如果(e.keyCode==39)
{
右压=假;
}
}
函数moveToKey(e:事件)
{//如果两条语句都运行,则这些函数将运行。
//对于列出的两种情况都是如此
如果(左键按下&(Spaceship.x>0))
//此功能使宇宙飞船向左移动5个像素
//当变量leftPressed为true且宇宙飞船大于0像素时
//该函数将运行
{
宇宙飞船.x-=5;
}
如果(右)按下&(Spaceship.x