我正在制作一个非常简单的Flash游戏来了解AS3。我用控件制作了一个圆圈。当我按下空间时,我想发射一颗子弹。我试过,虽然循环和不同的教程,但我只是无法弄清楚如何做到这一点。 这是代码:
import flash.net.drm.LoadVoucherSetting;
//controls
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
var dir:String = "default";
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
ball_mc.x -= 10;
dir = "venstre";
}
if (event.keyCode == Keyboard.RIGHT)
{
ball_mc.x += 10;
dir = "hojre";
}
if (event.keyCode == Keyboard.UP)
{
ball_mc.y -= 10;
dir = "op";
}
if (event.keyCode == Keyboard.DOWN)
{
ball_mc.y += 10;
dir = "ned";
}
if (event.keyCode == Keyboard.SPACE)
{
shootBullet();
}
}
var speed1:int = 1;
function shootBullet():void
{
//instantiate your object
var bullet:Bullets = new Bullets ;
//add it to the stage
addChild(bullet);
//object will default to x=0 , y=0 so you can define that as well
bullet.x = ball_mc.x;
bullet.y = ball_mc.y +(ball_mc.width/2);
bullet.y += 10 * 2;
}
stop();
答案 0 :(得分:1)
要使其移动,您需要使用某些间隔重复发生的事件
一个Timer
:
var myTimer:Timer = new Timer(25);//interval, milliseconds
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.start();
var speedX:int = 0;
var speedY:int = 1;
function timerHandler(event:TimerEvent):void {
bullet.y += speedX;
bullet.x += speedY;
}
或ENTER_FRAME
事件:
addEventListener(Event.ENTER_FRAME, onFrame);
function onFrame(event:Event):void {
bullet.y += speedX;
bullet.x += speedY;
}