【问题标题】:ActionScript3 How to increment score when working with classesActionScript3 如何在使用类时增加分数
【发布时间】:2020-07-31 10:26:10
【问题描述】:

每次激活另一个类中的事件侦听器时,我想将 TextField 的值加一。事实证明这非常困难,因为只有在主类中的 Timer 完成后才能触发事件侦听器。

我尝试在布尔值上使用 getter 方法来确保触发了不同类中的事件侦听器。然而,这已被证明是不成功的,因为 Timer 仅在完成时触发一次。

我是 AS3 的初学者,因此我们将不胜感激。谢谢你

这是主要课程的代码

    public function t1Finish(e:TimerEvent):void {
        ranNum2 = randomNum1(0, 11);
        holes[ranNum2].sendUp();
    
        setCounter();


}




    
    public function setCounter():void {
        
        if(holes[ranNum2].returnHasHit() == true) {
                counter1++;
        txt1instance.text = counter1.toString();
    } else {
        txt1instance.text = counter1.toString();
    }
    }

这是其他类的代码

    public function click1(e:MouseEvent):void {
    this.gotoAndPlay(32);
    hasHit = true;
    }
    
    
    public function returnHasHit():Boolean {
        return hasHit;
    }

【问题讨论】:

    标签: class actionscript-3 flash adobe game-engine


    【解决方案1】:

    您需要正确安排应用层次结构。您的情况并不完全有问题:您有主类想要捕获某些从属对象中发生的一些事件。

    // That HOLE class.
    
    // I assume it is an inner event handler, not a public
    // interface, thus there's no need for it to be public.
    private function click1(e:MouseEvent):void
    {
        // Normally you don't need to explicitly state "this".
        gotoAndPlay(32);
        
        // Produce a custom event.
        dispatchEvent(new Event(Event.OPEN));
    }
    

    然后,您需要在主类中捕获该事件。

    // The MAIN class.
    
    // You need to subscribe for that custom event somewhere.
    private function subscribeAll():void
    {
        for each (var aHole:MovieClip in holes)
        {
            aHole.addEventListener(Event.OPEN, onOpen);
        }
    }
    
    // The custom event handler.
    private function onOpen(e:Event):void
    {
        // If you want to know which hole dispatched this event.
        var aHole:MovieClip = e.target as MovieClip;
        
        // Here you might want to put any additional criteria
        // to check if you want to accept this event.
        
        // This handler is invoked ONLY if that "click1" handler was invoked
        // on any of the holes, so there's no need to check "wasHit" interface.
        
        counter1++;
        txt1instance.text = counter1.toString();
    }
    

    【讨论】:

    • 非常感谢你是个传奇
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2020-06-15
    相关资源
    最近更新 更多