3
\$\begingroup\$

In the first tutorial for GameMaker (catch the clown), I want to set the direction of the clown after the collision with the wall.

I want to do it by pressing the wall object with the mouse (before the collision, to set the angle parameter). For example by pressing only once the wall object it would change the clown's direction after collision by 45 degrees, twice by 90 degrees, and so on.

How can I do that?

\$\endgroup\$
  • 1
    \$\begingroup\$ Are you asking how to change the direction or how to click an object and store a value? Try to be as clear as possible. Also tell us what you've tried, and what about it didn't work. \$\endgroup\$ – MichaelHouse Aug 20 '14 at 22:59
  • 1
    \$\begingroup\$ Looking for bounce angle calculation? stackoverflow.com/questions/573084/… \$\endgroup\$ – Superdoggy Jun 17 '15 at 2:27
1
\$\begingroup\$

To change the direction of the clown, you can change the built-in property, "direction."

Since you want to change the direction on mouse click, you can use the following code.

// create event for clown
// this variable will be used to keep track of how many times mouse was clicked
globalvar numberOfClicks;
global.numberOfClicks = 0;

// mouse press event
global.numberOfClicks++;
if(global.numberOfClicks == 1) {
    // if mouse was clicked once, then direction is 45 degrees
    clownDirection = 45;
} else if(global.numberOfClicks == 2) {
    // if mouse was clicked twice, then direction is 90 degrees
    clownDirection = 90;
    // reset the number of times mouse was clicked
    global.numberOfClicks == 0;
}

// collision event with wall and clown
with(obj_clown) {
    direction = clownDirection;
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ok, so in your wall object, you should store the variable for how many times it has been clicked.
You can do this by first adding a Create event on it and create/set variable "numberOfClicks".
This can be done by adding a piece of code there and putting "numberOfClicks = 0;" inside it.
Or you can simply add a create variable and name it "numberOfClicks".

Then add a Clicked event. In there either add a piece of code again and put "numberOfClicks++;".
Or you can add a set variable enter "numberOfClicks", check relative, and set it to 1.

Lastly in the a collision event with the wall object for your clown object you need to add a piece of code with the following inside:

direction += 45 * other.numberOfClicks;
other.numberOfClicks = 0;

This will increment the direction of your clown by 45 per click on that specific wall.
If you want to set to that angle rather than increment by that angle, change "+=" to "=".

My guess is also you might want to invert the direction of the clown first (so that it bounces right back if it collides with one that hasn't been clicked). This can be achieved by putting "direction = -direction;" before those other two lines of code.

I haven't been working in Game Maker for some time so hopefully this works out! :)
Don't hesitate to ask for more info if anything is unclear.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.