0
votes

I'm a student I have a final project want to deliver it after two days. I'm making a drag and drop game, I watched a tutorial to do that. But after ending coding I faced a weird error! I've I checked that my code is the same as the code in the tutorial.


This is the Debug error report:

Attempting to launch and connect to Player using URL E:\FL\ActionScript\Drag and Drop Project\DragAndDrop.swf
[SWF] E:\FL\ActionScript\Drag and Drop Project\DragAndDrop.swf - 87403 bytes after decompression
TypeError: Error #1034: Type Coercion failed: cannot convert paper1$ to DragDrop.
    at Targets()[E:\FL\ActionScript\Drag and Drop Project\Targets.as:23]

My .fla File is containing 12 Objects to drag and another 12 Objects to drop on it.

The idea here is when drop the Object on the target the Object will become invisible and the target become visible (in .fla file target alpha = 0).

I made two classes:

DragDrop.as : for the objects that I'm going to drag. Targets.as : for the targets that I'm going to drop Objects on it.

Note: match function is to animate "GameOver" MovieClip When completing the game.

DragDrop.as:

package 
{
    import flash.display.*;
    import flash.events.*;

    public class DragDrop extends Sprite
    {
        var origX:Number;
        var origY:Number;
        var target:DisplayObject;

        public function DragDrop()
        {
            // constructor code
            origX = x;
            origY = y;
            addEventListener(MouseEvent.MOUSE_DOWN, drag);
            buttonMode = true;
        }

        function drag(evt:MouseEvent):void
        {
            stage.addEventListener(MouseEvent.MOUSE_UP, drop);
            startDrag();
            parent.addChild(this);
        }

        function drop(evt:MouseEvent):void
        {
            stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
            stopDrag();

            if(hitTestObject(target))
            {
                visible = false;
                target.alpha = 1;
                Object(parent).match();
            }

            x = origX;
            y = origY;
        }

    }

}

Targets.as:

package 
{

    import flash.display.*;
    import flash.events.*;


    public class Targets extends MovieClip
    {
        var dragdrops:Array;
        var numOfMatches:uint = 0;
        var speed:Number = 25;

        public function Targets()
        {
            // constructor code
            dragdrops = [paper1,paper2,paper3,paper4,paper5,paper6,
                         paper7,paper8,paper9,paper10,paper11,paper12,];

            var currentObject:DragDrop;
            for(var i:uint = 0; i < dragdrops.length; i++)
            {
                currentObject = dragdrops[i];
                currentObject.target = getChildByName(currentObject.name + "_target");
            }
        }

        public function match():void
        {
            numOfMatches++;
            if(numOfMatches == dragdrops.length)
            {
                win.addEventListener(Event.ENTER_FRAME, winGame);
            }
        }

        function winGame(event:Event):void
        {
            win.y -= speed;

            if(win.y <= 0)
            {
                win.y = 0;
                win.removeEventListener(Event.ENTER_FRAME, winGame);
                win.addEventListener(MouseEvent.CLICK, clickWin);
            }
        }

        function clickWin(event:MouseEvent):void
        {
            win.removeEventListener(MouseEvent.CLICK, clickWin);
            win.addEventListener(Event.ENTER_FRAME, animateDown);

            var currentObject:DragDrop;
            for(var i:uint = 0; i < dragdrops.length; i++)
            {
                currentObject = dragdrops[i];
                getChildByName(currentObject.name + "_target").alpha = 0;
                currentObject.visible = true;
            }
            numOfMatches = 0;
            addChild(win);
        }

        function animateDown(event:Event):void
        {
            win.y += speed;

            if(win.y >= stage.stageHeight)
            {
                win.y = stage.stageHeight;
                win.removeEventListener(Event.ENTER_FRAME, animateDown);
            }
        }
    }

}

...Thanks

1

1 Answers

0
votes

Are you sure what you're putting into array in Target instance's array IS DragDrop instances?

        dragdrops = [paper1,paper2,paper3,paper4,paper5,paper6,
                     paper7,paper8,paper9,paper10,paper11,paper12,];

I don't see any definitions for "paper1" in the code. And your error is telling paper1 is not a DragDrop instance.