如何使用 Action Script 3.0 使随机放置的符号飞过?

发布于 2024-08-26 01:42:35 字数 170 浏览 6 评论 0原文

我正在尝试使用Flash CS4和Action Script 3.0制作一个简单的动画,使许多符号不断地从右向左飞过。我想要的是,一旦一个符号到达屏幕的末尾,它就会被销毁,另一个符号会被放置在起始位置。

我打算给每个符号一个随机的速度,并在每次“摧毁”一个符号时创建一个随机符号。有什么线索我可以从哪里开始吗?

I'm trying to make a simple animation with Flash CS4 and Action Script 3.0 to make a number of Symbols fly by from right to left constantly. What I want is that once a symbol has reached the end of the screen it is destroyed and another one is placed at the start position.

I intend to give each symbol a random speed and create a random symbol each time one is 'destroyed'. Any clues where I can start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌路黄昏 2024-09-02 01:42:35

由于您似乎对 Flash 作为一个平台很陌生,所以我认为编写类不应该是您学习 ActionScript 时的第一个选择。现在肯定只是在时间轴上玩一下并学习基础知识。作为一个非常简单的解决方案,我建议在库中创建一个 MovieClip,其类名如“MyBall”...然后将其粘贴到主时间线的第一帧上,瞧。

    // Create some variables to store data
var numberOfBalls : int = 20;
var myBalls : Array = [];
var xVelocities : Array = [];

var maxXVelocitySpeed : Number = 5;
var minXVelocitySpeed : Number = 2;

// Add your orginal balls to the stage
for (var i : int = 0; i < numberOfBalls; i++)
{
    var myBall : MyBall = new MyBall();
    myBall.x = -(Math.random() * stage.stageWidth);
    myBall.y = Math.random() * stage.stageHeight;

    var xVelocity : Number = minXVelocitySpeed + (Math.random() * (maxXVelocitySpeed - minXVelocitySpeed));

    myBalls.push(myBall);
    xVelocities.push(xVelocity);

    addChild(myBall);
}

// Add a listener for enter frame events
addEventListener(Event.ENTER_FRAME, enterFrameHandler);


//Run this code on every frame to move the balls and reposition them if they are off the stage
function enterFrameHandler(event : Event) : void
{
    for each( var myBall : MyBall in myBalls)
    {
        var ballIndex : int = myBalls.indexOf(myBall);

        myBall.x += xVelocity[ballIndex];

        if (myBall.x > stage.stageWidth)
        {
            myBall.x = -(Math.random() * stage.stageWidth);
            myBall.y = Math.random() * stage.stageHeight;
        }
    }
}

As you seem new to flash as a platform I would think writing classes shouldn't be your first port of call when learning ActionScript. Definitely just play about on the timeline for now and learn the basics. As very simple solution to this, I would suggest creating a MovieClip in the library with a class name like 'MyBall'... then paste this onto the first frame of the main timeline et voila.

    // Create some variables to store data
var numberOfBalls : int = 20;
var myBalls : Array = [];
var xVelocities : Array = [];

var maxXVelocitySpeed : Number = 5;
var minXVelocitySpeed : Number = 2;

// Add your orginal balls to the stage
for (var i : int = 0; i < numberOfBalls; i++)
{
    var myBall : MyBall = new MyBall();
    myBall.x = -(Math.random() * stage.stageWidth);
    myBall.y = Math.random() * stage.stageHeight;

    var xVelocity : Number = minXVelocitySpeed + (Math.random() * (maxXVelocitySpeed - minXVelocitySpeed));

    myBalls.push(myBall);
    xVelocities.push(xVelocity);

    addChild(myBall);
}

// Add a listener for enter frame events
addEventListener(Event.ENTER_FRAME, enterFrameHandler);


//Run this code on every frame to move the balls and reposition them if they are off the stage
function enterFrameHandler(event : Event) : void
{
    for each( var myBall : MyBall in myBalls)
    {
        var ballIndex : int = myBalls.indexOf(myBall);

        myBall.x += xVelocity[ballIndex];

        if (myBall.x > stage.stageWidth)
        {
            myBall.x = -(Math.random() * stage.stageWidth);
            myBall.y = Math.random() * stage.stageHeight;
        }
    }
}
谈情不如逗狗 2024-09-02 01:42:35

首先,将符号转换为影片剪辑。然后为您的符号创建一个基类 MySymbol.as,类似于:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.geom.Point;


    public class MySymbol extends MovieClip
    {
        public var speed:Number;        // Pixels moved per frame


        public function MySymbol(speed:Number, startPosition:Point)
        {
            this.speed = speed;
            this.addEventListener(Event.ENTER_FRAME, update);

            this.x = startPosition.x;
            this.y = startPosition.y;
        }


        private function update():void
        {
            this.x -= this.speed;
            if (this.x < 0 - this.width) {      // We're at the left edge
                this.removeEventListener(Event.ENTER_FRAME, update);
                this.dispatchEvent(new Event(Event.COMPLETE));
            }
        }
    }
}

然后确保您的影片剪辑导出为 AS3(库中项目的“链接”选项)。使每个项目的类名称唯一(例如MySymbol1、MySymbol2),并将基类设置为MySymbol。

