使用AS3拖放游戏

时间:2012-06-06 14:12:38

标签: actionscript-3

import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;

var wordArray:Array = [mc_clipA, mc_clipB];
var imgArray:Array = [Aimg, Bimg];
var posArray:Array = [ {x:816.15, y:396.05}, {x:518.3, y:410.05}];

var ClipA:MovieClip;
var ClipB:MovieClip;

var startXpositionClipA:Number;
var startYpositionClipA:Number;
var startXpositionClipB:Number;
var startYpositionClipB:Number;

wordArray[0].buttonMode = true;
wordArray[1].buttonMode = true;

wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);

//Mouse Down Function for ClipA
function item_onMouseDownClipA(event:MouseEvent):void {
    ClipA = MovieClip(event.currentTarget);
    startXpositionClipA = ClipA.x;
    startYpositionClipA = ClipA.y;
    addChild(ClipA); 
    ClipA.startDrag();
    stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipA);
    wordArray[0].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
    wordArray[1].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
}

//Mouse Up Function for ClipA
function stage_onMouseUpClipA(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipA);
    ClipA.stopDrag();
    var indexClipA:int = wordArray.indexOf(ClipClipA);
    var matchClipClipA:MovieClip = MovieClip(imageArray[indexClipA]);
    if(matchClipClipA.hitTestPoint(ClipA.x, ClipA.y, true)) {
        ClipA.x = positionArray[indexClipA].x;
        ClipA.y = positionArray[indexClipA].y;
        ClipA.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownZip);
        ClipA.buttonMode = false;
        var tot=0; 
        tot=tot+10;
        score.text="You scored: " + tot +" Points ";
        var setTimer:Timer = new Timer(1000,60);
        setTimer.addEventListener(TimerEvent.TIMER,doTask);
        setTimer.start();
        function doTask(event:TimerEvent) {
        gotoAndStop(1,"Scene 2");
        setTimer.stop();
        }
    } 
    else
    {    
        ClipA.x = startXpositionClipA;
            ClipA.y = startYpositionClipA;
        wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
        wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);

    }
}

//Mouse Down Function for ClipB
function item_onMouseDownClipB(event:MouseEvent):void {
    ClipB = MovieClip(event.currentTarget);
    startXpositionClipB = ClipB.x;
    startYpositionClipB = ClipB.y;
    addChild(ClipB); 
    ClipB.startDrag();
    stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipB);
    wordArray[0].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
    wordArray[1].removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);

}

//Mouse Up Function for ClipB
function stage_onMouseUpClipB(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUpClipB);
    ClipB.stopDrag();
    var indexClipB:int = wordArray.indexOf(ClipB);
    var matchClipClipB:MovieClip = MovieClip(imageArray[indexClipB]);
    if(matchClipClipB.hitTestPoint(ClipB.x, ClipB.y, true)) {
        ClipB.x = positionArray3[indexTie].x;
        ClipB.y = positionArray3[indexTie].y;
        ClipB.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
        ClipB.buttonMode = false;
        var tot=0; 
        tot=tot;
        score.text="You scored: " + tot +" Points ";
        var setTimer:Timer = new Timer(1000,60);
        setTimer.addEventListener(TimerEvent.TIMER,doTask);
        setTimer.start();
        function doTask(event:TimerEvent) {
        gotoAndStop(1,"Scene 2");
        setTimer.stop();
        }
    } 
    else
    {    
        ClipB.x = startXpositionClipB;
            ClipB.y = startYpositionClipB;
        wordArray[0].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipA);
        wordArray[1].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDownClipB);
    }
}

这是关于拖拉游戏。将向用户显示特定的对象名称,然后用户必须将正确的图像拖放到指定的位置,一旦任务完成,它将动态导航到下一个场景。为此,我使用了计时器类。我在场景2中收到此错误,但在场景1中,代码运行完美。

命名约定都正确但我仍然会收到以下错误,

TypeError:错误#1009:无法访问空对象引用的属性或方法。     在MethodInfo-1156()     在flash.utils :: Timer / _timerDispatch()     在flash.utils :: Timer / tick()

有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

在执行鼠标侦听器后立即删除本地声明的方法“doTask”。您应该创建一个匿名内联侦听器

    var setTimer:Timer = new Timer(1000,60);
    setTimer.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void {
      gotoAndStop(1,"Scene 2");
      setTimer.stop();
    });
    setTimer.start();

有些人喜欢这样做,但我不是内联声明的忠实粉丝。我会在班上创建一个真正的方法作为听众。

private var setTimer:Timer;

function stage_onMouseUpClipB(event:MouseEvent):void {
  ...
  setTimer = new Timer(1000,60);
  setTimer.addEventListener(TimerEvent.TIMER,doTask);
  ...
}

function doTask(event:TimerEvent):void {
  gotoAndStop(1,"Scene 2");
  setTimer.stop();
}