actionscript_ActionScript中的一些棘手的错误/解决方案

actionscript

In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two.

在从事动作脚本开发人员的漫长职业生涯中,我整夜不眠,经常努力解决一些小问题,这些问题实际上占用了我大量的开发时间。 后来发现解决方案是一两行。

Here are some of the common mistakes and some quick code tricks and snippets I used, which can help other developers avoid sleep-less nights.

这是我使用的一些常见错误以及一些快速的代码技巧和摘要,可以帮助其他开发人员避免失眠。

Problem/Solution #1

问题/解决方案#1

This is an easy mistake to make when new to AS3.

对于AS3来说,这是一个容易犯的错误。

I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here's the original code:

我设置了一个for循环,以通过Array向后计数。 但是出于某些奇怪的原因,循环永远不会终止! 很简单,这是原始代码:

var i:uint;
for( i=arr.length-1; i>=0; i-- )
{
} 

Problem/Solution #2

问题/解决方案2

Once I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the "abstract" keyword from ActionScript meant a workaround.

一旦我需要在AS3中使用严格的抽象类,再一次,仅公开构造函数,并且ActionScript中没有“ abstract”关键字意味着有一种解决方法。

package
{
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;
    
    public class AbstractExample
    {
        public function AbstractExample()
        {
	            if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new Error( 'AbstractExample must be extended' );
	            
	            // rest of constructor here...
	        }      
	    }
	} 

Problem/Solution #3

问题/解决方案#3

As class constructors in ActionScript 3.0 must be public, there's not a really clean way to force a class to be a singleton as there is in ActionScript 2.0 or Java say.

由于ActionScript 3.0中的类构造函数必须是公共的,因此没有像ActionScript 2.0或Java中所说的那样真正干净的方法来强制类成为单例。

package
{
    public class Singleton
    {
        public static var instance:Singleton;
        
        public static function getInstance():Singleton
        {
            if( instance == null ) instance = new Singleton( getInstance );
            return instance;        
        }
        
        public function Singleton( caller:Function )
        {
            if( caller == Singleton.getInstance )
            {
                // instantiate class here   
            }
            else
            {
                throw new Error( 'Singleton is a singleton, use getInstance();' );
            }
        }
    }
} 

So you can only access this class by calling

因此,您只能通过以下方式访问此类

Singleton.getInstance();

Problem/Solution #4

问题/解决方案#4

Copy motion as ActionScript 3.0

If you are a developer, you might have been tasked with changing timeline animation to code. This can be a challenging task, because you need to replicate complex animation using complex code. Making the timeline animation and your code match might be both challenging and time-consuming.

如果您是开发人员,则可能要负责将时间轴动画更改为代码。 这可能是一项艰巨的任务,因为您需要使用复杂的代码来复制复杂的动画。 使时间线动画与代码匹配可能既困难又耗时。

Your work just got easier: Now you only need to select a menu option, and your code is automatically written for you in Flash CS3. Flash copies the properties that define a motion tween that resides on a timeline and writes the ActionScript 3.0 code for you to apply to a symbol. You can choose to apply the code as timeline code or in class files for the FLA file.

您的工作变得更加轻松:现在,您只需要选择一个菜单选项,您的代码就会在Flash CS3中自动为您编写。 Flash复制了定义驻留在时间轴上的补间动画的属性,并编写了ActionScript 3.0代码供您应用于符号。 您可以选择将代码作为时间轴代码或在FLA文件的类文件中应用。

Here's how you copy motion as ActionScript 3.0, and apply it to a second symbol.

这是将动作复制为ActionScript 3.0并将其应用于第二个符号的方法。

1. Create a new symbol or group, and place it on the Stage on Layer 1.

1.创建一个新的符号或组,并将其放置在第1层的舞台上。

2. Animate that instance on Layer 1 using a timeline animation, such as a simple motion tween on Layer 1.

2.使用时间轴动画(例如,第1层上的简单补间动画)在第1层上对该实例进行动画处理。

3. Create a new layer (Layer 2).

3.创建一个新层(第2层)。

4. Create a new symbol or group, or drag another instance of your existing symbol to the Stage on Layer 2.

4.创建一个新的符号或组,或将现有符号的另一个实例拖到第2层的舞台上。

5. Select the entire animation that you want to copy on Layer 1 of the timeline, right-click the animation, and then choose Copy Motion as ActionScript 3.0 from the context menu. The ActionScript code copies to the clipboard.

5.选择要在时间轴的第1层上复制的整个动画,右键单击该动画,然后从上下文菜单中选择“将动作复制为ActionScript 3.0”。 ActionScript代码将复制到剪贴板。

Tip: Click the first frame of the animation, press the Shift key, and then click the last frame of the animation to select a span of frames.
6. Type an instance name to use in the ActionScript code. You want to enter the instance name of the instance you will apply the ActionScript code to.
提示:单击动画的第一帧,按Shift键,然后单击动画的最后一帧以选择帧范围。
6.键入要在ActionScript代码中使用的实例名称。 您想要输入要将ActionScript代码应用到的实例的实例名称。

7. Select your second instance that you added to Layer 2, and type the instance name you entered into the Property inspector.

7.选择添加到第2层的第二个实例,然后输入在属性检查器中输入的实例名称。

8. Create a new layer (Layer 3), and select the first frame of that layer. Open the Actions panel and paste (Control + V or Command + V) the code into the Script pane.

8.创建一个新层(第3层),然后选择该层的第一帧。 打开“动作”面板,然后将代码(Control + V或Command + V)粘贴到“脚本”窗格中。

9. Before you test your animation, you need to make sure that Layer 2 has the same number of frames (so the symbol doesn't disappear when the animation plays). Select a frame and press F5 to insert frames on the Timeline.

9.在测试动画之前,需要确保第2层具有相同数量的帧(因此,动画播放时该符号不会消失)。 选择一个帧,然后按F5键在时间轴上插入帧。

10. Select Control > Test Movie to play the animation. You successfully applied the motion tween from Layer 1 to the instance on Layer 2 using ActionScript code. The scripted animation, and the one using timeline animation, should match.

10.选择“控制”>“测试影片”以播放动画。 您已使用ActionScript代码成功地将运动补间从第1层应用于第2层上的实例。 脚本动画和使用时间轴动画的动画应该匹配。

For video tutorials about copying motion as ActionScript 3.0 code, see Copying and pasting ActionScript from an animation and Creating an effective workflow between design and development.

有关将动作复制为ActionScript 3.0代码的视频教程,请参阅从动画复制和粘贴ActionScript以及在设计和开发之间创建有效的工作流

翻译自: https://www.experts-exchange.com/articles/4255/Some-Tricky-Mistakes-Solutions-in-ActionScript.html

actionscript

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值