当脚本第一次在动作脚本中运行时如何禁用按钮

时间:2012-05-25 10:12:30

标签: actionscript-3 flash

我有动作脚本套袋游戏我希望当页面加载时添加到当前行李被禁用,当点击添加到新行李时它应该激活这里是我用于动作脚本中的瞄准游戏的代码

enter image description here

如图所示添加到当前行李第一次应该在按下添加到新行李后无效然后它应该变为活动

    package {
import com.greensock.loading.LoaderMax;
import com.greensock.loading.XMLLoader;
import com.greensock.loading.ImageLoader;

import com.greensock.TimelineLite;
import com.greensock.TweenMax;

import com.greensock.events.LoaderEvent;

import com.greensock.plugins.TweenPlugin;
import com.greensock.plugins.GlowFilterPlugin;
import com.greensock.plugins.BlurFilterPlugin;

import appkit.responders.NResponder;

import bdesignet.utilities.keepRatio;

import flash.display.MovieClip;
import flash.display.StageAlign;    
import flash.display.StageScaleMode;

import flash.display.BitmapData;
import flash.display.Bitmap;

import flash.net.URLRequest;
import flash.net.navigateToURL;

import flash.events.MouseEvent;
import flash.events.TimerEvent;

import flash.utils.Timer;

public class main extends MovieClip {
    private var _controlsTimeline:TimelineLite = new TimelineLite( { paused:true, onComplete:startGame } );
    private var _scoreTimeline:TimelineLite = new TimelineLite( { paused:true, onComplete:enableReplay, onReverseComplete:startGame } );

    private var _loadermainXML:XMLLoader = new XMLLoader("", { name:"mainXML", onComplete:onmainDataLoaded } );

    private var _files:LoaderMax;

    private var _glowCurrentBag:TweenMax;
    private var _glowNewBag:TweenMax;
    private var _glowReplay:TweenMax;

    private var _glowPicture:TweenMax;

    private var _xmlData:XML;

    private var _currentBagClicked:NResponder;
    private var _newBagClicked:NResponder;

    private var _replayOver:NResponder;
    private var _replayOut:NResponder;
    private var _replayClicked:NResponder;

    private var _currentItem:uint = 0;
    private var _currentBag:uint = 0;
    private var _currentBagItems:uint = 0;
    private var _currentBagType:String = "";
    private var _points:int = 0;

    private var _image:Bitmap = new Bitmap();

    private var _started:Boolean = false;

    private var _start:Date;
    private var _end:Date;

    private var _hours:Number = new Number;
    private var _minutes:Number = new Number;
    private var _seconds:Number = new Number;
    private var _totalTime:Number = new Number;

    private var _counterTimer:Timer = new Timer(1000);
    private var _counterTimerEvent:NResponder;

    TweenPlugin.activate([GlowFilterPlugin, BlurFilterPlugin]);

    public function main():void {
        score.alpha = 0;

        stage.align = StageAlign.TOP_LEFT;
        stage.scaleMode = StageScaleMode.NO_SCALE;

        _loadermainXML.url = "items.xml";

        _loadermainXML.load();
    }
    private function onmainDataLoaded(event:LoaderEvent):void {
        _xmlData = XML(_loadermainXML.content);

        _files = new LoaderMax( { name:"files", onComplete:onFilesLoaded, onProgress:onFilesProgress } );

        _glowCurrentBag = TweenMax.to(controls.addCurrentBag, 1, { glowFilter: { color:0xF7E9CF, alpha:.8, blurX:16, blurY:16 }, timeScale:2, paused:true } );
        _glowNewBag = TweenMax.to(controls.addNewBag, 1, { glowFilter: { color:0xF7E9CF, alpha:.8, blurX:16, blurY:16 }, timeScale:2, paused:true } );
        _glowReplay = TweenMax.to(score.replay, 1, { glowFilter: { color:0x695532, alpha:.8, blurX:16, blurY:16 }, timeScale:2, paused:true } );

        _glowPicture = TweenMax.to(_image, 1, { blurFilter: { blurX:20, blurY:20 }, alpha:0, timeScale:2, paused:true, onComplete:showItem, onReverseComplete:enableClicks } );

        for (var i:uint = 0; i < _xmlData.images.image.length(); i++ ) {
            _files.append( new ImageLoader(_xmlData.images.@folder + _xmlData.images.image[i].@url, { name:"image_" + i }) );
        }
        _files.load();


    }
    private function onFilesProgress(event:LoaderEvent):void {
        loader.value_txt.text = String(Math.round(event.target.progress * 100)) + " %";
    }
    private function onFilesLoaded(event:LoaderEvent):void {
        controls.numberBags.numberBags_txt.text = String(_currentBag);
        controls.remainingText.value_txt.text = String(_xmlData.images.image.length() - 1);

        _replayOver = NResponder.addNative(score.replay, MouseEvent.MOUSE_OVER, onOverReplay);
        _replayOut = NResponder.addNative(score.replay, MouseEvent.MOUSE_OUT, onOutReplay);
        _replayClicked = NResponder.addNative(score.replay, MouseEvent.CLICK, onClickedReplay);
        _replayClicked.paused = true;
        _replayOver.paused = true;
        _replayOut.paused = true;

        NResponder.addNative(controls.addCurrentBag, MouseEvent.MOUSE_OVER, onOverCurrentBag);
        NResponder.addNative(controls.addCurrentBag, MouseEvent.MOUSE_OUT, onOutCurrentBag);
        _currentBagClicked = NResponder.addNative(controls.addCurrentBag, MouseEvent.CLICK, onClickedCurrentBag);
        _currentBagClicked.paused = true;

        NResponder.addNative(controls.addNewBag, MouseEvent.MOUSE_OVER, onOverNewBag);
        NResponder.addNative(controls.addNewBag, MouseEvent.MOUSE_OUT, onOutNewBag);

        _newBagClicked = NResponder.addNative(controls.addNewBag, MouseEvent.CLICK, onClickedNewBag);
        _newBagClicked.paused = true;

        controls.addCurrentBag.buttonMode = true;
        controls.addNewBag.buttonMode = true;

        NResponder.addNative(score.emailLink, MouseEvent.CLICK, onLinkClick);
        score.emailLink.buttonMode = true;

        imageContainer.addChild(_image);

        _counterTimerEvent = NResponder.addNative(_counterTimer, TimerEvent.TIMER, onCounterTimer);
        _counterTimerEvent.paused = true;

        _controlsTimeline.append(TweenMax.to(loader, 1, { alpha:0, timeScale:2 } ));
        _controlsTimeline.append(TweenMax.to(controls.addCurrentBag, 1, { alpha:1, x:237, timeScale:2 } ));
        _controlsTimeline.append(TweenMax.to(controls.addNewBag, 1, { alpha:1, x:237, timeScale:2 } ), -0.5);
        _controlsTimeline.append(TweenMax.to(controls.currentBag, 1, { alpha:1, y:280, timeScale:2 } ));
        _controlsTimeline.append(TweenMax.to(controls.numberBags, 1, { alpha:1, y:280, timeScale:2 } ), -0.5);
        _controlsTimeline.append(TweenMax.to(controls.remainingText, 1, { alpha:1, timeScale:2 } ));
        _controlsTimeline.append(TweenMax.to(controls.timeText, 1, { alpha:1, timeScale:2 } ));

        _scoreTimeline.append(TweenMax.to(controls.timeText, 1, { alpha:0, timeScale:2 } ));
        _scoreTimeline.append(TweenMax.to(controls.remainingText, 1, { alpha:0, timeScale:2 } ));
        _scoreTimeline.append(TweenMax.to(controls.numberBags, 1, { alpha:0, y:416, timeScale:2 } ));
        _scoreTimeline.append(TweenMax.to(controls.currentBag, 1, { alpha:0, y:416, timeScale:2 } ), -0.5);
        _scoreTimeline.append(TweenMax.to(controls.addNewBag, 1, { alpha:0, x:467, timeScale:2 } ));
        _scoreTimeline.append(TweenMax.to(controls.addCurrentBag, 1, { alpha:0, x:467, timeScale:2 } ), -0.5);
        _scoreTimeline.append(TweenMax.from(score, 1, { blurFilter: { blurX:20, blurY:20 }, alpha:0, timeScale:2 } ));
        _scoreTimeline.append(TweenMax.to(score, 1, { alpha:1, timeScale:2 } ), -0.5);

        _controlsTimeline.play();
    }
    private function onLinkClick(e:MouseEvent):void {
        var request:URLRequest = new URLRequest("mailto:imran.husain@celeritas-solutions.com" + "?subject=Score&body=Score%20:%20" + _points + "\nDate%20:%20" + (_end.month + 1) + "/" + _end.date + "/" + _end.fullYear);
        //trace(request.url);
        navigateToURL(request, "_blank");
    }
    private function onCounterTimer(e:TimerEvent):void {
        _end = new Date();
        controls.timeText.value_txt.text = returnTimeString(Math.round((_end.time - _start.time) / 1000));
    }
    private function startGame():void {

        _start = new Date();

        _counterTimerEvent.paused = false;
        _counterTimer.start();

        _currentItem = 0;
        _points = 0;
        _currentBag = 0;
        _currentBagItems = 0;

        _currentBagType = _xmlData.images.image[0].@bag_type;

        showItem();

        !_started ? (TweenMax.from(_image, 1, { blurFilter: { blurX:20, blurY:20 }, alpha:0, onComplete:enableClicks } ), _started = true):void;
    }
    private function onOverReplay(e:MouseEvent):void {
        _glowReplay.play()
    }
    private function onOutReplay(e:MouseEvent):void {
        _glowReplay.reverse();
    }
    private function onClickedReplay(e:MouseEvent):void {
        _replayClicked.paused = false;
        _replayOver.paused = false;
        _replayOut.paused = false;

        controls.numberBags.numberBags_txt.text = 0;
        controls.remainingText.value_txt.text = String(_xmlData.images.image.length() - 1);
        controls.currentBag.currentItems_txt.text = "";

        controls.timeText.value_txt.text = "00:00";

        _scoreTimeline.reverse();
    }
    private function onOverCurrentBag(e:MouseEvent):void {
        _glowCurrentBag.play();
    }
    private function onOutCurrentBag(e:MouseEvent):void {
        _glowCurrentBag.reverse();
    }
    private function onClickedCurrentBag(e:MouseEvent):void {
        _xmlData.images.image[_currentItem].@bag_type != _currentBagType ? _points-=2 : _points+=2;

        _currentBagItems > 0 ? controls.currentBag.currentItems_txt.appendText(", ") : controls.currentBag.currentItems_txt.text = "";
        controls.currentBag.currentItems_txt.appendText(_xmlData.images.image[_currentItem].@name);
        _currentBagItems++;
        nextItem();
    }
    private function onOverNewBag(e:MouseEvent):void {
        _glowNewBag.play();
    }
    private function onOutNewBag(e:MouseEvent):void {
        _glowNewBag.reverse();
    }
    private function onClickedNewBag(e:MouseEvent):void {
        _xmlData.images.image[_currentItem].@bag_type != _currentBagType ? _points++ : _points--;

        _currentBagType = _xmlData.images.image[_currentItem].@bag_type;
        _currentBagItems = 1;
        _currentBag++;
        controls.numberBags.numberBags_txt.text = String(_currentBag);
        controls.currentBag.currentItems_txt.text = _xmlData.images.image[_currentItem].@name;

        nextItem();
    }
    private function nextItem():void {
        _currentItem++;

        _currentBagClicked.paused = true;
        _newBagClicked.paused = true;

        _glowPicture.restart();
    }
    private function enableReplay():void {
        _replayClicked.paused = false;
        _replayOver.paused = false;
        _replayOut.paused = false;

        score.replay.buttonMode = true;

    }
    private function enableClicks():void {
        _currentBagClicked.paused = false;
        _newBagClicked.paused = false;
    }
    private function showItem():void {
        if (_currentItem < _xmlData.images.image.length()) {
            controls.remainingText.value_txt.text = String(_xmlData.images.image.length() - (_currentItem + 1));

            _image.bitmapData = LoaderMax.getLoader("image_" + _currentItem).rawContent.bitmapData;

            keepRatio.findValues(_image.height, _image.width, 268, 390, true);

            _image.x = keepRatio.newX;
            _image.y = keepRatio.newY;
            _image.height = keepRatio.newHeight;
            _image.width = keepRatio.newWidth;
            _glowPicture.currentProgress == 1 ? _glowPicture.reverse() : null;
        }else {
            //_end = new Date();

            _counterTimerEvent.paused = true;
            _counterTimer.stop();

            score.text.total_txt.text = String(_xmlData.images.@total_score);
            score.text.value_txt.text = String(_points);
            score.timeText.value_txt.text = returnTimeString(Math.round((_end.time - _start.time) / 1000));

            _scoreTimeline.play();
        }
    }
    public function returnTimeString($val:Number):String{
        _hours = Math.floor($val/3600);
        _minutes = Math.floor($val/60);
        _seconds = Math.floor($val-(_minutes*60));
        return (_hours > 0 ? String(_hours) + ':' : '' ) + (_minutes > 9 ?       String(_minutes) : '0' + String(_minutes)) + ':' + (_seconds > 9 ? String(_seconds) : '0' + String(_seconds));
    }
}

}

1 个答案:

答案 0 :(得分:0)

请勿立即添加eventListener以添加到当前行李。 创建布尔(即newBagPressed = false)。 当按下添加到新包时,测试布尔值。 如果为false,则将其设置为true,然后将eventListener添加到添加到当前包中。

希望这会有所帮助:)