使用ActionScript 3编写Coppercube脚本(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/scripting-coppercube-with-actionscript-3-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3540 个词 阅读量 0使用ActionScript 3编写Coppercube脚本(译文)
原文地址:https://www.codeproject.com/Articles/41974/Scripting-Coppercube-with-ActionScript-3
原文作者:MatthewCasperson
译文由本站 robot-v1.0 翻译
前言
Learn how to control a Coppercube SWF application using ActionScript
了解如何使用ActionScript控制Coppercube SWF应用程序
介绍(Introduction)
Coppercube是一种可用于为Windows或Web创建交互式3D应用程序的工具.虽然Coppercube包含许多行为,但自定义或复杂的操作仍需要自定义代码. Coppercube产生的SWF文件可以使用ActionScript编写脚本.本教程向您展示如何.(Coppercube is a tool that can be used to create interactive 3D applications, either for Windows or for the web. While Coppercube includes a number of behaviors, customized or complex actions still require custom code. The SWF files produced by Coppercube can be scripted using ActionScript. This tutorial shows you how.)
背景(Background)
此处提供的代码用作您可以查看的许多Coppercube教程的基础.(The code presented here is used as the basis for a number of Coppercube tutorials that you can view) 这里(here) .(.)
使用代码(Using the Code)
Coppercube应用程序可以通过以下方式编写脚本:(Coppercube applications can be scripted through the) SimpleAPI
,这是一组函数,可公开场景中元素的内部属性.我们将利用它来创建改变颜色的灯光.(, which is a set of functions that expose the inner properties of the elements in the scene. We will make use of this to create a light that changes color.)
首先,需要编译Coppercube应用程序.下载中包含样本项目,或者可以使用上所述的步骤创建自己的项目(First the Coppercube application needs to be compiled. The sample project is included in the downloads, or can create your own using the steps described on) 这个(this) 网页(您需要添加一个名为(web page (you will need to add an additional light called) Light2
如果遵循这些说明).(if following these instructions).)
Coppercube支持两种脚本语言:ActionScript(Flash或Flex)和Squirrel. ActionScript用于编写Flash应用程序的脚本,而Squirrel用于编写Windows应用程序的脚本.这意味着您的目标受众(网络或Windows)将定义您使用的语言.这将是一个Web应用程序,因此我们将使用ActionScript.(Coppercube supports two scripting languages: ActionScript (Flash or Flex) and Squirrel. ActionScript is used to script the Flash applications, while Squirrel is used to script the Windows applications. This means your target audience (web or Windows) will define what language you use. This will be a web application, so we will use ActionScript.)
对Flash Coppercube应用程序进行脚本编写基本上涉及将Coppercube创建的SWF文件加载到ActionScript编译器创建的新SWF中.我们将使用Flex编译器来创建最终的脚本应用程序.然后,您创建的SWF修改Coppercube SWF文件中的元素.(Scripting Flash Coppercube applications basically involves loading the SWF file created by Coppercube into a new SWF created by your ActionScript compiler. We will use the Flex compiler to create the final scripted application. The SWF you create then modifies the elements inside the Coppercube SWF file.)
为简单起见,您可以将Coppercube SWF嵌入最终的SWF文件中.过程描述(To keep things simple, you can embed the Coppercube SWF inside the final SWF file. The process is described) 这里(here) .我们对我们的班级使用相同的公式(. We use this same formula for our class called) CoppercubeSprite
.(.)
CoppercubeSprite.as(CoppercubeSprite.as)
package
{
import flash.display.*;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.ByteArray;
import gs.TweenMax;
[SWF(width="600", height="400", frameRate="100", backgroundColor="000000")]
public class CoppercubeSprite extends Sprite
{
// The CopperCube generated SWF file. Embedded into this file here
// so we get only one single SWF
[Embed(source="../resources/scripting.swf", mimeType="application/octet-stream")]
public static var EmbeddedCopperCubeSWFDataFile:Class;
private var loader:Loader;
private var simpleAPI:Object;
private var loadingFinished:Boolean;
private var applicationStarted:Boolean = false;
private var light:CCLight = null;
public function CoppercubeSprite()
{
addEventListener(Event.ENTER_FRAME, onGraphicsFrame);
// load the coppercube scene from the other swf
var copperCubeData:ByteArray = new EmbeddedCopperCubeSWFDataFile() as ByteArray;
loader = new Loader();
addChild(loader);
loader.loadBytes(copperCubeData);
loader.contentLoaderInfo.addEventListener(Event.INIT, onCoppercubeLoaded);
}
// called when the coppercube .swf file has been loaded and initialized
private function onCoppercubeLoaded(e:Event):void
{
loadingFinished = true;
simpleAPI = (loader.content as DisplayObject)['simpleAPI'];
}
// called every frame
public function onGraphicsFrame(event:Event):void
{
if (loadingFinished)
{
if (simpleAPI && simpleAPI.isLoaded() && !applicationStarted)
{
applicationStarted = true;
startCCApplication();
}
if (applicationStarted)
{
enterFrame(event);
}
}
}
public function startCCApplication():void
{
this.light = new CCLight
(this.simpleAPI, simpleAPI.getSceneNodeByName("Light2"));
this.light.Color = 0xFFFF00;
TweenMax.to(this.light, 1, {hexColors:{Color:0x00FFFF}, yoyo:0});
}
public function enterFrame(event:Event):void
{
}
}
}
首先,嵌入Coppercube SWF文件.您可以使用(First the Coppercube SWF file is embedded. You can use the) Embed
关键字可将资源添加到SWF文件中,从而使您可以分发一个文件,并且无需从外部资源加载资源(以及其固有的不可靠性).(keyword to add resources into a SWF file, giving you one file to distribute, and also negating the need to load resources from external sources (along with its inherent unreliability).)
[Embed(source="../resources/scripting.swf", mimeType="application/octet-stream")]
我们设置了一个在每一帧都调用的函数.在此功能中,我们可以修改Coppercube场景.(We set up a function to be called every frame. It is in this function that we can modify the Coppercube scene.)
addEventListener(Event.ENTER_FRAME, onGraphicsFrame);
然后,将嵌入的SWF文件与(The embedded SWF file is then loaded, with the) onCoppercubeLoaded
加载完成后要调用的函数集.(function set to be called when the loading is finished.)
// load the coppercube scene from the other swf
var copperCubeData:ByteArray = new EmbeddedCopperCubeSWFDataFile() as ByteArray;
loader = new Loader();
addChild(loader);
loader.loadBytes(copperCubeData);
loader.contentLoaderInfo.addEventListener(Event.INIT, onCoppercubeLoaded);
加载嵌入式SWF文件后,我们将(Once the embedded SWF file is loaded, we set) loadingFinished
至(to) true
指示可以修改Coppercube场景,并获得对(to indicate that the Coppercube scene is ready to be modified, and get a reference to the) SimpleAPI
接口.(interface.)
// called when the coppercube .swf file has been loaded and initialized
private function onCoppercubeLoaded(e:Event):void
{
loadingFinished = true;
simpleAPI = (loader.content as DisplayObject)['simpleAPI'];
}
的(The) onGraphicsFrame
函数直到什么都不会做(function won’t do anything until) loadingFinished
已设置为(has been set to) true
.一旦我们知道Coppercube SWF文件已加载,并且对(. Once we know that the Coppercube SWF file has been loaded and a reference to the) SimpleAPI
接口已获得,然后我们检查是否(interface has been obtained, we then check to see if the) SimpleAPI
已加载.一旦有了,(has loaded. Once it has, the) startCCApplication
函数被调用,并且(function is called, and the) applicationStarted
标志设置为(flag is set to) true
因此我们不再调用此函数.(so we don’t call this function again.)
if (simpleAPI && simpleAPI.isLoaded() && !applicationStarted)
{
applicationStarted = true;
startCCApplication();
}
应用程序启动后,我们将调用(Once the application has been started, we then call the) enterFrame
功能.(function.)
if (applicationStarted)
{
enterFrame(event);
}
它在(It is in the) startCCApplication
我们执行所需初始化的函数.在这种情况下,我们将创建一个新的(function that we perform any initialization required. In this case, we create a new instance of the) CCLight
类,并使用(class, and use the) 吐温麦克斯(TweenMax) 类来修改灯光的颜色.(class to modify the colour of the light.)
this.light = new CCLight(this.simpleAPI, simpleAPI.getSceneNodeByName("Light2"));
this.light.Color = 0xFFFF00;
TweenMax.to(this.light, 1, {hexColors:{Color:0x00FFFF}, yoyo:0});
补间是随时间推移平滑更改对象属性的过程,并且(Tweening is the process of changing the properties of an object smoothly over time, and) TweenMax
只是可用于ActionScript的许多补间库之一.我喜欢(is just one of many tweening libraries available for ActionScript. I like) TweenMax
因为它可以改变颜色,并且yoyo属性意味着属性可以来回循环.(because it can tween colours, and the yoyo property means that properties can by cycled back and forth.)
的(The) enterFrame
函数留空;的(function is left empty; the) TweenMax
类将为我们修改每帧的灯光颜色.(class will modify the colour of the light every frame for us.)
public function enterFrame(event:Event):void
{
}
CCLight.as(CCLight.as)
package
{
import mx.messaging.management.ObjectInstance;
public class CCLight
{
protected var ccObject:Object = null;
protected var simpleAPI:Object = null;
protected var color:int = 0x000000;
public function get Color():int
{
return this.color;
}
public function set Color(color:int):void
{
this.color = color;
simpleAPI.setLightColor(ccObject, color);
}
public function CCLight(simpleAPI:Object, ccObject:Object)
{
this.simpleAPI = simpleAPI;
this.ccObject = ccObject;
}
}
}
CCLight
是一个非常简单的类,旨在公开(is a very simple class designed to expose the colour property of a) Coppercube
轻如一双(light as a pair of) get
/(/) set
功能.这是因为(functions. This is because) TweenMax
希望能够修改属性,而不是向函数提供参数,这就是(expects to be able to modify a property rather than supply a parameter to a function, which is how the) SimpleAPI
作品.所以(works. So the) CCLight
该类仅保留对(class simply keeps a reference to the) SimpleAPI
界面以及要修改的内容,并对类别进行分类(interface and the light that it is to modify, and class the) SimpleAPI setLightColor
标准设置功能中的功能.这使得修改(function from within a standard set function. This makes modifying a) Coppercube
来自的对象(object from) TweenMax
简单.(simple.)
SpriteUIComponent.as(SpriteUIComponent.as)
package
{
import flash.display.Sprite;
import mx.core.UIComponent;
public class SpriteUIComponent extends UIComponent
{
public function SpriteUIComponent(sprite:Sprite)
{
super ();
explicitHeight = sprite.height;
explicitWidth = sprite.width;
addChild (sprite);
}
}
}
因为我们使用Flex创建应用程序,所以我们需要一种将Sprite添加到MXML文件中定义的应用程序的方法.不能将Sprites直接添加到Flex Application对象,因此,作为一种非常简单的解决方法,我们使用(Because we are using Flex to create the application, we need a way to add a Sprite to the application defined in the MXML file. Sprites can not be added directly to a Flex Application object, so as a very simple workaround we use the) SpriteUIComponent
类作为中间步骤.(class as an intermediate step.) SpriteUIComponent
扩展(extends the) UIComponent
类,这意味着可以将其添加到Flex Application对象,然后只需将提供的Sprite作为其自身的子级添加即可.(class, which means it can be added to a Flex Application object, and then simply adds the supplied Sprite as a child of itself.)
LightScripting.mxml(LightScripting.mxml)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
xmlns:ns1="*"
width="600"
height="400"
creationComplete="creationComplete()">
<mx:Script>
<![CDATA[
public function creationComplete():void
{
this.addChild (new SpriteUIComponent
(new CoppercubeSprite()));
}
]]>
</mx:Script>
</mx:Application>
MXML文件是应用程序的入口点.应用程序准备就绪后,(The MXML file is the entry point of the application. Once the application is ready, the) creationComplete
函数被调用,此时新(function is called, at which point a new) CoppercubeSprite
对象作为新子项添加到Flex Application对象中(object is added to the Flex Application object as a child of a new) SpriteUIComponent
目的.(object.)
当您运行该应用程序时,您将看到位于模型上方的灯光将在黄色和青色之间逐渐消失.可以使用相同的方法来修改Coppercube场景的几乎任何方面,并且我们还可以使用ActionScript的全部功能(例如补间).(When you run the application, you will see that the light that is sitting above the model will slowly fade between yellow and cyan. Almost any aspect of the Coppercube scene can be modified using the same methods, and we also have available to us the full power of ActionScript (such as tweening).)
查看现场演示(Check out the live demo) 这里(here) ,并下载源代码(, and download the source code) 这里(here) .(.)
历史(History)
- 1个(1)圣(st)2009年9月-初始职位(September, 2009 - Initial post)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
XML C# ASP.NET WebForms Ajax ASP 新闻 翻译