Jump to content

Clicking & Dragging, seperate functions.


imShyness
 Share

Recommended Posts

Hello, quick question: 

Is it possible to have a container with onclick and ondrag functionality, but let them do different things? 

I'm working on a selection/drag-drop tool. Where if you click on the container a red border appears around the container (which means it's selected), but if you drag and drop it, It shouldn't be selected, it should just.. drag and drop. 

Link to comment
Share on other sites

function onDragStart(event) {
                this.data = event.data;
                this.start_data = {x: event.data.originalEvent.screenX, y: event.data.originalEvent.screenY};
                this.dragPos = this.data.getLocalPosition(this.parent);

                this.oldPosition = new PIXI.Point();
                this.oldPosition.copy(this.position);
                //this.alpha = 0.5;
                this.dragging = true;
            }

            function onDragEnd(event) {
                var ev_data = event.data.originalEvent;
                //if real dragend
                if (this.start_data) {
                    if (Math.abs(this.start_data.x - ev_data.screenX) > 2 || Math.abs(this.start_data.y - ev_data.screenY) > 2)
                        event.stopPropagation();
                }
                this.dragging = false;
                // set the interaction data to null
                this.data = null;
            }

            function onDragMove() {
                if (this.dragging) {
                    var newPosition = this.data.getLocalPosition(this.parent);
                    var pv = container.position, pv2 = this.oldPosition;
                    pv.x = pv2.x + (newPosition.x - this.dragPos.x);
                    pv.y = pv2.y + (newPosition.y - this.dragPos.y);
                }
            }

This is working solution from one of my projects. See that thing with "even.stopPropagation"? That will help you. May the force be with you.

Link to comment
Share on other sites

Please like my post. Also, if you do "camera" drag and not container, please use "pivot" instead of "position". You position container on screen and specify pivot on your map. Like this, if you want point "whereIsCenter" to be in center of the screen, you do it:

container.position.set(renderer.width/2, renderer.height/2);
container.pivot.set(whereIsCenter.x, whereIsCenter.y);

That way scale also will work from/to center.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...