0

这是我的角色类,我希望可以将引用该类的角色电影剪辑添加到特定帧中的舞台。所以当我点击按钮开始时,它会添加角色的电影剪辑。如果有人可以帮助我,我将不胜感激......

package com.ply 
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

import flash.ui.Keyboard;

public class Heli extends MovieClip
{
    //Settings
    public var xAcceleration:Number = 0;
    public var yAcceleration:Number = 0;
    private var xSpeed:Number = 0;
    private var ySpeed:Number = 0;

    private var up:Boolean = false;
    private var down:Boolean = false;
    private var left:Boolean = false;
    private var right:Boolean = false;

    private var bullets:Array;
    private var missiles:Array;


    public function Heli()
    {
        init();
    }

    private function init():void
    {
        addEventListener(Event.ENTER_FRAME, runGame);           
    }

    private function runGame(event:Event):void
    {
        xSpeed += xAcceleration ;       //increase the speed by the acceleration
        ySpeed += yAcceleration ;       //increase the speed by the acceleration

        xSpeed *= 0.95;                 //apply friction
        ySpeed *= 0.95;                 //so the speed lowers after time

        if(Math.abs(xSpeed) < 0.02)     //if the speed is really low
        {
            xSpeed = 0;                 //set it to 0
                                //Otherwise I'd go very small but never really 0
        }
        if(Math.abs(ySpeed) < 0.02)     //same for the y speed
        {
            ySpeed = 0;
        }

        xSpeed = Math.max(Math.min(xSpeed, 10), -10);       //dont let the speed get bigger as 10
        ySpeed = Math.max(Math.min(ySpeed, 10), -10);       //and dont let it get lower than -10

        this.x += xSpeed;               //increase the position by the speed
        this.y += ySpeed;               //idem

    }

    /**
     * Keyboard Handlers in main class 
     */

}

}

4

2 回答 2

0

我不完全理解这里的问题,你想做什么,但试试这个。将您的构造函数更改为以下代码:

public function Heli()
{
   addEventListener(Event.ADDED_TO_STAGE, init);
}

比,将 init() 函数更改为init(e:Event):void. 这将确保在您将 MovieClip 添加到舞台后完成任何初始化。哦,在 init() 函数的顶部添加这一行: removeEventListener(Event.ADDED_TO_STAGE, init);

然后,在按钮 START 的事件处理程序中,创建类的新实例,并添加到舞台(或父容器,取决于您的游戏结构)。

于 2013-04-01T12:32:19.303 回答
0

如果你想在某个框架中添加角色,那就去做吧。你有两种方法:

  1. 使用 Timer 创建角色类 Heli() 的实例

    var timer:Timer = new Timer(count_seconds, repeat_parameter) timer.addEventListener(TimerEvent.TIMER_COMPLETE, onEventHandler) timer.start();

    function onEventHandler(e:TimerEvent):void { create the unstance of Heli here }

  2. 您可以在 EnterFrame 处理程序中计算帧数,并在一些计数值之后创建角色 Heli 的实例

于 2013-04-02T08:52:34.317 回答