- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新6 :
Fenomenas建议我尽可能简单地重新创建所有内容。我对此表示怀疑,因为算法保持不变,并且性能似乎不是问题,这会产生什么不同。无论如何,这是我得到的唯一建议,所以这里是:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.getTimer;
[SWF(width="800", height="600", frameRate="40", backgroundColor="#000000")]
public class SimpleMovement extends Sprite
{
private static const TURNING_SPEED:uint = 180;
private static const MOVEMENT_SPEED:uint = 400;
private static const RADIAN_DIVIDE:Number = Math.PI/180;
private var playerObject:Sprite;
private var shipContainer:Sprite;
private var moving:Boolean = false;
private var turningMode:uint = 0;
private var movementTimestamp:Number = getTimer();
private var turningTimestamp:Number = movementTimestamp;
public function SimpleMovement()
{
//step 1: create player object
playerObject = new Sprite();
playerObject.graphics.lineStyle(1, 0x000000);
playerObject.graphics.beginFill(0x6D7B8D);
playerObject.graphics.drawRect(0, 0, 25, 50);
//make it rotate around the center
playerObject.x = 0 - playerObject.width / 2;
playerObject.y = 0 - playerObject.height / 2;
shipContainer = new Sprite();
shipContainer.addChild(playerObject);
shipContainer.x = 100;
shipContainer.y = 100;
shipContainer.rotation = 180;
addChild(shipContainer);
//step 2: install keyboard hook when stage is ready
addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);
//step 3: install rendering update poll
addEventListener(Event.ENTER_FRAME, updatePoller, false, 0, true);
}
private function updatePoller(event:Event):void
{
var newTime:Number = getTimer();
//turning
if (turningMode != 0)
{
var turningDeltaTime:Number = newTime - turningTimestamp;
turningTimestamp = newTime;
var rotation:Number = TURNING_SPEED * turningDeltaTime / 1000;
if (turningMode == 1) shipContainer.rotation -= rotation;
else shipContainer.rotation += rotation;
}
//movement
if (moving)
{
var movementDeltaTime:Number = newTime - movementTimestamp;
movementTimestamp = newTime;
var distance:Number = MOVEMENT_SPEED * movementDeltaTime / 1000;
var rAngle:Number = shipContainer.rotation * RADIAN_DIVIDE; //convert degrees to radian
shipContainer.x += distance * Math.sin(rAngle);
shipContainer.y -= distance * Math.cos(rAngle);
}
}
private function stageReady(event:Event):void
{
//install keyboard hook
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp, false, 0, true);
}
private final function keyDown(event:KeyboardEvent):void
{
if ((event.keyCode == 87) && (!moving)) //87 = W
{
movementTimestamp = getTimer();
moving = true;
}
if ((event.keyCode == 65) && (turningMode != 1)) //65 = A
{
turningTimestamp = getTimer();
turningMode = 1;
}
else if ((event.keyCode == 68) && (turningMode != 2)) //68 = D
{
turningTimestamp = getTimer();
turningMode = 2;
}
}
private final function keyUp(event:KeyboardEvent):void
{
if ((event.keyCode == 87) && (moving)) moving = false; //87 = W
if (((event.keyCode == 65) || (event.keyCode == 68)) && (turningMode != 0)) turningMode = 0; //65 = A, 68 = D
}
}
}
rendering took: 14 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 14 ms
rendering took: 14 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 24 ms
rendering took: 18 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 232 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 16 ms
rendering took: 12 ms
rendering took: 14 ms
rendering took: 12 ms
ships[id] = new GameShip();
ships[id].setMovementMode(1); //move forward
addEventListener(Event.ENTER_FRAME, movementHandler);
private final function movementHandler(event:Event):void
{
var newTimeStamp:uint = UtilLib.getTimeStamp(); //set current timeStamp
var distance:Number = (newTimeStamp - movementTimeStamp) / 1000 * movementSpeed; //speed = x pixels forward every 1 second
movementTimeStamp = newTimeStamp; //update old timeStamp
var diagonalChange:Array = getDiagonalChange(movementAngle, distance); //the diagonal position update based on angle and distance
charX += diagonalChange[0];
charY += diagonalChange[1];
if (shipContainer)
{ //when the container is ready to be worked with
shipContainer.x = charX;
shipContainer.y = charY;
}
}
private final function getDiagonalChange(angle:Number, distance:Number):Array
{
var rAngle:Number = angle * Math.PI/180; //convert degrees to radian
return [Math.sin(rAngle) * distance, (Math.cos(rAngle) * distance) * -1];
}
最佳答案
我不知道这里是否有任何黄金答案,但是我有一些建议。
首先,我将不对优化Math.PI / 180等问题进行任何查询。通常较高的帧速率应明确表明,简单的计算不会降低任何速度。
其次,解决偶尔出现的显示延迟尖峰:在我看来,这很像垃圾收集器运行得非常频繁。在对您的代码进行非常简短的浏览后,我没有发现任何频繁出现GC的明显原因,但是我有两个建议。首先,如果您有权访问Flash IDE,我将尝试在不使用Flex框架的情况下重新创建您的项目。 Flash项目除包含您所输入的代码外不包含任何代码,但是Flex使用了很多自己的工具,这可能并不总是很明显,并且您的代码与框架之间的某些交互可能会导致GC。
如果那没有帮助,那么另一件事是尝试制作一个大大简化的代码版本(如果可能的话,在Flash中),希望它足够简单而不触发相同的峰值。我的意思是,例如,附着在图形上的单个类仅具有一个用于键事件的侦听器和一个用于帧(或计时器)事件的第二侦听器,在该侦听器中未创建任何变量。如果最低版本没有显示这些峰值,那么应该有可能在那个版本和您的完整客户端之间进行三角测量,以找出导致峰值的原因。
最后,关于一般的平滑度,我唯一的评论是Flash的屏幕更新本质上有些不均匀,实际上,您只有两种方法可用。您可以根据帧更新来移动actor,这会使它们的移动随帧率的变化而稍微不均匀,或者根据经过的时间对其进行移动,这会使它们的整体移动变得平滑(以每秒像素为单位),但它们的显示会稍微不均匀(以移动像素为单位)每帧)。较高的FPS会放大差异。
另外,请务必记住,Flash进行更新后,它们在屏幕上的显示方式会受到视频卡的严重影响。尤其是您会发现,在一种环境中,剪切和垂直同步问题可能非常明显,而在另一种环境中却没有。开发人员没有真正的解决办法,除非通常避免播放非常高FPS的动画,并尽可能降低总体处理器负担。
编辑:有关帧更新定时“天生不平衡”的更多信息,请参阅this blog post。屏幕更新之间的延迟介于12-16ms之间,您无能为力。这是因为操作系统和浏览器会影响Flash计时工作方式的事实。 (即使在一部空电影中,您也会看到这种东西,这就是为什么该主题中有关优化数学等的许多评论不会对您有所帮助的原因。)您无法避免这种变化,但是正如我如上所述,您可以定制视觉效果来唤起您想要的效果。无论哪种方式,我认为峰值都值得担心。您正在查看的变化是微妙的,在有很多东西在进行的游戏中将很难注意到,但是尖峰令人震惊。
编辑2
您问:“您真的认为那些流畅的游戏使用与我相同的移动算法吗?”
答案是,我认为他们在做简单得多的事情。他们几乎可以肯定正在执行以下一项操作:
function onEnterFrame() { // move at a constant speed per frame
ship.angle += dtheta;
ship.x += speed * Math.cos( ship.angle );
ship.y += speed * Math.sin( ship.angle );
}
function onEnterFrame2() { // move at a constant speed per second
var dt:Number = getTimeSinceLastFrame();
ship.angle += anglePerSecond * dt/1000;
var dist:Number = speedPerSecond * dt/1000;
ship.x += dist * Math.cos( ship.angle );
ship.y += dist * Math.sin( ship.angle );
}
dt
限制为理想帧(1 / fps)的持续时间的最多2或3倍。
关于actionscript-3 - 优化2D Flash游戏的过渡/运动平滑度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1284886/
在 ActionScript 中深度克隆对象的最佳方法是什么? 最佳答案 执行此操作的最佳方法是将 ByteArray 与方法 writeObject 一起使用。像这样: function clone
我有以下问题:我从用户那里获取日期(作为字符串数据类型)。现在,我想知道 actionscript 中是否有一个函数可以将其转换为日期格式。现在,我只是解析字符串并将各个部分连接在一起。即: chan
我想知道是否有任何简单的方法可以通过使用某种应用程序(例如 ruby 的 irb 或 javasctip spidermonkey)来测试 actionscript,您只需打开终端并立即输入代码即
我没有特定的代码示例,但是有没有通用的方法来猜测代码片段是什么版本的 Actionscript:1 或 2 或 3? 我在某处读到,如果它是时间线中的代码,则它被认为是 Actionscript 1。
版本之间的主要区别是什么? 最佳答案 除了库更改之外,Actionscript 3 还针对完全不同的虚拟机 (AVM2) 进行编译和运行,该虚拟机是从头开始重新编写的。据报道,它执行编译的 AS3 代
我需要一点帮助来了解类在Actionscript 3中的工作方式。我理解您从“包”开始,然后为什么要导入任何必需的库,然后命名该类并说明它是公共/私有还是扩展任何内容。 在那之后,我不明白。看来您写的
我对以下语句有疑问 trace(Number("1/2")) //output NaN 但 trace(Number("1.2")) //output 1.2 所以,我有点困惑,为什么第一个语句没有给
当我的目标是 10.3 及更高版本时,此代码在 actionscript 3 中工作正常,但是当我的目标是 Flash Player 9 时,它给了我错误场景 1, 第 1 层,第 1 帧,第 7 行
我开始学习Flex和ActionScript,并遇到了有趣的陈述:无类型变量。那是: var x:*; 要不就 var x; 我发现它们可以保存undefined值。不能使用Object类型的变量。但
我不确定我的代码是否有问题或是否必须以不同的方式处理错误。我有一个Gear实例,另一个是bLine。齿轮落在线上时,我试图使两者同时运动。所以我在实例编码的内部是: var ev2:Event = n
我的问题基本上如下。有一个扩展 EventDispatcher 的类的实例。当我像这样向对象添加事件监听器时,一切顺利: myObject.addEventListener('eventName',
应该很容易。我有一个对象。我想修改它,但在修改之前我想保存它的副本以便我可以返回。我尝试设置副本=原始,但是当我修改原始的属性时,副本也会显示更改。我假设这是因为在 ActionScript 中任何时
如果我没有在代码中专门键入一个变量,它会编译为默认数据类型吗?例如,“for each ... in”函数在不输入变量的情况下效果最好: for each (var element in myArra
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我正在关注 hemanth sharma 关于 starling 框架的优秀教程系列。我或多或少地复制了他的代码,并对我想到的游戏进行了一些更改。代码与他在项目中使用的代码 80% 相同。尽管如此,我
我正在尝试构建一个基于 Web 的 Flash 应用程序。我对闪存很陌生。我想以两种形式开发它——演示版和付费版。要使此应用程序充当付费版本,我需要某种序列号。为了实现这一目标,我用谷歌搜索并遇到了类
我正在尝试编写一个简单的 as3 绘图类,但它不起作用。甚至没有触发事件。可能是什么问题? 用法: var drawingBoard:Drawing = new Drawing(); drawingB
有没有可以从Action Script代码自动生成类图的工具? 最佳答案 我刚刚搜索谷歌并很快找到,http://seantheflexguy.com/blog/2007/11/20/actionsc
我正在开发一款小型联网 Flash 游戏。我有自己的 C/Linux 内置服务器,我通过闪存套接字连接到服务器。我正在争论是否将游戏逻辑的重载放在客户端或服务器上。主要担心的是有人破解了代码并破坏了其
跟踪 mouseX / mouseY 时或 localX / localY显示对象的坐标,为什么x从 1 开始,而 y从 0 开始? 例如,我用 MouseEvent.MOUSE_MOVE 在舞台上绘
我是一名优秀的程序员,十分优秀!