【问题标题】:How to add a unique function to all the movieclips in AS3?如何为AS3中的所有movieclip添加一个独特的功能?
【发布时间】:2014-03-28 08:18:33
【问题描述】:

我想为每个影片剪辑添加一个特定的功能。我添加了一个事件监听器,但所有的影片剪辑都在做同样的事情。 我注意到我不能用 i 变量来做到这一点,因为它是 11 你能帮我找到另一种方法吗?

  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);
    }
}
    }

【问题讨论】:

    标签: actionscript-3


    【解决方案1】:

    你的gotostore 函数是一个嵌套函数,这是一个非常糟糕的设计实践,不要使用嵌套函数。您可以使用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...
        }
    }
    

    【讨论】:

    • 所以我有 50-100 件商品,每件商品我都需要它来将我带到特定站点。如何为每个项目添加特定的事件侦听器?
    • 我用过的所有这些都在做同样的事情。我希望每个项目都做不同的事情。
    • 你可以在switch的情况下设置不同的动作...看代码。
    • 当你不知道 Flash 中的所有技巧时,当你的救命稻草这个网站真的很有帮助,希望它有效
    • 查找有关如何使用 Flash IDE 的初学者指南/教程。
    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多