您的文档类可能看起来像这样:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import MySymbol;                // Not strictly needed

    public class DocumentClass extends flash.display.MovieClip
    {
        private static var SYMBOLS:Array = new Array(MySymbol1, MySymbol2);

        public function DocumentClass()
        {
            // Create five symbols:
            for (var i:int = 0; i < 5; i++) {
                makeSymbol();
            }
        }


        private function makeSymbol():void
        {
            // Pick a random symbol from the array:
            var symType:Class = SYMBOLS[Math.random() * SYMBOLS.length];

            // Construct the new symbol:
            var loc:Point = new Point(stage.stageWidth, Math.random() * stage.stageHeight);
            var sym:MySymbol = new symType(1 + Math.random() * 30, loc);

            // Listen for the object hitting the left edge:
            sym.addEventListener(Event.COMPLETE, remakeObject);
            this.addChild(sym);
        }


        private function remakeObject(e:Event):void
        {
            e.target.removeEventListener(Event.COMPLETE, remakeObject);
            this.removeChild(e.target);

            // Replace the dead symbol:
            makeSymbol();
        }
    }
}

如果不是销毁并重新创建一个飞离舞台的对象,而是重新使用现有的对象并将其移回到右侧,那么效率会高得多。但这是一种优化,如果事情变得很慢,您可以稍后实施。

请注意,上面的所有代码都未经测试,而且我已经有一段时间没有编写 AS3 代码了,因此其中可能至少存在一些错误。希望它将作为一个足够好的起点。

First, turn your symbols into MovieClips. Then create a base class MySymbol.as for your symbols, something like:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.geom.Point;


    public class MySymbol extends MovieClip
    {
        public var speed:Number;        // Pixels moved per frame


        public function MySymbol(speed:Number, startPosition:Point)
        {
            this.speed = speed;
            this.addEventListener(Event.ENTER_FRAME, update);

            this.x = startPosition.x;
            this.y = startPosition.y;
        }


        private function update():void
        {
            this.x -= this.speed;
            if (this.x < 0 - this.width) {      // We're at the left edge
                this.removeEventListener(Event.ENTER_FRAME, update);
                this.dispatchEvent(new Event(Event.COMPLETE));
            }
        }
    }
}

Then make sure your movie clips are exported for AS3 (the "linkage" option on the item in the library). Make the class name for each item unique (e.g. MySymbol1, MySymbol2), and set the base class to MySymbol.

Your document class might look something like this:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import MySymbol;                // Not strictly needed

    public class DocumentClass extends flash.display.MovieClip
    {
        private static var SYMBOLS:Array = new Array(MySymbol1, MySymbol2);

        public function DocumentClass()
        {
            // Create five symbols:
            for (var i:int = 0; i < 5; i++) {
                makeSymbol();
            }
        }


        private function makeSymbol():void
        {
            // Pick a random symbol from the array:
            var symType:Class = SYMBOLS[Math.random() * SYMBOLS.length];

            // Construct the new symbol:
            var loc:Point = new Point(stage.stageWidth, Math.random() * stage.stageHeight);
            var sym:MySymbol = new symType(1 + Math.random() * 30, loc);

            // Listen for the object hitting the left edge:
            sym.addEventListener(Event.COMPLETE, remakeObject);
            this.addChild(sym);
        }


        private function remakeObject(e:Event):void
        {
            e.target.removeEventListener(Event.COMPLETE, remakeObject);
            this.removeChild(e.target);

            // Replace the dead symbol:
            makeSymbol();
        }
    }
}

It is a lot more efficient if instead of destroying and re-creating an object that flies off-stage you re-use the existing one and move it back to the right. But this is an optimization you can implement later, if things become slow.

Note that all the code above is UNTESTED and I have not coded AS3 in a while, so there's likely at least a few bugs in it. Hopefully it will serve as a good enough starting point.

赤濁 2024-09-02 01:42:35
  • 定义一个Circle(符号)类,该类扩展 Sprite/Shape 并具有一个velocity 变量
  • 用随机颜色绘制一个圆(或其他)
    Math.floor(Math.random() * 0xffffff)
  • 为速度分配随机值
    minVelocity + Math.floor(Math.random() *velocityRange)
  • Circle 类中创建一个 start() 方法来注册输入帧handler
  • 在输入帧处理程序内递增 this.y,如果 y 大于最大值,则调度 'recycleMe' 事件。
  • 创建 N 个Circle 实例,addChild 它们,并调用它们的 start() 方法。
  • 侦听每个事件上的 'recycleMe' 事件,并从处理程序重置 y 的值。
  • Define a Circle (symbol) class that extends Sprite/Shape and has a velocity variable
  • Draw a circle (or whatever) with a random color
    Math.floor(Math.random() * 0xffffff)
  • Assign a random value to velocity
    minVelocity + Math.floor(Math.random() * velocityRange)
  • Create a start() method inside the Circle class that registers an enter frame handler
  • Increment this.y inside the enter frame handler, and dispatch a 'recycleMe' event if y is more than the max value.
  • Create N instances of Circle, addChild them, and call their start() methods.
  • listen to 'recycleMe' events on each of them, and reset the value of y from the handler.
清醇 2024-09-02 01:42:35

以下是一些帮助您入门的提示。

MovieClips 具有 xy 属性。如果您随着时间的推移添加这些数字,您将看到 MovieClip 沿着舞台 的 x 和/或 y 轴移动。考虑使用 Event.ENTER_FRAME 来执行此操作,这将允许您在每次屏幕更新时更改值。

您的 stage 将具有给定的宽度(stageWidth 属性)。您可能想要监视 MovieClip 的 x 属性何时大于舞台的宽度。如果是删除 (removeChild) 并添加一个新的 (addChild),并将其放回起始 x/y 位置。

Here's a few prompts to get you started.

MovieClips have an x and y property. If you were to add to these numbers over time you would see the MovieClip move along the x and/or y axis of the stage. Look into doing this using the Event.ENTER_FRAME which will allow you to change the values every time the screen is going to update.

Your stage will have a given width (a stageWidth property). You probably want to monitor when your MovieClip's x property is greater than the width of your stage. If it is remove (removeChild) it and add a new one (addChild) and place it back at the start x/y position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文