Actionscript 3 设置倒计时,并在达到0时停止游戏
我在AS3中有一个简单的游戏,我试图让倒计时开始,所以当倒计时到0时,游戏结束 当我测试这段代码时,我没有得到任何错误,但我也没有得到倒计时时钟。在该项目中,我创建了一个文本框,并使用嵌入字体使其动态化Actionscript 3 设置倒计时,并在达到0时停止游戏,actionscript-3,flash,timer,Actionscript 3,Flash,Timer,我在AS3中有一个简单的游戏,我试图让倒计时开始,所以当倒计时到0时,游戏结束 当我测试这段代码时,我没有得到任何错误,但我也没有得到倒计时时钟。在该项目中,我创建了一个文本框,并使用嵌入字体使其动态化 package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.getTimer; import flash
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.media.Sound;
import flash.media.SoundChannel;
public class MatchingGameObject10 extends MovieClip {
// game constants
private static const boardWidth:uint = 2;
private static const boardHeight:uint = 2;
private static const cardHorizontalSpacing:Number = 52;
private static const cardVerticalSpacing:Number = 52;
private static const boardOffsetX:Number = 145;
private static const boardOffsetY:Number = 70;
private static const pointsForMatch:int = 50;
private static const pointsForMiss:int = -5;
// variables
private var firstCard:Card10;
private var secondCard:Card10;
private var cardsLeft:uint;
private var gameScore:int;
private var gameStartTime:uint;
private var clockTimer:Timer;
// text fields
private var gameScoreField:TextField;
private var clock:TextField;
// timer to return cards to face-down
private var flipBackTimer:Timer;
// set up sounds
var theFirstCardSound:FirstCardSound = new FirstCardSound();
var theMissSound:MissSound = new MissSound();
var theMatchSound:MatchSound = new MatchSound();
// initialization function
public function MatchingGameObject10():void {
// make a list of card numbers
var cardlist:Array = new Array();
for(var i:uint=0;i<boardWidth*boardHeight/2;i++) {
cardlist.push(i);
cardlist.push(i);
}
// create all the cards, position them, and assign a randomcard face to each
cardsLeft = 0;
for(var x:uint=0;x<boardWidth;x++) { // horizontal
for(var y:uint=0;y<boardHeight;y++) { // vertical
var c:Card10 = new Card10(); // copy the movie clip
c.stop(); // stop on first frame
c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
c.y = y*cardVerticalSpacing+boardOffsetY;
var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
c.cardface = cardlist[r]; // assign face to card
cardlist.splice(r,1); // remove face from list
c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
c.buttonMode = true;
addChild(c); // show the card
cardsLeft++;
}
}
// set up the score
gameScoreField = new TextField();
addChild(gameScoreField);
gameScore = 0;
showGameScore();
}
// set up the clock
public function CountdownClock() {
startClock();
}
public function startClock() {
clockTimer = new Timer(1000,10);
clockTimer.addEventListener(TimerEvent.TIMER, clockTick);
clockTimer.addEventListener(TimerEvent.TIMER_COMPLETE, clockEnd);
clockTimer.start();
showClock();
}
public function clockTick(event:TimerEvent) {
showClock();
}
public function showClock() {
clock.text = String(clockTimer.repeatCount - clockTimer.currentCount);
}
public function clockEnd(event:TimerEvent) {
clock.text = "!";
}
// player clicked on a card
public function clickCard(event:MouseEvent) {
var thisCard:Card10 = (event.target as Card10); // what card?
if (firstCard == null) { // first card in a pair
firstCard = thisCard; // note it
thisCard.startFlip(thisCard.cardface+2);
playSound(theFirstCardSound);
} else if (firstCard == thisCard) { // clicked first card again
firstCard.startFlip(1);
firstCard = null;
playSound(theMissSound);
} else if (secondCard == null) { // second card in a pair
secondCard = thisCard; // note it
thisCard.startFlip(thisCard.cardface+2);
// compare two cards
if (firstCard.cardface == secondCard.cardface) {
// remove a match
removeChild(firstCard);
removeChild(secondCard);
// reset selection
firstCard = null;
secondCard = null;
// add points
gameScore += pointsForMatch;
showGameScore();
playSound(theMatchSound);
// check for game over
cardsLeft -= 2; // 2 less cards
if (cardsLeft == 0) {
MovieClip(root).gameScore = gameScore;
MovieClip(root).clockTimer = clockTimer;
MovieClip(root).gotoAndStop("gameover");
}
} else {
gameScore += pointsForMiss;
showGameScore();
playSound(theMissSound);
flipBackTimer = new Timer(2000,1);
flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
flipBackTimer.start();
}
} else { // starting to pick another pair
returnCards(null);
playSound(theFirstCardSound);
// select first card in next pair
firstCard = thisCard;
firstCard.startFlip(thisCard.cardface+2);
}
}
// return cards to face-down
public function returnCards(event:TimerEvent) {
if (firstCard != null) firstCard.startFlip(1);
if (secondCard != null) secondCard.startFlip(1);
firstCard = null;
secondCard = null;
flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
}
public function showGameScore() {
gameScoreField.text = "Score: "+String(gameScore);
}
public function playSound(soundObject:Object) {
var channel:SoundChannel = soundObject.play();
}
}
}
包{
导入flash.display.*;
导入flash.events.*;
导入flash.text.*;
导入flash.utils.getTimer;
导入flash.utils.Timer;
导入flash.media.Sound;
导入flash.media.SoundChannel;
公共类MatchingGameObject10扩展了MovieClip{
//博弈常数
专用静态const boardWidth:uint=2;
专用静态常数板高度:uint=2;
私有静态常量card水平间距:Number=52;
专用静态常量卡垂直间距:数字=52;
专用静态常数boardOffsetX:Number=145;
专用静态常数boardOffsetY:Number=70;
私有静态常量点格式:int=50;
私有静态常量pointsformis:int=-5;
//变数
私人信用卡:Card10;
个人信用卡:Card10;
私有var cardsLeft:uint;
个人分数:int;
私有变量gameStartTime:uint;
专用var时钟定时器:定时器;
//文本字段
私有变量gameScoreField:TextField;
专用变量时钟:TextField;
//将卡片正面朝下返回的计时器
私有变量flipBackTimer:计时器;
//设置声音
var theFirstCardSound:FirstCardSound=newfirstcardsound();
var theMissSound:MissSound=新MissSound();
var theMatchSound:MatchSound=new MatchSound();
//初始化函数
公共函数匹配GameObject10():void{
//列一张卡号清单
var cardlist:Array=new Array();
对于(var i:uint=0;i在您的问题中,您所说的是您添加到后台的文本字段,但在这种情况下,您当前的代码应该会给出关于行private var clock:TextField;
的错误,如果您的文本字段名为clock
,否则(您的文本字段名不是clock
)您可以在showClock()
函数中使用该名称,并且不要忘记调用将启动计时器的CountdownClock()
:
public function MatchingGameObject10(): void
{
// ...
CountdownClock();
}
及
您还可以像使用gameScoreField
one一样创建clock
文本字段,在这种情况下,您可以删除已插入到stage中的文本字段:
public function MatchingGameObject10(): void
{
// ...
clock = new TextField();
addChild(clock);
CountdownClock();
}
另外,不要忘记设置文本字段的位置,因为默认情况下它们将插入相同的位置(0,0)
要动态嵌入字体(使用ActionScript),请查看
希望这能有所帮助。我用跟踪替换了你的clock.text,它工作正常。你的输入是否在后台?我还删除了你对我的测试的自定义依赖项。
public function MatchingGameObject10(): void
{
// ...
clock = new TextField();
addChild(clock);
CountdownClock();
}