如何使用源将影片剪辑添加到特定帧(带有整个事件)的舞台上?

时间:2013-04-01 08:08:33

标签: actionscript-3 flash flash-cs5

这是我的角色类,我希望可以将引用此类的角色影片剪辑添加到某个帧中的舞台。因此,当我点击按钮开始时,它将添加角色的动画片段。如果有人能帮助我,我真的很感激....

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 
     */

}

}

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

如果你想在某个帧中添加字符,那就去做吧。你有两种方式:

  1. 创建字符类Heli()的实例使用Timer

    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的实例