2015-10-15 29 views
0

我使用Adobe 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); 
} 

回答

0

要說清楚,我不是遊戲開發者。

其實,有的時候是一個Timer以1毫秒的間隔和另一個30毫秒的間隔之間沒有大的區別,因爲它是depending on the SWF file's framerate or the runtime environment ...但在這裏,關於使用Event.ENTER_FRAME事件,而不是一個Timer的是什麼?因爲Adobe表示here約計時器與ENTER_FRAME事件:

選擇使用計時器或ENTER_FRAME事件,這取決於內容的動畫。

對於長時間執行的非動畫內容,定時器優先於Event.ENTER_FRAME事件。

在你的情況下,內容是動畫的(即使你的遊戲仍然是基本的)。

然後你可以使用一個變種來設置hitbox的速度,你可以在任何時間更新:

var speed:int = 2; 

function timerEvent(evt:TimerEvent): void 
{  
    // ... 

    if (directionCheckerX == 0) { 
     hitbox.x -= speed; 
    } else { 
     hitbox.x += speed; 
    } 

    // ... 

} 

希望能有所幫助。

+0

這是非常有益的,謝謝! – Smebbs

相關問題