Jump to content

Drag and Scale Object


Recommended Posts

Hi, I am looking for a way to drag and scale my object in my game.

Right now this is my code:


test: function(){
        test1 = this.game.add.graphics(0, 0);
        test1.lineStyle(2, 0x0000FF, 1);
        test1.drawRect(168, 70, 46, 46);

        dot = this.game.add.sprite(214, 116, 'blackdot');
        dot.inputEnabled = true;
        dot.allowVerticalDrag = true;
        dot.allowHorizontalDrag = false;
        dot.input.setDragLock(true, true);

        dot2 = this.game.add.sprite(261, 116, 'blackdot');

        dot3 = this.game.add.sprite(168, 70, 'blackdot');

dot being the corner of the rectangle I want to drag and scale my rectangle.

dot2 and dot3 being the area I want to drag and scale my rectangle to.

Is there a way to do it?

Scaling the rectangle.png

Link to comment
Share on other sites


Okay I tried out the code and it is exactly what I want for dragging and scaling the object.

So I want to add in vertical scaling and horizontal scaling from one side at any point only.

Also I want to make it such that if the object is drag to like half the grid, it will automatically scale and snap to the nearest grid line.

Anyway to do it?



Link to comment
Share on other sites

Sounds like a bunch of individual solutions, so I guess you just have to try out some stuff, but I'd probably start by adding some children to the "main" sprite in each corner and maybe some larger one along the edges, all giving them a certain version of the click-drag-scale function (scale.set(x, constant), scale.set(constant, y) and the diagonal one scale.set(d)).

For snapping to the grid you can try something like x = Math.round(x/10)*10 which would give you values like 10, 20, ... 110, 120, ...


Link to comment
Share on other sites


  • Recently Browsing   0 members

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