krk_gamer

Making a group draggable

Recommended Posts

Hi All,

I am trying develop a game that contains a color band of 5 different colored rectangles. So I created a group and added those rectangles to it(I have uploaded the image of the output). Now I am trying to write the code in such a way that the entire group will be moving horizontally once I drag it. But from what I found we can make the individual element in the group draggable but not the entire group. Anyways, I did write the code this way but I think its wrong as its not working. Here it is.

var leftRectGroup = game.add.group();
var colorarray = ["0x607D8B", "0xFF5722", "0xFFEB3B", "0x9E9E9E", "0x795548"];

for(var i = 0; i < 5; i++){
  var rectanglegraphics = game.add.graphics(0,0);
  rectanglegraphics.beginFill(colorarray, 1);
  rectanglegraphics.drawRect(0, i*256, 100, 256);
  leftRectGroup.add(rectanglegraphics);
  rectanglegraphics.endFill();
}

leftRectGroup.inputEnabled = true;
leftRectGroup.input.enableDrag();
leftRectGroup.input.allowHorizontalDrag = false;

Can anyone please provide me the right snippet to make the entire group draggable at once.

Thanks in Advance.

Capture.PNG

Share this post


Link to post
Share on other sites

Groups don't have input handlers, so they can't be dragged directly.

I'm not sure what the end goal of the game is, but if the entire bar will always be dragged as one image, then you could use a single bitmapData object and draw the rectangles onto that. You can then make a sprite using the bitmapData as the texture, and enable drag on that single sprite.

You could also enable drag on each rectangle, and in the update-loop update the positions of the rectangles so that they remain positioned relative to each other. This would create the illusion of them being a single draggable body. This option is more expensive, however, and should be done if you know you need to keep the rectangles separated.

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.