簡體   English   中英

如何為AS3中的所有動畫片段添加一個獨特的功能?

[英]How to add a unique function to all the movieclips in AS3?

我想為每個動畫片段添加一個特定的功能。 我添加了一個事件監聽器,但所有的影片剪輯都在做同樣的事情。 我注意到我不能用i變量做到這一點,因為它可以幫助我找到另一種方式嗎?

  package
{
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageOrientation;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.StageOrientationEvent;
import flash.system.Capabilities;
import flash.text.TextField;
import flash.ui.Keyboard;
import com.thanksmister.touchlist.renderers.TouchListItemRenderer;
import com.thanksmister.touchlist.events.ListItemEvent;
import com.thanksmister.touchlist.controls.TouchList;

[SWF( width = '480', height = '800', backgroundColor = '#000000', frameRate = '24')]
public class AS3ScrollingList extends MovieClip
{
    private var touchList:TouchList;
    private var textOutput:TextField;
    private var stageOrientation:String = StageOrientation.DEFAULT;

    public function AS3ScrollingList()
    {
        // needed to scale our screen
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;

        if(stage) 
            init();
        else
            stage.addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void
    {
        stage.removeEventListener(Event.ADDED_TO_STAGE, init);

        stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
        stage.addEventListener(Event.RESIZE, handleResize);

        // if we have autoOrients set in permissions we add listener
        if(Stage.supportsOrientationChange) {
            stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, handleOrientationChange);
        }

        if(Capabilities.cpuArchitecture == "ARM") {
                NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate, false, 0, true);
                NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
        }

        // add our list and listener
        touchList = new TouchList(stage.stageWidth, stage.stageHeight);
        touchList.addEventListener(ListItemEvent.ITEM_SELECTED, handlelistItemSelected);
        addChild(touchList);

        // Fill our list with item rendreres that extend ITouchListRenderer. 
        for(var i:int = 1; i < 3; i++) {
var item:TouchListItemRenderer = new TouchListItemRenderer();
item.index = i;
item.data = "This is list item " + String(i);
item.itemHeight = 120;
item.addEventListener(MouseEvent.CLICK, gotostore);
item.buttonMode = true;
touchList.addListItem(item);
}
//not nested function
function gotostore (e:MouseEvent) {
switch(e.currentTarget.name) {
    case "firstMovieClip":
    trace("buton1");
        //first movieclip clicked
        break;
    case "secondMovieClip":
            trace("buton2");
        //second movieclip clicked
        break;
    //etc...
    }
    }
    }

    /**
     * Handle stage orientation by calling the list resize method.
     * */
    private function handleOrientationChange(e:StageOrientationEvent):void
    {
        switch (e.afterOrientation) { 
            case StageOrientation.DEFAULT: 
            case StageOrientation.UNKNOWN: 
                //touchList.resize(stage.stageWidth, stage.stageHeight);
                break; 
            case StageOrientation.ROTATED_RIGHT: 
            case StageOrientation.ROTATED_LEFT: 
                //touchList.resize(stage.stageHeight, stage.stageWidth);
                break; 
        } 
    }

    private function handleResize(e:Event = null):void
    {
        touchList.resize(stage.stageWidth, stage.stageHeight);
    }

    private function handleActivate(event:Event):void
    {
        NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
    }

    private function handleDeactivate(event:Event):void
    {
        NativeApplication.nativeApplication.exit();
    }

    /**
     * Handle keyboard events for menu, back, and seach buttons.
     * */
    private function handleKeyDown(e:KeyboardEvent):void
    {
        if(e.keyCode == Keyboard.BACK) {
            e.preventDefault();
            NativeApplication.nativeApplication.exit();
        } else if(e.keyCode == Keyboard.MENU){
            e.preventDefault();
        } else if(e.keyCode == Keyboard.SEARCH){
            e.preventDefault();
        }
    }

    /**
     * Handle list item seleced.
     * */
    private function handlelistItemSelected(e:ListItemEvent):void
    {
        trace("List item selected: " + e.renderer.index);
    }
}
    }

你的gotostore函數是一個嵌套函數,這是一個非常糟糕的設計實踐,不使用嵌套函數。 您可以使用e.currentTarget.name檢查用戶單擊了哪個movieclip,這將為您提供用戶“選擇”的e.currentTarget.name片段的INSTANCE NAME

for(var i:int = 1; i < 11; i++) {
    var item:TouchListItemRenderer = new TouchListItemRenderer();
    item.index = i;
    item.data = "This is list item " + String(i);
    item.itemHeight = 120;
    item.addEventListener(MouseEvent.CLICK, gotostore);
    item.buttonMode = true;
    touchList.addListItem(item);
}
//not nested function
function gotostore (e:MouseEvent) {
    switch(e.currentTarget.name) {
        case "firstMovieClip":
            //first movieclip clicked
            break;
        case "secondMovieClip":
            //second movieclip clicked
            break;
        //etc...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM