简体   繁体   English

每次在AS3中激活该框架时,将变量增加1

[英]Increasing variable by 1 every time the frame is active with AS3

This is properly an easy question to answer for some, hehe: How can I increase a varible value by 1 every time it enters that spesific frame? 对于某些人来说,这是一个很容易回答的问题,嘿嘿:每当变量值进入那个特殊的框架时,如何将变量值增加1?

I curently have this code which runs one time and then it will not run again next time the timeline is on the frame: 我目前拥有这段代码,它将运行一次,然后在时间轴出现在框架上时将不再运行:

// This is at the end of the animation

var DAY = 0;

DAY++;
dayTextField.text = DAY;

gotoAndStop(7); // Go to the beginning and re-loop the animation

I have tried to do this code in a function that run from a stage.addEventListener(Event.ENTER_FRAME, function) , but this just increased the value non-stop over and over again. 我尝试在从stage.addEventListener(Event.ENTER_FRAME, function)运行的函数中执行此代码,但这只是一遍又一遍地不断增加了该值。

Thank you. 谢谢。

For some wierd reason I got it to work. 出于某种奇怪的原因,我开始使用它。 This code is added in the beginning and not at the end as I did last time. 这段代码是在开头而不是最后一次添加的,而不是我上次添加的。 Here is what I did: 这是我所做的:

var DAY = 0;

stage.addEventListener(Event.ENTER_FRAME, newDayFunc);

function newDayFunc(event:Event) {
    if (currentFrame == 1286) {
        if (Moon.hitTestObject(wallNewDay)) {
            DAY++;
            dayTextField.text = DAY;
            trace("What day it is:", DAY);
        }
    }
}

**Here is what I ended up using after figuring something out, hehe: **这是我弄清楚某些东西后最终使用的东西,呵呵:

// I figured it out. This is on the last frame.

// Variables
var DAY = 0;
//
// Listeners
stage.addEventListener(Event.ENTER_FRAME, newDayFunc);
//
// Functions
function newDayFunc(event:Event) {
        if (DAY < 7)) {
            DAY++;
            dayTextField.text = DAY;
            trace("What day it is:", DAY);
            stage.removeEventListener(Event.ENTER_FRAME, newDayFunc);
        } else {
            // Added other eventListeners that needed to be removed
            gotoAndStop("finished"); // The name of the finish frame
        }
}
//

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM