Level 10 Lesson 01 [Drag and Drop]

Introduction
In this lesson you will learn how you can combine knowledge of the mouse coordinates and states to add some great game features.

Part A – Mouse X and Y
The mouse position is always available to you by using the mouse_x and mouse_y game variables. You can use mouse_x and mouse_y to accomplish some simple drag and drop behaviour in your game.

Watch the video: 10-01-A
Matching Project: L10-01-A.gmk


Challenge 1 – Drag and Drop
Watch the Preview Video: L10-01-X1-Preview


Preview File: L10-01-X1Preview.exe
Start with the file: L10-01-X1Start.gmk.

There are three parts to the this challenge.

a) Give rock objects a state variable that is 0 when then rock is not being dragged and 1 when the rock is being dragged. When the user left clicks on a rock, the rock should change its state to 1 and the rock should now change its x and y position constantly to match the position of the mouse. Of course you also have to add that another click on a rock that is being dragged should let go of the rock by changing its state back to 0.

b) To avoid the problem of being able to grab more than one rock at a time, we will create a global variable called global.dragState=0. When no objects are being dragged, the variable will equal 0. When you start to drag a rock, change this global variable to 1 so you can remember that an object is currently being dragged. Go back to your code from part a and add use this global variable in your code so that you don’t pick up rocks unless this global variable is 0, you change the variable to 1 when you start dragging a rock, and you change this variable back to 0 when you release a rock.

c) We don’t want to be able to drop rocks on top of other rocks. When the user clicks to let go of a rock, you can check if the current location would be free of a collision with another rock by using the place_meeting script. Try to use place_meeting to make sure that the current position would be a rock free collision location. If so, drop the rock. If not, don’t drop the rock (maybe play a bad sound)!

Challenge 1 Solution
Solution Video: L10-01-X1-Solution
Solution File: 10-01-X1-Done.gmk (has all three parts solved)

Challenge 2 – Select Units and Way Points
Here’s a great little game task. Select a game unit and tell it where to walk with a way point.

Watch the Preview Video: L10-01-X2Preview

Preview File: L10-01-X2Preview.exe
Start with the file: L10-01-X2Start.gmk.

Watch the preview video and run the preview file. Try to imitate the program without any hints! If you need hints, here are some – and if they don’t help, give the solution video a watch.

a) give each bones a state variable that determines whether or not they are selected. Selecting and deselecting is done with the left click. Also add some code in the draw event of the bones so that when a bones is selected you draw the bones sprite and also a circle around the bones (remember to change draw color so you’re not drawing black on black).

b) just before you change the state variable of a bones to 1, use a with statement to tell all the bones objects to change their state to 0. This way selecting a unit deselects all other units.

c) give each bones a variable that determines whether or not they are moving and also give them variables to keep track of an x and y destination coordinate.

d) when a bones detects a global right click, and if they are selected, they will change their moving variable to 1. The step method should then be coded to check whether or not they are moving. If they are, find out the direction toward their x and y destination coordinates and move that way.

e) if a bones is moving and the distance between it’s destination is less than it’s speed value, it should stop moving.

Hopefully these hints help you out a bit. They try to bring together a few of the concepts from earlier in the course – especially the idea of states.

Challenge 2 Solution
Solution Video: 10-01-X2-Solution (has all parts solved)
Solution File: 10-01-X2Done.gmk