我使用Adobe Flash Professional CS6創建遊戲。我會在下面發佈代碼。請注意,我使用Flash創建的兩個符號不是由代碼創建的。這些符號是十字線符號和Hitbox符號。基本上,遊戲的目標是點擊Hitbox符號。我的問題是我遇到了似乎是瓶頸問題。當我用快速計時器很多次點擊Hitbox符號時,得分不會被註冊。我記得這是來自(可能)無效的運動算法。但我似乎無法找到改進的空間。一些幫助將不勝感激。試圖創建一個非常基本的遊戲,但遇到一些瓶頸問題(我認爲)!
注意,我必須將計時器從Timer(1)
更改爲Timer(30)
。這使得瓶頸問題變得更好一些,但是讓遊戲變得不那麼流利。
啊,以及爲什麼我使用directionCheckerY
和directionCheckerX
變量的原因是我稍後會在開發中添加隨機移動。隨機計時器會將這些更改爲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);
}
這是非常有益的,謝謝! – Smebbs