简体   繁体   中英

Createjs Drag and drop - drop target isn't registering

I'm attempting to do a drag and drop. I cant seem to register a drop when I release the circle over the rectangle. How do I fix the issue? My target is always null no matter where I drop it on the stage or over the rectangle.

var rectangle = new createjs.Container();
        rect = new createjs.Shape();
        rect.graphics.beginFill("blue").drawRect(50, 50, 250, 210);
        rect.name = "rectangle";
        rect.x = 300;
        rect.y = 300;
        rectangle.addChild(rect);
        stage.addChild(rectangle);

        circle = new createjs.Shape();
        circle.name = "mycircle";
        circle.graphics.beginFill("red").drawCircle(15, 15, 15);
        circle.x = 15;
        circle.y = 80;


        var dragger = new createjs.Container();

        dragger.addChild(circle);
        dragger.name = "dragger1";
        stage.addChild(dragger);

        dragger.on("pressmove", function (evt) {
            evt.currentTarget.x = evt.stageX;
            evt.currentTarget.y = evt.stageY;
            stage.update();
        });

        dragger.on("pressup", function (evt) {
            var target = dragger.getObjectUnderPoint(evt.rawX, evt.rawY, 0);
            console.log("this is my target");
            console.log(target);

        });

You are calling dragger.getObjectUnderPoint() , which will return an object within dragger that is under the point. I'm guessing you want it to return rect , which is not in dragger . Just run stage.getObjectUnderPoint() instead.

Note of course, that if your dragged object is under the point when you do the check, it will be returned instead. There are lots of ways to work around that but its a separate topic.

http://jsfiddle.net/gskinner/by3humee/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM