【问题标题】:Trying to create a very basic game, but experiencing some bottlenecking issues (I think)!尝试创建一个非常基本的游戏,但遇到了一些瓶颈问题(我认为)!
【发布时间】:2016-01-13 19:38:34
【问题描述】:

我正在使用 Adob​​e Flash Professional CS6 来创建游戏。我会把代码贴在下面。请注意,我使用 Flash 创建的两个符号不是由代码创建的。这些符号是十字准线符号和 Hitbox 符号。基本上,游戏的目标是点击 Hitbox 符号。我的问题是我遇到了似乎是瓶颈的问题。当我使用快速计时器多次单击 Hitbox 符号时,分数不会记录。我认为这来自(可能)无效的运动算法。但我似乎真的找不到改进的余地。一些帮助将不胜感激。

请注意,我必须将计时器从 Timer(1) 更改为 Timer(30)。这让瓶颈问题稍微好一些,但游戏流畅度降低了。

啊,我使用directionCheckerYdirectionCheckerX 变量的原因是我稍后会在开发中添加随机运动。随机计时器会将它们更改为 0 和 1,从而产生随机运动。

import flash.events.MouseEvent;
import flash.events.TimerEvent;

// Variables

var directionCheckerX:int=0;
var directionCheckerY:int=0;
var pointChecker:int=0;

// Croshair

var crosshair:Crosshair = new Crosshair();
addChild(crosshair);
Mouse.hide();

function moveCrossEvent (evt: MouseEvent) {
    crosshair.x = mouseX;
    crosshair.y = mouseY;
    evt.updateAfterEvent();
}

// Hitbox

var hitbox:Hitbox = new Hitbox();
addChild(hitbox);
hitbox.x=50;
hitbox.y=50;

// Timer

var myTimer:Timer = new Timer(30);
myTimer.addEventListener(TimerEvent.TIMER, timerEvent);
myTimer.start();
function timerEvent(evt:TimerEvent) {
    // Border code (Keeps the Hitbox away from out of bounds)
    if (hitbox.x <= 0) {
        directionCheckerX = 1;
    } else if (hitbox.x >= 550) {
        directionCheckerX = 0;
    }
    if (directionCheckerX == 0) {
        hitbox.x-=2;
    } else {
        hitbox.x+=2;
    }
    if (hitbox.y <= 0) {
        directionCheckerY = 1;
    } else if (hitbox.y >= 400) {
        directionCheckerY = 0;
    }
    if (directionCheckerY == 0) {
        hitbox.y-=2;
    } else {
        hitbox.y+=2;
    }
}

// EventListeners

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCrossEvent); 
hitbox.addEventListener(MouseEvent.CLICK, hitboxEvent);
stage.addEventListener(MouseEvent.CLICK, stageEvent);

function hitboxEvent (evt:MouseEvent) {
    pointChecker+=1;
    outputTxt.text = String(pointChecker);
    evt.stopImmediatePropagation();
    //evt.updateAfterEvent();
}
function stageEvent(evt:MouseEvent) {
    pointChecker-=1;
    outputTxt.text = String(pointChecker);
}

【问题讨论】:

    标签: performance actionscript-3 flash


    【解决方案1】:

    需要说明的是,我不是游戏开发者。

    实际上,有时Timer 间隔为 1 毫秒,而另一个间隔为 30 毫秒,因为它是 depending on the SWF file's framerate or the runtime environment ...,但在这里,使用 Event.ENTER_FRAME 事件而不是 Timer 呢?因为正如 Adob​​e 所说的 here 关于 Timers 与 ENTER_FRAME 事件:

    根据内容是否为动画,选择计时器或 ENTER_FRAME 事件。

    对于长时间执行的非动画内容,计时器优于 Event.ENTER_FRAME 事件。

    在你的情况下,内容是动画的(即使你的游戏仍然是基本的)。

    然后您可以使用 var 设置您的 hitbox 的速度,您可以随时更新:

    var speed:int = 2;
    
    function timerEvent(evt:TimerEvent): void 
    {    
        // ...
    
        if (directionCheckerX == 0) {
            hitbox.x -= speed;
        } else {
            hitbox.x += speed;
        }
    
        // ...
    
    }
    

    希望能有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2016-09-22
      • 2020-12-24
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多